array + if in linux shell scripting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting array + if in linux shell scripting
# 15  
Old 07-30-2011
Hi everyone,

I am having two lists of equal number of files,
Code:
list1:
f_1a0s.pdb123fname
f_1a0t.pdb123fname
f_1acc.pdb123fname
f_1af6.pdb123fname
f_1aij.pdb123fname
f_1ap9.pdb123fname
f_1bh3.pdb123fname
f_1brx.pdb123fname
f_1c3w.pdb123fname

and

Code:
List2:
f_1a0s.pdbdist
f_1a0t.pdbdist
f_1acc.pdbdist
f_1af6.pdbdist
f_1aij.pdbdist
f_1ap9.pdbdist
f_1bh3.pdbdist
f_1brx.pdbdist
f_1c3w.pdbdist

Content of files from list1 is :
Code:
3.789894  3.775013        3.720026        3.766262        3.729790        3.775523        3.759575        3.781067        3.789970        0.000000        3.785133        3.756248        3.773160        3.720026        3.737493        3.773160        0.000000        3.772912        3.775922        3.737493

and content of files from list2 is:
Code:
126     ALA     124     VAL     5.242442
126     ALA     125     GLY     3.839224
126     ALA     126     ALA     0.000000
126     ALA     127     LEU     3.789894
126     ALA     128     THR     5.824391
126     ALA     129     LYS     8.606871
126     ALA     130     VAL     10.355069
126     ALA     131     TYR     9.790689
126     ALA     132     SER     11.737067

I want to compare 1st column of list1 files with 5th column of list2 files,
if it is same && the 2nd column of list2 files are "ALA", it must print 4th column of list2 files.
I used the below code:
Code:
fname=list1
exec<$fname
while read line1
do
fname2=membdistlist
exec<$fname2
while read line2
do
awk 'NR==1{s=$1; next} $2=="ALA" && $5==s {print $4} ' $line1 $line2
done
done

Output: It is printing "ALA" multiple number of times, which is wrong output.
Whereas, the command:
Code:
awk 'NR==1{s=$1; next} $2=="ALA" && $5==s {print $4} ' f_1a0s.pdb123fname f_1a0s.pdbdist

Which is showing single file from list 1 and single file from list2 is showing the correct output.
Can any one help in accessing this code for multiple files ????
Thanx in advance Smilie
# 16  
Old 07-30-2011
If you say the awk command gives you what you want, you can just make a loop to process all files like this:

Code:
while read f1 f2 ; do 
  awk 'NR==1{s=$1; next} $2=="ALA" && $5==s {print $4} ' $f1 $f2
done <(paste list1 List2)

But I'm afraid that the awk command will do what you want only if file1 contains only one line.
# 17  
Old 07-31-2011
Hi mirni,

Thanx for ur efforts.....
but the code is showing an error: "unexpected end of file"
Can u plz figure it out.
# 18  
Old 07-31-2011
ummm... sorry I forgot a redirection operator:
Code:
while read f1 f2 ; do 
  awk 'NR==1{s=$1; next} $2=="ALA" && $5==s {print $4} ' $f1 $f2
done < <(paste list1 List2)

or, in a more concise way:
Code:
paste list1 List2 | while read f1 f2 ; do 
  awk 'NR==1{s=$1; next} $2=="ALA" && $5==s {print $4} ' $f1 $f2
done

This User Gave Thanks to mirni For This Post:
# 19  
Old 08-01-2011
Hi Mirnis,

