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?
# 8  
Old 06-13-2013
Quote:
Originally Posted by bananamen
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

Code:
awk -F, 'NR==FNR{     #Reading file1

A[$1]=$0;next}             #Assign $0 to array A with index $1. Using next it skips the next code.

A[$2]            # Check If the A[$2] is present or not. Here we are reading file2

{print $1 FS A[$2]}'       # print $1 and A[$2](values from file1) with Field separator.

Hope this helps you.

Regards,
pamu
This User Gave Thanks to pamu For This Post:
# 9  
Old 06-13-2013
text1.txt
Code:
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
Code:
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:
Code:
XXX XXX XXXX:1234,1111_111,ABCD 123456,XXXX XXXXXX:12345,XX,AAAAAA1,BBBBBB1
XXX XXX XXXX:1234,1111_111,BCDF 123456,XXXX XXXXXX:12345,XX,AAAAAA2,BBBBBB3
XXX XXX XXXX:1234,1111_111,CDEF 123456,XXXX XXXXXX:12345,XX,AAAAAA3,BBBBBB2

With some edit on the code above i still cant get the result i wanted. Any one know how do i solve this ? Thank you.

Last edited by Scott; 06-13-2013 at 05:47 AM.. Reason: Code tags
# 10  
Old 06-13-2013
There is huge difference between your previous input file and current input file.
Try
Code:
awk -F, 'NR==FNR{A[$1]=$NF;B[$NF]=$(NF-1);next}{$(NF-1)=A[$3];$(NF-2)=B[$(NF-1)]}1' OFS="," file2 file1


Last edited by pamu; 06-13-2013 at 06:03 AM.. Reason: corrected...
# 11  
Old 06-13-2013
It gave me the wrong output.
output:
Code:
XXX XXX XXXX:1234,,,XXXX XXXXXX:12345
XXX XXX XXXX:1234,,,XXXX XXXXXX:12345
XXX XXX XXXX:1234,,,XXXX XXXXXX:12345

Moderator's Comments:
Mod Comment Please use code tags
# 12  
Old 06-13-2013
Quote:
Originally Posted by bananamen
It gave me the wrong output.
output:
Code:
XXX XXX XXXX:1234,,,XXXX XXXXXX:12345
XXX XXX XXXX:1234,,,XXXX XXXXXX:12345
XXX XXX XXXX:1234,,,XXXX XXXXXX:12345

Please check my previous post.
I have corrected the code.
This User Gave Thanks to pamu For This Post:
# 13  
Old 06-13-2013
Code:
XXX XXX XXXX:1234,1111_111,ABCD 123456,XXXX XXXXXX:12345,AAAAAA1,BBBBBB1
XXX XXX XXXX:1234,1111_111,BCDF 123456,XXXX XXXXXX:12345,AAAAAA2,BBBBBB3
XXX XXX XXXX:1234,1111_111,CDEF 123456,XXXX XXXXXX:12345,AAAAAA3,BBBBBB2

Thank pamu! It solved my problem ! What if i want to remove the ",XX" from the output ? May i check what do i have to do to obtain the above output?
# 14  
Old 06-13-2013
Quote:
Originally Posted by bananamen
Code:
XXX XXX XXXX:1234,1111_111,ABCD 123456,XXXX XXXXXX:12345,AAAAAA1,BBBBBB1
XXX XXX XXXX:1234,1111_111,BCDF 123456,XXXX XXXXXX:12345,AAAAAA2,BBBBBB3
XXX XXX XXXX:1234,1111_111,CDEF 123456,XXXX XXXXXX:12345,AAAAAA3,BBBBBB2

Thank pamu! It solved my problem ! What if i want to remove the ",XX" from the output ? May i check what do i have to do to obtain the above output?
do you want to remove only XX or all X.
Please provide me your desired output.

You can use awk's inbuilt sub functionality to remove X.

Regards,
pamu

Last edited by pamu; 06-13-2013 at 07:47 AM..
 
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