# array + if in linux shell scripting

**URL:** <https://community.unix.com/t/array-if-in-linux-shell-scripting/288329>\
**Category:** Shell Programming and Scripting\
**Created:** [May 19, 2011, 10:32am UTC](https://community.unix.com/t/array-if-in-linux-shell-scripting/288329 "2011-05-19T10:32:32Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![CAch](https://community.unix.com/letter_avatar/cach/32/6_449c533f586939b6e5f246632a46421f.png) [@CAch](https://community.unix.com/u/CAch)\
**Post date:** [May 19, 2011, 10:32am UTC](https://community.unix.com/t/array-if-in-linux-shell-scripting/288329/1 "2011-05-19T10:32:32Z")

</div>

Hi,

I am having two set of files with different number of columns and rows.  
A set of files have only single row with 20 columns.  
B set of files have 1000s of rows with 5 columns.  
both set contains equal number of files.

I want to save all the 20 columns of A in variables one by one and then compare it with 5th column of set B files. If it matches....it should print the 4th column of set B files.

Thanks in advance!!!! 🙂

---

<div class="post-metadata">

**Author:** ![radoulov](https://community.unix.com/user_avatar/community.unix.com/radoulov/32/489_2.png) [@radoulov](https://community.unix.com/u/radoulov)\
**Post date:** [May 19, 2011, 11:15am UTC](https://community.unix.com/t/array-if-in-linux-shell-scripting/288329/2 "2011-05-19T11:15:02Z")

</div>

It would be easier if you post sample data: input and expected output.

---

<div class="post-metadata">

**Author:** ![CAch](https://community.unix.com/letter_avatar/cach/32/6_449c533f586939b6e5f246632a46421f.png) [@CAch](https://community.unix.com/u/CAch)\
**Post date:** [May 20, 2011, 1:41am UTC](https://community.unix.com/t/array-if-in-linux-shell-scripting/288329/3 "2011-05-20T01:41:47Z")

</div>

Hi radoulov,  
Thanks for ur interest.  
sample files are:  
A files sample :

```nohighlight
3.764724 3.765135 3.780947 3.785922 2.918862 3.791665 3.776370 2.918862 3.773137 3.778527 3.773680 2.943051 3.782568 3.771691 3.773778 3.765135 0.000000 3.773137 3.773778 3.779468

```

B files sample :

```nohighlight
1 AAN 97 APN 7.789069
1 AAN 98 ASK 9.827249
1 AAN 99 DAS 7.531465
2 BBM 1 GFD 3.786426
2 BBM 2 RYT 0.000000
2 BBM 3 RGF 3.764724
2 BBM 4 MKH 6.393094
2 BBM 5 POU 8.005275
2 BBM 6 PHE 6.145675...... AND SO ON..

```

I want to check if first field (i.e, 3.764724 ) == 5th column && 2nd column =="BBM" then  
echo col4th of B files.

Likewise, I need to check it for every variable of A files from every corresponding B files.

Thanx 🙂

---

<div class="post-metadata">

**Author:** ![Franklin52](https://community.unix.com/user_avatar/community.unix.com/franklin52/32/500_2.png) [@Franklin52](https://community.unix.com/u/Franklin52)\
**Post date:** [May 20, 2011, 3:56am UTC](https://community.unix.com/t/array-if-in-linux-shell-scripting/288329/4 "2011-05-20T03:56:21Z")

</div>

Something to start with, this command gives the output of one Afile and one Bfile:

```nohighlight
awk '
NR==1{s=$1; next}
$2=="BBM" && $5==s {print $4}
' Afile Bfile

```

---

<div class="post-metadata">

**Author:** ![ygemici](https://community.unix.com/letter_avatar/ygemici/32/6_449c533f586939b6e5f246632a46421f.png) [@ygemici](https://community.unix.com/u/ygemici)\
**Post date:** [May 20, 2011, 3:58am UTC](https://community.unix.com/t/array-if-in-linux-shell-scripting/288329/5 "2011-05-20T03:58:38Z")

</div>

```nohighlight
for i in $(<file1); do awk '($5 " " $2) ~ /'$i' BBM/ {print $4}' file2; done
RGF
RYT

```

---

<div class="post-metadata">

**Author:** ![CAch](https://community.unix.com/letter_avatar/cach/32/6_449c533f586939b6e5f246632a46421f.png) [@CAch](https://community.unix.com/u/CAch)\
**Post date:** [May 20, 2011, 8:40am UTC](https://community.unix.com/t/array-if-in-linux-shell-scripting/288329/6 "2011-05-20T08:40:48Z")

</div>

Hi Franklin,

I have tried ur code. It works well :b:  
But it shows output only for a single A file and its corresponding single B file.  
Now, I need to run it for multiple files and save their data into new file.

I tried the following code, but it doesnt work:(

f

```nohighlight
name=list1
exec<$fname
while read line
       do
 
        f2name=list2
        exec<$f2name
        while read line2
        do
 
        awk ' NR==1{a=$1; next} $2=="BBM" && $5==a {print $4}' $line $line2> $line"456"
        done
done

```

thanks for the help!!!!

---------- Post updated at 07:40 AM ---------- Previous update was at 06:04 AM ----------

Hi ygemici,

code given by you also works for single A file and single B file.  
the output shown is :

```nohighlight
RGF
HGD
ALA
ALA
ALA
ALA
ALA
ALA
ALA
ALA
ALA
ALA
ALA

```

Although I need to display only RGF

Kindly help me to work with multiple files at a time.

---

<div class="post-metadata">

**Author:** ![Franklin52](https://community.unix.com/user_avatar/community.unix.com/franklin52/32/500_2.png) [@Franklin52](https://community.unix.com/u/Franklin52)\
**Post date:** [May 20, 2011, 10:03am UTC](https://community.unix.com/t/array-if-in-linux-shell-scripting/288329/7 "2011-05-20T10:03:34Z")

</div>

Do you have the files in one directory?

What are the names of the A files and the B files?

---

<div class="post-metadata">

**Author:** ![CAch](https://community.unix.com/letter_avatar/cach/32/6_449c533f586939b6e5f246632a46421f.png) [@CAch](https://community.unix.com/u/CAch)\
**Post date:** [May 20, 2011, 10:14am UTC](https://community.unix.com/t/array-if-in-linux-shell-scripting/288329/8 "2011-05-20T10:14:16Z")

</div>

yes, same directory has all the files.

A files are named as : npkf\_123,  
qb5f\_123  
and list1 consists the names of all these files.

whereas, B files are named as : mist\_npkf,  
mist\_qb5f etc  
and list2 consists the names of all B files.

I have created the above given shell in the same directory...

---

<div class="post-metadata">

**Author:** ![Franklin52](https://community.unix.com/user_avatar/community.unix.com/franklin52/32/500_2.png) [@Franklin52](https://community.unix.com/u/Franklin52)\
**Post date:** [May 20, 2011, 10:26am UTC](https://community.unix.com/t/array-if-in-linux-shell-scripting/288329/9 "2011-05-20T10:26:28Z")

</div>

list1 and list2?

Post some lines of those files and please use [code tags](http://www.unix.com/misc.php?do=bbcode&#code) when posting code or data examples.

---

<div class="post-metadata">

**Author:** ![CAch](https://community.unix.com/letter_avatar/cach/32/6_449c533f586939b6e5f246632a46421f.png) [@CAch](https://community.unix.com/u/CAch)\
**Post date:** [May 20, 2011, 10:33am UTC](https://community.unix.com/t/array-if-in-linux-shell-scripting/288329/10 "2011-05-20T10:33:21Z")

</div>

```php
 
List 1: 
npkf_123
qb5f_123
 
list2 : 
mist_npkf
mist_qb5f 
 

```

---

<div class="post-metadata">

**Author:** ![Franklin52](https://community.unix.com/user_avatar/community.unix.com/franklin52/32/500_2.png) [@Franklin52](https://community.unix.com/u/Franklin52)\
**Post date:** [May 20, 2011, 11:39am UTC](https://community.unix.com/t/array-if-in-linux-shell-scripting/288329/11 "2011-05-20T11:39:31Z")

</div>

Assuming the name of a Bfile is "mist\_" and the part before the "\_" of the Afile:

```nohighlight
while read Afile
do
  file=${Afile%_*}
  Bfile="mist_"$file
  awk 'NR==1{s=$1; next}
  $2=="BBM" && $5==s {print $4}' $Afile $Bfile > $file"456"
done < List1

```

---

<div class="post-metadata">

**Author:** ![CAch](https://community.unix.com/letter_avatar/cach/32/6_449c533f586939b6e5f246632a46421f.png) [@CAch](https://community.unix.com/u/CAch)\
**Post date:** [May 21, 2011, 11:05am UTC](https://community.unix.com/t/array-if-in-linux-shell-scripting/288329/12 "2011-05-21T11:05:01Z")

</div>

Hi franklin,

The above code gives the error :

> [@](#):
>
> Usage: file [-bcikLhnNsvz] [-f namefile] [-F separator] [-m magicfiles] file...  
> file -C -m magicfiles  
> Try `file --help' for more information.

---

<div class="post-metadata">

**Author:** ![Scott](https://community.unix.com/letter_avatar/scott/32/6_449c533f586939b6e5f246632a46421f.png) [@Scott](https://community.unix.com/u/Scott)\
**Post date:** [May 21, 2011, 11:12am UTC](https://community.unix.com/t/array-if-in-linux-shell-scripting/288329/13 "2011-05-21T11:12:40Z")

</div>

Hi.

I imagine this line:

```nohighlight
Bfile="mist_" file

```

should be:

```nohighlight
Bfile="mist_"$file

```

---

<div class="post-metadata">

**Author:** ![CAch](https://community.unix.com/letter_avatar/cach/32/6_449c533f586939b6e5f246632a46421f.png) [@CAch](https://community.unix.com/u/CAch)\
**Post date:** [May 21, 2011, 2:52pm UTC](https://community.unix.com/t/array-if-in-linux-shell-scripting/288329/14 "2011-05-21T14:52:00Z")

</div>

hi,  
the above code is also giving the error

> [@](#):
>
> fatal: cannot open file `mist\_' for reading (No such file or directory)

Whereas, B files are starting with "dist\_", i cant understand, wotz the problem....

---

<div class="post-metadata">

**Author:** ![CAch](https://community.unix.com/letter_avatar/cach/32/6_449c533f586939b6e5f246632a46421f.png) [@CAch](https://community.unix.com/u/CAch)\
**Post date:** [July 30, 2011, 7:59am UTC](https://community.unix.com/t/array-if-in-linux-shell-scripting/288329/15 "2011-07-30T07:59:32Z")

</div>

Hi everyone,

I am having two lists of equal number of files,

```nohighlight
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

```nohighlight
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 :

```nohighlight
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:

```nohighlight
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:

```nohighlight
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:

```nohighlight
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 🙂

---

<div class="post-metadata">

**Author:** ![mirni](https://community.unix.com/letter_avatar/mirni/32/6_449c533f586939b6e5f246632a46421f.png) [@mirni](https://community.unix.com/u/mirni)\
**Post date:** [July 30, 2011, 11:51am UTC](https://community.unix.com/t/array-if-in-linux-shell-scripting/288329/16 "2011-07-30T11:51:59Z")

</div>

If you say the awk command gives you what you want, you can just make a loop to process all files like this:

```nohighlight
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.

---

<div class="post-metadata">

**Author:** ![CAch](https://community.unix.com/letter_avatar/cach/32/6_449c533f586939b6e5f246632a46421f.png) [@CAch](https://community.unix.com/u/CAch)\
**Post date:** [July 31, 2011, 1:00pm UTC](https://community.unix.com/t/array-if-in-linux-shell-scripting/288329/17 "2011-07-31T13:00:17Z")

</div>

Hi mirni,

Thanx for ur efforts.....  
but the code is showing an error: "unexpected end of file"  
Can u plz figure it out.

---

<div class="post-metadata">

**Author:** ![mirni](https://community.unix.com/letter_avatar/mirni/32/6_449c533f586939b6e5f246632a46421f.png) [@mirni](https://community.unix.com/u/mirni)\
**Post date:** [July 31, 2011, 3:10pm UTC](https://community.unix.com/t/array-if-in-linux-shell-scripting/288329/18 "2011-07-31T15:10:02Z")

</div>

ummm... sorry I forgot a redirection operator:

```nohighlight
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:

```nohighlight
paste list1 List2 | while read f1 f2 ; do 
  awk 'NR==1{s=$1; next} $2=="ALA" && $5==s {print $4} ' $f1 $f2
done 

```

---

<div class="post-metadata">

**Author:** ![CAch](https://community.unix.com/letter_avatar/cach/32/6_449c533f586939b6e5f246632a46421f.png) [@CAch](https://community.unix.com/u/CAch)\
**Post date:** [August 1, 2011, 1:51am UTC](https://community.unix.com/t/array-if-in-linux-shell-scripting/288329/19 "2011-08-01T01:51:37Z")

</div>

Hi Mirnis,

Above first code still shows an error

> [@](#):
>
> 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.

```plaintext
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?

---

<div class="post-metadata">

**Author:** ![ygemici](https://community.unix.com/letter_avatar/ygemici/32/6_449c533f586939b6e5f246632a46421f.png) [@ygemici](https://community.unix.com/u/ygemici)\
**Post date:** [August 2, 2011, 3:30am UTC](https://community.unix.com/t/array-if-in-linux-shell-scripting/288329/20 "2011-08-02T03:30:09Z")

</div>

Hi,you can try this..

```nohighlight
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

[Next page](https://community.unix.com/t/array-if-in-linux-shell-scripting/288329.md?page=2)
