Sina Sameti
Sina Sameti

Reputation: 47

write a shell script with a loop

i have an awk command as below :

awk 'FNR==NR{a[$1$2]=$3;next} ($1$2 in a) {print$1,$2,a[$1$2],$3}' ""each line of file 1""  >./awkfile/12as_132l.txt

and f1.txt content is :

1sas.txt 12ds.txt 
13sa.txt 21sa.txt

i want that my script read each line of fil1.txt and put the contents in this awk command...instead of """each line of file1"".. and execute the command like below:

    awk 'FNR==NR{a[$1$2]=$3;next} ($1$2 in a) {print$1,$2,a[$1$2],$3}'  1sas.txt 12ds.txt  >./awkfile/12as_132l.txt



  awk 'FNR==NR{a[$1$2]=$3;next} ($1$2 in a) {print$1,$2,a[$1$2],$3}' 13sa.txt 21sa.txt  >./awkfile/12as_132l.txt

i need a loop but the problem is that's a little strange for me .

Upvotes: 0

Views: 74

Answers (2)

Akshay Hegde
Akshay Hegde

Reputation: 16997

Here is Snippet, which reads 2 fields line by line from f1.txt, put it into variable, and used in awk

#!/usr/bin/env bash

while read -r  col1file col2file; do

       # your command goes here
       # this awk does nothing, replace it with your command
       awk '{  }' "$col1file" "$col2file" > someoutfile 

done < "f1.txt"

Test Results:

akshay@db-3325:/tmp$ cat f1.txt 
1sas.txt 12ds.txt 
13sa.txt 21sa.txt

akshay@db-3325:/tmp$ cat test.sh 
while read -r  col1file col2file; do

       echo "col1 : $col1file"
       echo "col2 : $col2file"

       # your command goes here
       # this awk does nothing, replace it with your command
       # awk '{  }' "$col1file" "$col2file" > someoutfile 

done < "f1.txt"

akshay@db-3325:/tmp$ bash test.sh 
col1 : 1sas.txt
col2 : 12ds.txt
col1 : 13sa.txt
col2 : 21sa.txt

Upvotes: 1

tech_help
tech_help

Reputation: 42

change the IFS to IFS=' ' and use loop .

Upvotes: 0

Related Questions