Help to replace a field in one file with a field from another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help to replace a field in one file with a field from another file
# 1  
Old 04-08-2009
Help to replace a field in one file with a field from another file

I have to replace a field in one file with a field from other file.

I came across this awk command to replace a field with one string
nawk -F'|' -v OFS='|' '$2="replace"' temp2 > temp3

I need to have something like cut -f2 -d "|" temp1 (a field from other file) instead of 'replace'

Is this possible in the above command or something else needs to be used??
# 2  
Old 04-08-2009
The command you have does not have a way to determine which line in the other file to get field2 from in order to insert into temp3. If you have a list of data you want to replace field2 with in temp2 then you can execute a for loop to complete your task.
Code:
list.txt contains

john
sally
mike

for name in $(< list.txt); do
    nawk -F'|' -v OFS='|' '$2="$name"' temp2 > temp3
done

An alternative would be the file you want field2 from has the same number of lines and is pipe delimited just like file temp2.
Code:
IFS="|"
cat file_with_field2 | while read field1 field2 rest ; do
    nawk -F'|' -v OFS='|' '$2="$field2"' temp2 > temp3
done

Else you need to provide more information about the content of file1 and file2 along with the desired output.
# 3  
Old 04-09-2009
Thanks ldapswandog..

I tried both the scripts.. the required field is getting replaced with $name/$field2..

For script

for name in $(<temp4); do
nawk -F'|' -v OFS='|' '$2="$name"' temp2 > temp3
done

Files
temp4
gg
re
tt
vv
qq

temp2
11|22|33|44|zz
11|22|33|44|zz
11|22|33|44|zz
11|22|33|44|zz
11|22|33|44|zz


outputf file(temp3)
11|$name|33|44|zz
11|$name|33|44|zz
11|$name|33|44|zz
11|$name|33|44|zz
11|$name|33|44|zz

required output
11|gg|33|44|zz
11|re|33|44|zz
11|tt|33|44|zz
11|vv|33|44|zz
11|qq|33|44|zz


My requirements
===================

file1
11|22|33|44|zz
11|22|33|44|zz
11|22|33|44|zz
11|22|33|44|zz
11|22|33|44|zz


file2
aa|bb|cc1|dd|55
aa|bb|cc2|dd|55
aa|bb|cc3|dd|55
aa|bb|cc4|dd|55
aa|bb|cc5|dd|55


required output
11|22|cc1|44|zz
11|22|cc2|44|zz
11|22|cc3|44|zz
11|22|cc4|44|zz
11|22|cc5|44|zz


I need to replace the 3rd field of file1 with 3rd field of file2 for all records(1st record should be replaced with 1st record only of the other file)
Both files have same number of lines as well as fields.
# 4  
Old 04-09-2009
Try this:

Code:
awk -F"|" 'NR==FNR{a[NR]=$3;next}
{$3=a[FNR]}
1' OFS="|" file2 file1

Regards
# 5  
Old 04-09-2009
cool.. it works...

Thanks so much Franklin..

I have never been into awk programming.. it would be great if you would let me know how this works.. just a small explanation.. this makes use of arrays??
# 6  
Old 04-09-2009
Code:
awk -F"|" 'NR==FNR{a[NR]=$3;next}
{$3=a[FNR]}
1' OFS="|" file2 file1

-F"|" -> set fieldseparator

NR==FNR -> if we read the first file (file2)

a[NR]=$3;next
-> set array a[NR] with the value of the 3th field and read the next line


The next lines of the code affect the 2nd file (file1):

{$3=a[FNR]} -> replace the 3th field with the content of a[FNR]

1
-> print the current line of the buffer


Have a read of this tutorial regarding the built-in variables NR and FNR:

Awk - A Tutorial and Introduction - by Bruce Barnett

Regards
# 7  
Old 04-09-2009
Thanks again Franklin...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to replace last field in a file,if first field matches

