How to merge lines based off of text?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to merge lines based off of text?
# 1  
Old 11-23-2011
How to merge lines based off of text?

Hello Everyone,
I have two files, similar to the following:

File 1:
Code:
8010  ITEM01  CODE1 FLAG1
filler
filler
7020  OBJECT  CODE2 FLAG2
filler
6010  THING1  CODE4 FLAG4
6011  ITEM20  CODE7 FLAG7

File 2 contains:
Code:
6020  ITEM01  CODEA FLAGA
filler
filler
filler
7000  OBJECT  CODEB FLAGB
filler
filler
4020  THING1  CODEC FLAGC
2211  ITEM20  CODED FLAGD

I want to compare file 1 and two by the keyword in column two (in this example, the items in column two are ITEM01, OBJECT, THING1, and ITEM20). If the keyword if found in both files, then the CODE/FLAG from file2 will overwrite the code/flag found in file1 like so:

Code:
8010  ITEM01  CODEA FLAGA
filler
filler
7020  OBJECT  CODEB FLAGB
filler
6010  THING1  CODEC FLAGC
6011  ITEM20  CODED FLAGD

I tried using the "cut -b 7-12" command, since I know that the keyword is found in byte positions 7-12 and tried comparing what i extracted between both files, but that doesn't seem to work. I've also tried cutting the files so that in one file, I have the keywords and the other file, I have the code/flag. I then tried joining the files. This didn't work since i have filler text in between the lines of the keywords. Any suggestions?
# 2  
Old 11-23-2011
Code:
# awk 'BEGIN { while(getline<"file2") f2[$2]=$3 " " $4 }; ($2 in f2) { print $1" "$2" "f2[$2] }' file1
8010 ITEM01 CODEA FLAGA
filler
filler
7020 OBJECT CODEB FLAGB
filler
6010 THING1 CODEC FLAGC
6011 ITEM20 CODED FLAGD

# 3  
Old 11-23-2011
Quote:
Originally Posted by MR.bean
Code:
# awk 'BEGIN { while(getline<"file2") f2[$2]=$3 " " $4 }; ($2 in f2) { print $1" "$2" "f2[$2] }' file1
8010 ITEM01 CODEA FLAGA
filler
filler
7020 OBJECT CODEB FLAGB
filler
6010 THING1 CODEC FLAGC
6011 ITEM20 CODED FLAGD

Thanks mr.bean, but what do you do when the keywords in column two contains spaces and filler has multiple entries? Like if there was a "ITEM 1" in the following? Is there a way to go by byte position?

Code:
8010 ITEM01 CODEA FLAGA
8011 ITEM 1 CODEE FLAGE
filler filler
filler
7020 OBJECT CODEB FLAGB
filler
6010 THING1 CODEC FLAGC
6011 ITEM20 CODED FLAGD

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Merge lines based on match

I am trying to merge two lines to one based on some matching condition. The file is as follows: Matches filter: 'request ', timestamp, <HTTPFlow request=<GET: Matches filter: 'request ', timestamp, <HTTPFlow request=<GET: Matches filter: 'request ', timestamp, <HTTPFlow ... (8 Replies)
Discussion started by: jamie_123
8 Replies

2. Shell Programming and Scripting

Merge mutiple lines into one based on if the first word is some particular value

Hi, trying to knock something together to create one line entries based on whether the first word on each line matches a particular value. eg. Link,"Name=""Something\something"","Timeout=""1800""", "Target=""\\thing\thing\thing""","State=""ONLINE""",something,... (0 Replies)
Discussion started by: adamdb
0 Replies

3. Shell Programming and Scripting

E-mail Output Merge Lines for Some Text..

Hi all. This is the content of the text file used for the e-mail: TM ICP-EDW BILLING REGISTER USAGE BREAKDOWN_01062014.csv TM_ICP_EDWH_FICL_13062014.TXT TM_ICP_EDWH_FICL_16062014.TXT TM_ICP_EDW_Detailed Payment Journal Report_13062014.txt TM_ICP_EDW_Detailed Payment Journal... (9 Replies)
Discussion started by: aimy
9 Replies

4. Shell Programming and Scripting

Need to merge lines based on pattern

Hi, I have a requirement to merge multiple lines based on search pattern. The search criteria is : it will search for CONSTRAINT and when it found CONSTRAINT, it will merge all lines to 1 line till it founds blank line. For Example: CREATE TABLE "AMS_DISTRIBUTOR_XREF" ( "SOURCE"... (5 Replies)
Discussion started by: satyaatcgi
5 Replies

5. Shell Programming and Scripting

Merge file lines based off of keyword

Hello Everyone, I have two files I created in a format similar to the ones found below (character position is important): File 1: 21 Cat Y N S Y Y N N FOUR LEGS TAIL WHISKERS 30 Dog N N 1 Y Y N N FOUR LEGS TAIL 33 Fish Y N 1 Y Y N N FINS 43 CAR Y N S Y Y N N WHEELS DOORS... (7 Replies)
Discussion started by: jl487
7 Replies

6. Shell Programming and Scripting

Merge two non-consecutive lines based on line number or string

This is a variation of an earlier post found here: unixcom/shell-programming-scripting/159821-merge-two-non-consecutive-lines.html User Bartus11 was kind enough to solve that example. Previously, I needed help combining two lines that are non-consecutive in a file. Now I need to do the... (7 Replies)
Discussion started by: munkee
7 Replies

7. Shell Programming and Scripting

Gawk / Awk Merge Lines based on Key

Hi Guys, After windows died on my netbook I installed Lubuntu and discovered Gawk about a month ago. After using Excel for 10+ years I'm amazed how quick and easily Gawk can process data but I'm stuck with a little problem merging data from multiple lines. I'm an SEO Consultant and provide... (9 Replies)
Discussion started by: Jamesfirst
9 Replies

8. Shell Programming and Scripting

Merge lines in text file based on pattern

Hello, I have searched forum trying to find a solution to my problem, but could not find anything or I did not understand the examples.... I should say, I am very inexperienced with text processing. I have a text file with approx 60k lines in it. I need to merge lines based on the number... (8 Replies)
Discussion started by: Bertik
8 Replies

9. Shell Programming and Scripting

merge lines into single line based on symbol \t

The symbols are \t and \t\t (note: not tab) If the line starts with \t merge them into a single line upto symbol \t\t \t\t to end and start new line I able to join in a single line but not ending at \t\t and I completely confused help would be appreciated:b::D Input \ta tab XXXXXXXXXX \te... (5 Replies)
Discussion started by: repinementer
5 Replies

10. Shell Programming and Scripting

Merge lines in Flat file based on first 5 characters

Hi I have the fixed width flat file having the following data 12345aaaaaaaaaabbbbbbbbbb 12365sssssssssscccccccccc 12365sssss 12367ddddddddddvvvvvvvvvv 12367 vvvvv Here the first column is length 5 second is length 10 third is length 10 if the second or third column exceeds... (3 Replies)
Discussion started by: Brado
3 Replies
Login or Register to Ask a Question