Merge lines based on match


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Merge lines based on match
# 1  
Old 08-19-2015
Merge lines based on match

I am trying to merge two lines to one based on some matching condition.

The file is as follows:

Code:
Matches filter:
'request ', timestamp, <HTTPFlow
 request=<GET:
Matches filter:
'request ', timestamp, <HTTPFlow
 request=<GET:
Matches filter:
'request ', timestamp, <HTTPFlow
 request=<GET:

I want to merge the line ending with <HTTPFlow with the next line request. The expected output is:

Code:
Matches filter:
'request ', timestamp, <HTTPFlow request=<GET:
Matches filter:
'request ', timestamp, <HTTPFlow request=<GET:
Matches filter:
'request ', timestamp, <HTTPFlow request=<GET:

Thanks.
# 2  
Old 08-19-2015
Hello Jamie_123,

Following may help you in same.
Code:
 awk '(NR%2==0){A=$0;getline;print A OFS $0;NR++;next};{print}'  Input_file

Output will be as follows.
Code:
Matches filter:
'request ', timestamp, <HTTPFlow  request=<GET:
Matches filter:
'request ', timestamp, <HTTPFlow  request=<GET:
Matches filter:
'request ', timestamp, <HTTPFlow  request=<GET:

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 08-19-2015
Try also
Code:
awk '/<HTTPFlow$/ {getline X; $0 = $0 X} 1' file

This User Gave Thanks to RudiC For This Post:
# 4  
Old 08-19-2015
Code:
perl -pe 's/(<HTTPFlow)\n/$1 /' jamie.file

---------- Post updated at 09:35 AM ---------- Previous update was at 08:23 AM ----------

Code:
awk '/<HTTPFlow$/ {printf $0;next}1' jamie.file

This User Gave Thanks to Aia For This Post:
# 5  
Old 08-19-2015
Some more:
Code:
awk '!/<HTTPFlow$/{$0=$0 RS} 1' ORS= file

Code:
awk '1;!/<HTTPFlow$/{print RS}' ORS= file

Code:
awk '{ORS=/<HTTPFlow$/?x:RS}1' file


Last edited by Scrutinizer; 08-20-2015 at 01:17 PM.. Reason: Thanks, MadeInGermany, Ravinder. Changed FS to RS
# 6  
Old 08-19-2015
Quote:
Originally Posted by Aia
...
Code:
awk '/<HTTPFlow$/ {printf $0;next}1' jamie.file

printf $0 will do terrible things if $0 has % signs!
Correct is printf "%s",$0
--
A sed solution:
Code:
sed '/<HTTPFlow$/ {$!N; s/\n//;}' file

# 7  
Old 08-20-2015
Quote:
Originally Posted by Scrutinizer
Some more:
Code:
awk '!/<HTTPFlow$/{$0=$0 RS} 1' ORS= file

Code:
awk '1;!/<HTTPFlow$/{print RS}' ORS= file

Code:
awk '{ORS=/<HTTPFlow$/?x:FS}1' file

Hello Scrutinizer,

For code:
Code:
 awk '{ORS=/<HTTPFlow$/?x:FS}1' Input_file

It will give output as follows.
Code:
 Matches filter: 'request ', timestamp, <HTTPFlow request=<GET: Matches filter: 'request ', timestamp, <HTTPFlow request=<GET: Matches filter: 'request ', timestamp, <HTTPFlow request=<GET:

We can change a bit to it to as follows:
Code:
 awk '{ORS=/<HTTPFlow$/?FS:"\n"}1' Input_file

Output will be as follows then.
Code:
Matches filter:
'request ', timestamp, <HTTPFlow  request=<GET:
Matches filter:
'request ', timestamp, <HTTPFlow  request=<GET:
Matches filter:
'request ', timestamp, <HTTPFlow  request=<GET:

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. Shell Programming and Scripting

How to merge lines based off of text?

Hello Everyone, I have two files, similar to the following: File 1: 8010 ITEM01 CODE1 FLAG1 filler filler 7020 OBJECT CODE2 FLAG2 filler 6010 THING1 CODE4 FLAG4 6011 ITEM20 CODE7 FLAG7 File 2 contains: 6020 ITEM01 CODEA FLAGA filler filler filler 7000 OBJECT CODEB... (2 Replies)
Discussion started by: jl487
2 Replies

4. 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

5. 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

6. 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

7. 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

8. Shell Programming and Scripting

Merge two file data together based on specific pattern match

My input: File_1: 2000_t g1110.b1 abb.1 2001_t g1111.b1 abb.2 abb.2 g1112.b1 abb.3 2002_t . . File_2: 2000_t Ali england 135 abb.1 Zoe british 150 2001_t Ali england 305 g1111.b1 Lucy russia 126 (6 Replies)
Discussion started by: patrick87
6 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