简体   繁体   中英

bash script to manipulate text

okay so i have a text file with the following info my.txt

user:pass
user:pass
user:pass
user:pass

i want to open the file grab the contents and manipulate each line then output it into a text called my2 file so it looks like this

http://mysever.com/2/tasks.php?account=user%3Apass
http://mysever.com/2/tasks.php?account=user%3Apass
http://mysever.com/2/tasks.php?account=user%3Apass
http://mysever.com/2/tasks.php?account=user%3Apass

can anyone help

使用一个小awk脚本:

 awk -F: '{printf "http://mysever.com/2/tasks.php?account=%s%%3A%s\n", $1, $2}' < my.txt > my2

Very basic on bash:

while IFS=":" read u p
do
  echo "http://mysever.com/2/tasks.php?account=$u%3A$p"
done < my.txt > my2.txt

Test

$ while IFS=":" read u p; do echo "http://mysever.com/2/tasks.php?account=$u%3A$p"; done < file > my2.txt
$ cat my2.txt
http://mysever.com/2/tasks.php?account=user%3Apass
http://mysever.com/2/tasks.php?account=user%3Apass
http://mysever.com/2/tasks.php?account=user%3Apass
http://mysever.com/2/tasks.php?account=user%3Apass

Using sed : You can use -i option to do infile substitutions or redirect to a new file

$ sed 's,\(.*\):\(.*\),http://mysever.com/2/tasks.php?account=\1%3A\2,' my.txt > my2.txt
http://mysever.com/2/tasks.php?account=user%3Apass
http://mysever.com/2/tasks.php?account=user%3Apass
http://mysever.com/2/tasks.php?account=user%3Apass
http://mysever.com/2/tasks.php?account=user%3Apass

Using awk :

$ awk -F: '{print "http://mysever.com/2/tasks.php?account="$1"%3A"$2}' my.txt > my2.txt
http://mysever.com/2/tasks.php?account=user%3Apass
http://mysever.com/2/tasks.php?account=user%3Apass
http://mysever.com/2/tasks.php?account=user%3Apass
http://mysever.com/2/tasks.php?account=user%3Apass

Perl solution:

perl -ne 'chomp; ($user, $pass) = split /:/;
  print "http://mysever.com/2/tasks.php?account=$user%3A$pass\n"' my.txt

Does what you want and discards lines with empty fields (eg, empty lines):

while read l; do
    [[ $l =~ .+:.+ ]] || continue
    printf "http://mysever.com/2/tasks.php?account=%s\n" "${l/:/%3A}"
done < my.txt > my2.txt

Caveat. Make sure the passwords don't contain any funny symbols.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM