[bash] Reading two files and change a specific field


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers [bash] Reading two files and change a specific field
# 1  
Old 05-12-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. Smilie

Any suggestion?

Maybe is it simplier in python?

Thank you in advance!
# 2  
Old 05-12-2012
If you apply \t to FS on the first line, then it will not work for that line. You should set FS before you start rading records. Also the way it is used:
Code:
FS ="\t"

This means that each record of file1 will get printed. Try something yo set the field separator like this:
Code:
awk -F '\t' '{ if ($1==3 && $2==-2) {getline < "file2" ; $3=77; print $0} else {getline < "file2" ; print $0}  }' file1

# 3  
Old 05-12-2012
Same problem! When the condition is satisfied, it prints the line with ' ' as a field separator, not '\t'. Smilie(
# 4  
Old 05-12-2012
Then you need to set the output field separator as well
Code:
awk 'BEGIN{ FS=OFS="\t" } {if ....

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to move specific files to directory based on match to file

I am trying to mv each of the .vcf files in the variants folder to the folder in /home/cmccabe/f2 that the .vcf id is found in file. $2 in file will always have the id of a .vcf in the variants folder. The line in blue staring with R_2019 in file up to the -v5.6 will always be an exact match to a... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

awk to create separate files but not include specific field in output

I am trying to use awk to create (in this example) 3 seperate text file from the unique id in $1 in file, if it starts with the pattern aa. The contents of each row is used to populate each text file except for $1 which is not needed. It seems I am close but not quite get there. Thank you :). ... (3 Replies)
Discussion started by: cmccabe
3 Replies

3. Shell Programming and Scripting

Bash to move specific files from folders in find file

I have a directory /home/cmccabe/nfs/exportedReports that contains multiple folders in it. The find writes the name of each folder to out.txt. A new directory is then created in a new location /home/cmccabe/Desktop/NGS/API, named with the date. What I am trying to do, unsuccessfully at the moment,... (7 Replies)
Discussion started by: cmccabe
7 Replies

4. Shell Programming and Scripting

Bash to tell download where specific files are stored

The bash below will download all the files in download to /home/Desktop/folder. That works great, but within /home/Desktop/folder there are several folders bam, other, and vcf, is there a way to specify by extention in the download file where to download it to? For example, all .pdf and .zip... (2 Replies)
Discussion started by: cmccabe
2 Replies

5. Shell Programming and Scripting

Bash to download specific files and save in two folders

I am trying to download all files from a user authentication, password protected https site, with a particular extension (.bam). The files are ~20GB each and I am not sure if the below is the best way to do it. I am also not sure how to direct the downloaded files to a folder as well as external... (7 Replies)
Discussion started by: cmccabe
7 Replies

6. Shell Programming and Scripting

Bash script read specific value from files of an entire folder

Hello, I heva a problem creating a script that read specifc value from all the files of an entire folder I have a number of email files into a directory and i need to extrect from each file 2 specific values. After that i have to put them into a new file that looks like that: To: value1 ... (1 Reply)
Discussion started by: ahmenty
1 Replies

7. Shell Programming and Scripting

Replace specific field on specific line sed or awk

I'm trying to update a text file via sed/awk, after a lot of searching I still can't find a code snippet that I can get to work. Brief overview: I have user input a line to a variable, I then find a specific value in this line 10th field in this case. After asking for new input and doing some... (14 Replies)
Discussion started by: crownedzero
14 Replies

8. Shell Programming and Scripting

reading configuration files in bash. Best way?

Context: I have a random pin number generator script that reads a tab-delimited file containing a location and a count: eg., mansfield 30 tokyo 15 smithville 34It produces random PIN# in the amount specified by the number in the second column. Currently, I read the file... (7 Replies)
Discussion started by: Bubnoff
7 Replies

9. Shell Programming and Scripting

Reading specific contents from 1 input files and appending it to another input file

Hi guys, I am new to AWK and unix scripting. Please see below my problem and let me know if anyone you can help. I have 2 input files (example given below) Input file 2 is a standard file (it will not change) and we have to get the name (second column after comma) from it and append it... (5 Replies)
Discussion started by: sksahu
5 Replies

10. Shell Programming and Scripting

reading files for specific dates

assume files are in a directory /data $ ls -ltr Just displaying Data and file name: 01/01/2004 file_3434_typea.dat 01/01/2004 file_3423_typea.dat 01/01/2004 file_3436_typea.dat 01/01/2004 file_3434_typeb.dat 01/01/2004 file_3423_typeb.dat 01/01/2004 file_3436_typeb.dat ... (3 Replies)
Discussion started by: siva_jm
3 Replies
Login or Register to Ask a Question