Unix iTOps Tube

Monday, May 14, 2012

[bash] Reading two files and change a specific field

Hi all.



I have 2 files like these:



file1


Code:


3    -2    5    4    .    .    .

 3    3    3    4    .    .    .

 2    2    3    4    .    .    .

3    -2    8    4    .    .    .


file2


Code:


0.4242    2    3    4    .    .    .

 2.562    7    3    4    .    .    .

 0.7242    5    5    4    .    .    .

1.1242    -2    3    4    .    .    .


and I need to read every lines from file1, cheking if ($1==3 && $2==-2) and modifying $3 to 77 in the corresponding line in file2.



The result should be:



Outputfile


Code:


0.4242    2    77    4    .    .    .

  2.562    7    3    4    .    .    .

  0.7242    5    5    4    .    .    .

 1.1242    -2    77  4    .    .    .


I am trying in bash with:




Code:


awk ' FS ="\t" ;{ if ($1==3 && $2==-2) {getline < "file2" ; $3=77; print $0} else {getline < "file2" ; print $0}  }' file1

but there is a problem with the field separator. It seems like '\t' is not applied. :wall:



Any suggestion?



Maybe is it simplier in python?



Thank you in advance!




No comments:

Post a Comment