Hi, Need to replace last field in a file(/etc/passwd) ,if first filed matches with particular username. Scenario: cat testfor1 deekshi:x:7082:7082::/home/deekshi:/bin/bash harini1:x:7083:7083::/home/harini1:/bin/bash Here,if first field contains "deekshi", then i should replace... (4 Replies)
Discussion started by: Sumanthsv
4 Replies

2. Shell Programming and Scripting

Command/script to match a field and print the next field of each line in a file.

Hello, I have a text file in the below format: Source Destination State Lag Status CQA02W2K12pl:D:\CAQA ... (10 Replies)
Discussion started by: pocodot
10 Replies

3. Linux

How do I format a Date field of a .CSV file with multiple commas in a string field?

I have a .CSV file (file.csv) whose data are all enclosed in double quotes. Sample format of the file is as below: column1,column2,column3,column4,column5,column6, column7, Column8, Column9, Column10 "12","B000QRIGJ4","4432","string with quotes, and with a comma, and colon: in... (3 Replies)
Discussion started by: dhruuv369
3 Replies

4. Shell Programming and Scripting

Plz Help. Compare 2 files field by field and get the output in another file.

Hi Freinds, I have 2 files . one is source.txt and second one is target.txt. I want to keep source.txt as baseline and compare target.txt. please find the data in 2 files and Expected output. Source.txt 1|HYD|NAG|TRA|34.5|1234 2|CHE|ESW|DES|36.5|134 3|BAN|MEH|TRA|33.5|234... (5 Replies)
Discussion started by: i150371485
5 Replies

5. Shell Programming and Scripting

Compare two files Field by field and output the result in another file

Hi Friends, Need Help. I have file1.txt as File1.txt |123|A|7267|Hyder|Cross|Sell|7801 |995|A|7051|2008|Lunar|New|Year|Promotion|7801 |996|A|7022|Q108|Targ|Prospect|&|SSCC|Savings|Promo|7801 |997|A|7182|Q1|Feb-Apr|08|Credit|ITA|PA|SBA|Campaign|7801 File2.txt... (7 Replies)
Discussion started by: i150371485
7 Replies

6. Shell Programming and Scripting

replace blank field in file 2 with content of file 1

Something like vlookup in excel, column 2 in file 2 is blank and should be replaced by column 2 in file 1 based on comparing column 1 in both files. file1 Code: 1234~abc~b~c~d~e~f~g~h~09/10/09 5678~def~b~c~d~e~f~g~h~12/06/10 8910~hij~b~c~d~e~f~g~h~03/28/13... (1 Reply)
Discussion started by: sigh2010
1 Replies

7. Shell Programming and Scripting

Append 1st field from a file into 2nd field of another file

Hi, I've internally searched through forums for about 2+ hours. Unfortunately, with no luck. Although I've found some cases close to mine below, but didn't help so much. Actually, I'm in short with time. So I had to post my case. Hoping that you can help. I have 2 files, FILE1 ... (1 Reply)
Discussion started by: amurib
1 Replies

8. Shell Programming and Scripting

Appending 1st field in a file into 2nd field in another file

Hi, I've internally searched through forums for about 2+ hours. Unfortunately, with no luck. Although I've found some cases close to mine below, but didn't help so much. Actually, I'm in short with time. So I had to post my case. Hoping that you can help. I have 2 files, FILE1 ... (0 Replies)
Discussion started by: amurib
0 Replies

9. Shell Programming and Scripting

Replace third field of the first record in a file....

Hi, I am new to unix and am trying to do something below: I have a pipe delimited file with millions of records. I need to replace the third column of the first record to the number of lines in the file. How can I do that. Will appreciate any advice and help. Thanks Simi (3 Replies)
Discussion started by: simi28
3 Replies

10. Shell Programming and Scripting

replace a field in a CSV file

Hello all, I've a CSV file and need to replace 5th field if its value is "X". The exact requirement is to replace 5th field (column) with "Y" if a. it's value is "X" AND b. the line must start with ABC string i guess this can be done with awk. Pl help. For security reasons, the... (2 Replies)
Discussion started by: prvnrk
2 Replies
Login or Register to Ask a Question