switz
switz

Reputation: 25188

Loop over each line of text file in bash

Here's what the text looks like:

428, "http://www.youtube.com/watch?v=aqlJl1LfDP4", "NEW YORK,NEW YORK- FRANK SINATRA - YouTube", "moc.ebutuoy.www.", 1, 0, 0, 20, 96, 1329427038818198, "aiODk063S1YW" 
429, "http://www.youtube.com/watch?v=KIiUqfxFttM&feature=list_related&playnext=1&list=AVGxdCwVVULXfJnKOaSACBmNaC6ZZ71zS7", "Frank Sinatra - That's Life - YouTube", "moc.ebutuoy.www.", 1, 0, 0, 20, 96, 1329427228164029, "96X5yyV88p8v"

id,url,title,...unnecessary info...

I want to grab each row of a text file in a bash script. Get the second parameter (url), and then patch that into a script.

I'm not really sure how to loop for each line, and then pull out that second param.

Thanks

Upvotes: 3

Views: 7683

Answers (4)

ohaal
ohaal

Reputation: 5268

Print the second argument of each line:

awk '{ print $2 }' thefile.txt

To use it in a script (i.e. loop over it):

for URL in `awk '{ print $2 }' thefile.txt`
do
    echo $URL
done

One important thing to note is that the " and , will be included in the URL.

If you want to remove these, just grab the substring of the 2nd argument:

awk '{ print substr( $2, 2, length($2) -3 ) }' thefile.txt

We start at the 2nd character on the line, and show the full length -3 of the line.

In a loop:

for URL in `awk '{ print substr( $2, 2, length($2) -3 ) }' thefile.txt`
do
    echo $URL
done

Upvotes: 0

jon
jon

Reputation: 6246

Building on Oli's answer, you can remove beginning " and final ", using sed:

cut -f 2 -d " " thefile.txt | sed -e "s/^\"//" -e "s/\"\,$//"

Upvotes: 0

Alex Howansky
Alex Howansky

Reputation: 53563

for URL in $(cut -d, -f2 < file)
do
    echo $URL
done

Upvotes: 2

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272487

cut -f 2 -d " " thefile.txt

Upvotes: 1

Related Questions