David Tuckwell
David Tuckwell

Reputation: 157

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

Upvotes: 1

Views: 228

Answers (5)

gniourf_gniourf
gniourf_gniourf

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

Ansgar Wiechers
Ansgar Wiechers

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

jaypal singh
jaypal singh

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

nif
nif

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

fedorqui
fedorqui

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

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

Upvotes: 1

Related Questions