3

I want help with scripting to work with two files, wherein file 1 lists amino acids which are in specific order (one below the other and also might repeat) and the second file 2 constitutes the characteristics feature listed under each amino acid. Here, I am trying to match the amino acid from list one (file 1) to obtain its characteristics features listed under the same amino acid of the second file (file 2)and copy it to an output file in the same order as mentioned in file 1.

For example File1.txt

    Threonine
    Glutamine
    Alanine
    Asparatate
    Glutamine
    Alanine
    Threonine

File2.txt

    [ Alanine ] 
    89.1    13.7    -3.12   -10.09
    [ Asparatate ]  
    133.1   30  -2.43   -10.35
    [ Glutamine ]   
    146.1   42.7    -3.46   -10.23
    [ Threonine ]   
    119.1   28.5    -2.43   -9.99   

The output I am expecting is as below: output.txt

    [ Threonine ]   
    119.1   28.5    -2.43   -9.99
    [ Glutamine ]   
    146.1   42.7    -3.46   -10.23
    [ Alanine ] 
    89.1    13.7    -3.12   -10.09
    [ Asparatate ]  
    133.1   30  -2.43   -10.35
    [ Glutamine ]   
    146.1   42.7    -3.46   -10.23
    [ Alanine ] 
    89.1    13.7    -3.12   -10.09 
    [ Threonine ]   
    119.1   28.5    -2.43   -9.99

I have tried using the below script in awk, which works with numbers as index other than words but not for this purpose.

awk 'FNR==NR { a[ "\\[ " $1 " \\]" ]; next } /^\[/ { f=0 } { for (i in a) if ($0 ~ i) f=1 } f' file1.txt file2.txt > output.txt

I am not knowing how to modify the script to make it work on the words even. Please tell me where I am going wrong and help me execute the script to get the output as desired.

I will highly appreciate your help.

Thanks in advance.

Asha

  • Yes, the spaces has to be preserved. Also, file 1 is a list of amino acids, which has to find its match in second file to grep lines below it. Also, the order has to preserved as is in the file 1 – Asha Apr 21 '15 at 21:04
  • only one word per line – Asha Apr 22 '15 at 5:09
4

Everything what you need to loop through acids in File1.txt and find matched line in File2.txt + 1 line which easy done by grep

for acid in $(sed 's/^\s*//' File1.txt)
do
    grep -FA1 "$acid" File2.txt
done > Output.txt

But if you like awk:

awk '
FNR!=NR{
    print "    [",$1,"]"
    print acids[$1]
    next
}
/\[/{
    acid=$2
    next
}
{
    acids[acid]=$0
}' File2.txt File1.txt > Output.txt
  • Thank you very much. The first script has worked fine but awk script didnot generate the required output. – Asha Apr 22 '15 at 5:35
  • @Asha The second script is tested on your data (be attentive to pass secon file (with numbers) first). – Costas Apr 22 '15 at 6:11
2

A quick way to do this is using:

while read amino_acid
do
    grep -A1 ${amino_acid} File2.txt >> output.txt
done < File1.txt
  • Thank you very much for the help. This has also worked to generate the output as required. – Asha Apr 22 '15 at 5:35
2

With xargs/grep:

xargs -n1 -I '{}' grep '{}' -A1 File2.txt <File1.txt

Explanation:

  • -n1: forces xargs to execute the command for every line
  • -I '{}': set a placeholer
  • grep '{}' -A1 File2.txt: the command to execute
    • -A1: print also the line after the search pattern
    • File2.txt: search trough File2.txt
  • <File1.txt: input is File1.txt
  • Thanks, this script has also helped me in getting the required output – Asha Apr 22 '15 at 5:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.