Above first code still shows an error
Quote:
syntax error near unexpected token `('
Whereas, the second given by you works perfectly. Thank you very much for this.

Now, I want to compare each of the 20 fields in list1 files with 5th field in list2 files and print their corresponding 4th field, separated by a tab.
Code:
paste lista listb | while read f1 f2;
do
awk 'NR==1{ala=$1; next} $2=="ALA" && $5==ala {print $4"\t"}' | awk 'NR==1{val=$2; next} $2=="VAL" && $5==val {print $4"\t"} ' | 
awk 'NR==1{leu=$3; next} $2=="VAL" && $5==leu {print $4"\t"} ' | awk 'NR==1{iso=$4; next} $2=="VAL" && $5==iso {print $4"\t"} ' 
$f1 $f2 > $f1"output"
done

and so on..... for all 20 fields

Is it possible in a single code or do i need to make 20 such programmes and then merge them into one?
# 20  
Old 08-02-2011
Hi,you can try this..
Code:
paste list1 list2 | while read f1 f2;do
awk 'NR==FNR{a[$20]=$20;b=$4;next}a[$5]{print b"\t"$4}' $f1 $f2;done

regards
ygemici
This User Gave Thanks to ygemici For This Post:
# 21  
Old 08-05-2011
Thanks Ygemici,

But, I am comparing $2 which may contain any one out of 20 different strings like ALA,VAL, etc.. and if it matches with any one of these, it must print the corresponding $4. If I am doing it for only one string (like ALA) it is showing the perfect result. If I will do it separately for all the 20 strings...and then concatenate or merge the 20 different files of a single file..it would be quite cumbersome....so i was looking for the help if I could do it in a same script.

Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Array size in C shell scripting

Hi, I would like to know how to define the size of the array in c shell scripting. (15 Replies)
Discussion started by: gopishrine
15 Replies

2. UNIX for Beginners Questions & Answers

Convert String to an Array using shell scripting in JSON file.

This is the sample json I have pasted here. I want all the IP address strings to be converted into an array. For example "10.38.32.202" has to be converted to everywhere in the JSON. There are multiple IPs in a JSON I am pasting one sample object from the JSON. But the IPs already in an Array... (11 Replies)
Discussion started by: vinshas1
11 Replies

3. Homework & Coursework Questions

Linux Shell Scripting If-else and Case

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: This is what is asked: If the user enters ‘3’, prompt the user for two file names. Verify that the file names... (2 Replies)
Discussion started by: cindy01
2 Replies

4. Shell Programming and Scripting

Assigning array values using awk in shell scripting

hi My script as below #!/bin/ksh for i in `seq 1 7` do a=$(awk '{print $i}' /home/rama/expenese.txt) done for i in `seq 1 7` do echo "${a}" done content of expense.txt is as below 5032 210179 3110 132813874 53488966 11459221 5300794 I want output as... (6 Replies)
Discussion started by: Ramakrishna V
6 Replies

5. Shell Programming and Scripting

Linux/Unix shell scripting vs Perl

Hi, I have general question: i have good working Perl script with .pl extension, and now I have to migrate all to another Linux box, and I was told that I can use only shell scripting, so I'm not sure how different those two things are, will it work without any changes . Is there anything smart I... (6 Replies)
Discussion started by: trento17
6 Replies

6. Shell Programming and Scripting

Shell Scripting Linux

In shell Can I pass " to a program ? If not what is the work around ..? Can I define * as a wild card charecter that will return all the values from the files ..? I am trying to create a tool that will return the values from a property file . This tool has to support all the wild card... (1 Reply)
Discussion started by: app1sxv
1 Replies

7. Linux

Linux/Unix Shell Scripting Book

I want to learn Linux/Unix shell scripting, I searched this forum but got some results for Unix Admin books and general Linux books. Would someone recommend a good Linux Shell Scripting book? I did order one book A Practical Guide to Linux(R) Commands, Editors, and Shell Programming ... (4 Replies)
Discussion started by: thoughts
4 Replies

8. Linux

Linux game programing or just shell scripting

Well Acording to my job... Anyhelp plz. I need some basic scripting stuff. (3 Replies)
Discussion started by: Irish Jimmy
3 Replies

9. Linux

Difference between Linux and Unix and also C and C++ and also about Shell Scripting

Please Let me know this Que and Ans please Because i applied for a H1B VISA nad i have a INTERVIEW as soon so please "Difference between Linux and Unix and also C and C++ and also about Shell Scripting about all Questions and Answers ." (3 Replies)
Discussion started by: madhav
3 Replies

10. Shell Programming and Scripting

Shell scripting for Unix and/or Linux

Anyone here knows any reasonably priced courses for independent (not corporate) users that can point me to. Already know Unix commands/vi/etc but now need scripting too. Any replies will be appreciated. (2 Replies)
Discussion started by: FastFood
2 Replies
Login or Register to Ask a Question