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?
# 22  
Old 06-17-2013
text1.txt

XXX XXX XXXX:1234,1111_111,ABCD 123456,XXXX XXXXXX:12345,XX,YYYYY1,ZZZZZ1,
XXX XXX XXXX:1234,1111_111,BCDF 123456,XXXX XXXXXX:12345,XX,YYYYY2,ZZZZZ2,
XXX XXX XXXX:1234,1111_111,CDEF 123456,XXXX XXXXXX:12345,XX,YYYYY3,ZZZZZ3,

text2.txt

ABCD 123456,1111,11111,111111,XX,AAAAAA1,BBBBBB1
CDEF 123456,1111,11111,111111,XX,AAAAAA2,BBBBBB2
BCDF 123456,1111,11111,111111,XX,AAAAAA3,BBBBBB3

Output:

XXX XXX XXXX:1234,1111_111,ABCD 123456,XXXX XXXXXX:12345,XX,YYYYY1,ZZZZZ1,
XXX XXX XXXX:1234,1111_111,BCDF 123456,XXXX XXXXXX:12345,XX,YYYYY2,ZZZZZ2,
XXX XXX XXXX:1234,1111_111,CDEF 123456,XXXX XXXXXX:12345,XX,YYYYY3,ZZZZZ3,

This is the original text file input and it gave the desired output. However with the NewTextFile.txt input below the output is somehow "incorrect" with the same text2.txt.


NewTextFile.txt:

XXXXX XXXXX:123E,1234567_123,ABCD 123456,XXXX XXXX:123E,X,YYYYY1,ZZZZZ1,
XXXXX XXXXX:213E,2345678_123,BCDF 123456,XXXX XXXX:123E,X,YYYYY2,ZZZZZ2,
XXXXX XXXXX:321E,3456789_123,CDEF 123456,XXXX XXXX:123E,X,YYYYY3,ZZZZZ3,

text2.txt

ABCD 123456,1111,11111,111111,XX,AAAAAA1,BBBBBB1
CDEF 123456,1111,11111,111111,XX,AAAAAA2,BBBBBB2
BCDF 123456,1111,11111,111111,XX,AAAAAA3,BBBBBB3

Output:
,XXXX XXXXX:123E,1234567_123,ABCD 123456,XXXX XXX:123E,X,YYYYY1,ZZZZZ1,
,XXXX XXXXX:213E,2345678_123,CDEF 123456,XXXX XXX:123E,X,YYYYY2,ZZZZZ2,
,XXXX XXXXX:321E,3456789_123,BCDF 123456,XXXX XXX:123E,X,YYYYY3,ZZZZZ3,

With the NewTextFile.txt, the first character X is removed and replaced with , (comma) for all new line.

Thank you Smilie

Last edited by bananamen; 06-17-2013 at 10:21 PM..
# 23  
Old 06-18-2013
try

Code:
awk -F, 'NR==FNR{A[$1]=$NF;B[$NF]=$(NF-1);C[$NF]=$(NF-2);next}{$(NF-1)=A[$3];$(NF-2)=B[$(NF-1)];$(NF-3)=C[$(NF-1)]}1' OFS="," file2 file1

# 24  
Old 06-18-2013
Hi pamu, the output remains the same. With the first X being replaced by a comma.

Another question, awk give me this message when the input file don't have the similar pattern. is there anyway for it to do it in a more "gracious" way ?
# 25  
Old 06-18-2013
Quote:
Originally Posted by bananamen
Hi pamu, the output remains the same. With the first X being replaced by a comma.
In my latest code I have not used substitute function. so there is no chance of X being replaced by ,

Please show us what you tried.

Quote:
Originally Posted by bananamen
Another question, awk give me this message when the input file don't have the similar pattern. is there anyway for it to do it in a more "gracious" way ?
It is very difficult to program any script when you are not aware of it's all in and out.
Yes it can be done but only way is programmer should know all possible scenarios.

Regards,

pamu
This User Gave Thanks to pamu For This Post:
# 26  
Old 06-23-2013
Thanks pamu for your help Smilie

Actually there is no problem with your code. Just formatting of the input file giving me the error 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