Reputation: 157
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
Upvotes: 1
Views: 228
Reputation: 46833
Does what you want and discards lines with empty fields (e.g., 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.
Upvotes: 0
Reputation: 200293
Perl solution:
perl -ne 'chomp; ($user, $pass) = split /:/;
print "http://mysever.com/2/tasks.php?account=$user%3A$pass\n"' my.txt
Upvotes: 0
Reputation: 77105
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
Upvotes: 1
Reputation: 3442
Use a little awk script:
awk -F: '{printf "http://mysever.com/2/tasks.php?account=%s%%3A%s\n", $1, $2}' < my.txt > my2
Upvotes: 2
Reputation: 289775
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
$ 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
Upvotes: 1