Replace columns from File1 with columns from File2


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Replace columns from File1 with columns from File2
# 1  
Old 04-22-2009
Replace columns from File1 with columns from File2

Hi all,

I would like to replace some columns from file1 with columns from file2. Currently, I'm able to do it with the following command:


awk 'NR==FNR{a[NR]=$1;b[NR]=$2;c[NR]=$3;next;}
{$2=a[FNR];$4=b[FNR];$5=c[FNR];print}' file2 file1 > temp
mv -f temp file1


First, i make the changes and save it as a temp file. Then, i replace file1 with the temp file.
Is there any way i can write the changes to file1 directly without storing it in a temp file first.
How can i perform the same operation using sed?
Pls advice.
Thanks and Regards.

Example:
--------
File 1:
LAYOUT Label TEXT x1 x2 46 10 FOLLI018ULL
LAYOUT Label TEXT x1 x2 46 10 FOLLI018ULL
LAYOUT Label TEXT x1 x2 46 10 FOLLI018ULL
LAYOUT Label TEXT x1 x2 46 10 FOLLI018ULL
LAYOUT Label TEXT x1 x2 46 10 FOLLI018ULL

File 2:
V1 10 20
V5 100 20
V7 50 20
V15 20 50
V20 50 100

Output File 1:
LAYOUT V1 TEXT 10 20 46 10 FOLLI018ULL
LAYOUT V5 TEXT 100 20 46 10 FOLLI018ULL
LAYOUT V7 TEXT 50 20 46 10 FOLLI018ULL
LAYOUT V15 TEXT 20 50 46 10 FOLLI018ULL
LAYOUT V20 TEXT 50 100 46 10 FOLLI018ULL
# 2  
Old 04-22-2009
I don't see the problem to use a temporary file but you can embed the mv command in your awk command in the END block:

Code:
awk 'NR==FNR{a[NR]=$1;b[NR]=$2;c[NR]=$3;next;}
{$2=a[FNR];$4=b[FNR];$5=c[FNR];print}
END{system("mv temp file1")}' file2 file1 > temp

Regards
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Mapping the values of ids of two columns of file1 from file2

I have of two space separated files: ==> File1 <== PT|np_496075.1 st|K92748.1 st|K89648.1 PT|np_001300561.1 PT|np_497284.1 st|K90752.1 st|K90279.1 PT|np_740775.1 PT|np_497749.1 st|K90752.1 st|K92038.1 PT|np_490856.1 PT|np_497284.1 st|K90752.1 st|K88095.1 PT|np_494764.1 ==> File 2 <==... (2 Replies)
Discussion started by: sammy777888
2 Replies

2. Shell Programming and Scripting

awk to search field2 in file2 using range of fields file1 and using match to another field in file1

I am trying to use awk to find all the $2 values in file2 which is ~30MB and tab-delimited, that are between $2 and $3 in file1 which is ~2GB and tab-delimited. I have just found out that I need to use $1 and $2 and $3 from file1 and $1 and $2of file2 must match $1 of file1 and be in the range... (6 Replies)
Discussion started by: cmccabe
6 Replies

3. UNIX for Dummies Questions & Answers

Compare file1 and file2, print matching lines in same order as file1

I want to print only the lines in file2 that match file1, in the same order as they appear in file 1 file1 file2 desired output: I'm getting the lines to match awk 'FNR==NR {a++}; FNR!=NR && a' file1 file2 but they are in sorted order, which is not what I want: Can anyone... (4 Replies)
Discussion started by: pathunkathunk
4 Replies

4. UNIX for Dummies Questions & Answers

if matching strings in file1 and file2, add column from file1 to file2

I have very limited coding skills but I'm wondering if someone could help me with this. There are many threads about matching strings in two files, but I have no idea how to add a column from one file to another based on a matching string. I'm looking to match column1 in file1 to the number... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

5. Shell Programming and Scripting

search from file1 and replace into file2

I have 2 files: file1.txt: 1|15|XXXXXX||9630716||0096000||30/04/2012|E|O|X||||20120525135617-30.04.2012|PAT66OLM|STA||||00001|STA_0096000_YYYPPPXTMEX00_20120525135617_02_P.pdf|... (2 Replies)
Discussion started by: pparthiv
2 Replies

6. Shell Programming and Scripting

Get values from different columns from file2 when match values of file1

Hi everyone, I have file1 and file2 comma separated both. file1 is: Header1,Header2,Header3,Header4,Header5,Header6,Header7,Header8,Header9,Header10 Code7,,,,,,,,, Code5,,,,,,,,, Code3,,,,,,,,, Code9,,,,,,,,, Code2,,,,,,,,,file2... (17 Replies)
Discussion started by: cgkmal
17 Replies

7. Shell Programming and Scripting

Read Field from file1 and find and replace in file2

Hi All, I have file1 line below: $myName$|xxx Now I need to read the file1 and find for $myName$ in file2 and replace with xxx file1: $myName$|xxx file2: My name is $myName$ expected output in file2 after executing the script is below: my name is xxx Thanks, (8 Replies)
Discussion started by: gdevadas
8 Replies

8. Shell Programming and Scripting

Search & replace fields from file1 to file2

hi, I have two xml files with the name source.xml and tobe_replaced.xml. Sample data: source.xml contains: <?xml version="1.0"?> <product description="prod1" product_info="some/info"> <product description="prod2" product_info="xyz/allinfo"> <product description="abc/partialinfo"... (2 Replies)
Discussion started by: dragon.1431
2 Replies

9. UNIX for Dummies Questions & Answers

Extracting 482/300k columns no's with respective info. listed in file2 from file1

Hi, I have 2 files File 1: 1 2 3 4 5 6 .......etc until column 300K 1 23 21 24 12 22 1 23 21 24 12 22 1 23 21 24 12 22 1 23 21 24 12 22 1 23 21 24 12 22 1 23 21 24 12 22 1 23 21 24 12 22 . . etc until row 1411 File 2: (14 Replies)
Discussion started by: sogi
14 Replies

10. Shell Programming and Scripting

awk/sed search lines in file1 matching columns in file2

Hi All, as you can see I'm pretty new to this board. :D I'm struggling around with small script to search a few fields in another file. Basically I have file1 looking like this: 15:38:28 sz:10001 pr:14.16 15:38:28 sz:10002 pr:18.41 15:38:29 sz:10003 pr:19.28 15:38:30 sz:10004... (1 Reply)
Discussion started by: floripoint
1 Replies
Login or Register to Ask a Question