How to append portion of a file content to another file when a certain pattern is matching?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to append portion of a file content to another file when a certain pattern is matching?
# 1  
Old 06-13-2013
How to append portion of a file content to another file when a certain pattern is matching?

Hi ladies and gentleman.. I have two text file with me. I need to replace one of the file content to another file if one both files have a matching pattern.

Example:
text1.txt:
Code:
ABCD 1234567,HELLO_WORLDA,HELLO_WORLDB
DCBA 3456789,HELLO_WORLDE,HELLO_WORLDF

text2.txt:
Code:
XXXX,ABCD 1234567,WORLD_HELLOA,WORLD_HELLOB
XXXX,DCBA 3456789,WORLD_HELLOA,WORLD_HELLOB

output.txt:
Code:
XXXX,ABCD 1234567,HELLO_WORLDA,HELLO_WORLDB
XXXX,DCBA 3456789,HELLO_WORLDE,HELLO_WORLDF

Any help will be greatly appreciated Smilie

Last edited by Scott; 06-13-2013 at 05:47 AM.. Reason: Please use code tags for code and data
# 2  
Old 06-13-2013
Code:
awk -F',' 'NR==FNR {a[$1]=$2","$3; next} a[$2] {print $1","$2","a[$2]}' text1.txt text2.txt


Last edited by balajesuri; 06-13-2013 at 03:35 AM..
This User Gave Thanks to balajesuri For This Post:
# 3  
Old 06-13-2013
Thanks !

any idea why it is causing a syntax error?
# 4  
Old 06-13-2013
Code:
awk -F, 'NR==FNR{A[$1]=$0;next}A[$2]{print $1 FS A[$2]}' file1 file2

# 5  
Old 06-13-2013
Thanks pamu!

But same as the previous reply it has syntax error. Sorry.. I'm kinda new with shell scripting. Any guidance will be appreciated Smilie
# 6  
Old 06-13-2013
have you tried with nawk ?
This User Gave Thanks to pamu For This Post:
# 7  
Old 06-13-2013
Nice ! I got what i wanted. Thank pamu and balajesuri. You guys are good ! Smilie

---------- Post updated at 02:13 AM ---------- Previous update was at 01:57 AM ----------

Pamu, do you mind to do some explanation on the code ? Thank you Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk to update file with partial matching line in another file and append text

In the awk below I am trying to cp and paste each matching line in f2 to $3 in f1 if $2 of f1 is in the line in f2 somewhere. There will always be a match (usually more then 1) and my actual data is much larger (several hundreds of lines) in both f1 and f2. When the line in f2 is pasted to $3 in... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

Insert content of file before the first occurrence of a line starts with a pattern in another file

Hi all, I'm new to scripting.. facing some problems while inserting content of a file into another file... I want to insert content of a file (file2) into file1, before first occurrence of "line starts with pattern" in file1 file1 ====== working on linux its unix world working on... (14 Replies)
Discussion started by: Jagadeesh Kumar
14 Replies

3. Shell Programming and Scripting

Big pattern file matching within another pattern file in awk or shell

Hi I need to do a patten match between files . I am new to shell scripting and have come up with this so far. It take 50 seconds to process files of 2mb size . I need to tune this code as file size will be around 50mb and need to save time. Main issue is that I need to search the pattern from... (2 Replies)
Discussion started by: nitin_daharwal
2 Replies

4. Shell Programming and Scripting

Insert content of a file into another file before given pattern

I need to insert file x2 into x1 right before first BBB line. $ cat x1 AAA 1 AAA 2 AAA 3 BBB 1 BBB 2 BBB 3 $ cat x2 XXX - insert 1 XXX - insert 2 I need to get AAA 1 AAA 2 AAA 3 XXX - insert 1 XXX - insert 2 BBB 1 (2 Replies)
Discussion started by: migurus
2 Replies

5. Shell Programming and Scripting

How to remove exisiting file content from a file and have to append new file content?

hi all, i had the below script x=`cat input.txt |wc -1` awk 'NR>1 && NR<'$x' ' input.txt > output.txt by using above script i am able to remove the head and tail part from the input file and able to append the output to the output.txt but if i run it for second time the output is... (2 Replies)
Discussion started by: hemanthsaikumar
2 Replies

6. Shell Programming and Scripting

Append timestamp create .trg file for all content of an unzipped archive

Hi, I have a test.zip archive that contains test.zip --> (file_1.txt, file_2.txt , file_3.txt) I need to unzip the file like this, file_1_timestamp.txt file_1_timestamp.trg file_2_timestamp.txt file_2_timestamp.trg file_3_timestamp.txt file_3_timestamp.trg Could you please let me know... (7 Replies)
Discussion started by: Shandel
7 Replies

7. Shell Programming and Scripting

Help with ksh-to read ip file & append lines to another file based on pattern match

Hi, I need help with this- input.txt : L B white X Y white A B brown M Y black Read this input file and if 3rd column is "white", then add specific lines to another file insert.txt. If 3rd column is brown, add different set of lines to insert.txt, and so on. For example, the given... (6 Replies)
Discussion started by: prashob123
6 Replies

8. Shell Programming and Scripting

Need to append matching strings in a file

Hi , I am writing a shell script to check pvsizes in linux box. # for i in `cat vgs1` > do > echo "########### $i ###########" > pvs|grep -i $i|awk '{print $2,$1,$5}'>pvs_$i > pvs|grep -i $i|awk '{print $1}'|while read a > do > fdisk -l $a|head -2|tail -1|awk '{print $2,$3}'>pvs_$i1 >... (3 Replies)
Discussion started by: nanduri
3 Replies

9. Shell Programming and Scripting

awk - writing matching pattern to a new file and deleting it from the current file

Hello , I have comma delimited file with over 20 fileds that i need to do some validations on. I have to check if certain fields are null and then write the line containing the null field into a new file and then delete the line from the current file. Can someone tell me how i could go... (2 Replies)
Discussion started by: goddevil
2 Replies

10. Shell Programming and Scripting

if file is NOT empty, then append content to file

hi people, i have texts down.txt and down-new.txt and i want to check; - if down-new.txt is NOT empty, then write date and its content to /home/gc_sw/down.txt for example; down.txt:AAAA SSSS down-new.txt:123 456 and after checking down-new.txt is NOT empty, down.txt should... (10 Replies)
Discussion started by: gc_sw
10 Replies
Login or Register to Ask a Question