Basic line reading and file merge question

No doubt these questions have been answered many times, but I struggled to find them - sorry. 2 questions:

  1. I wish to read in a file one line at a time and do 'stuff' with it, such as:
file="tst2"
while IFS= read -r line
do

    echo `wget -qO - http://cactus.nci.nih.gov/chemical/structure/$line/smiles` >> smiles.txt
   
done <"$file"

If 'line' has 6 columns and I want only $4, how do I insert only that column into the web address rather the whole line?

  1. If I have 2 files A & B, both having multiple columns, how do I take these 2 files and create a new file with columns in the format A2, B1, A3. Where A2 denotes the 2nd column from file A etc.

Many thanks

awk '{system ("wget -qO - http://cactus.nci.nih.gov/chemical/structure/" $4 "/smiles")}
' tst2 > smiles.txt
  1. Assuming both files have the same numbers of lines:
awk 'NR==FNR{a[NR]=$1;next}{print $2, a[FNR], $3}' fileB fileA
1 Like

Thank you for such a quick reply - I should maybe have made my question a bit clearer.

  1. This is useful thank-you!
    However I was looking to access directly the elements of each line though, so that if for example $4 didn't return what I was after, then I could try $5. Then, depending on what column was finally used I would then print out either $1, $2 or $3 from that same line... plus some more stuff.

I realise now that I could just take each line and put it into a temporary new file and access its elements that way - it would just have been nicer if i was able to use some variable like $(line$2).

  1. Perfect thanks

You can do something like:

awk '{
  if($4 == "xx"){
     # actions if $4 == "xx"
  }
}
{
  system ("wget -qO - http://cactus.nci.nih.gov/chemical/structure/" $4 "/smiles")
}
' tst2 > smiles.txt