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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Merge mutiple lines into one based on if the first word is some particular value
# 1  
Old 04-02-2015
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.

Code:
Link,"Name=""Something\something"","Timeout=""1800""",
"Target=""\\thing\thing\thing""","State=""ONLINE""",something,
"Target=""\\thing\thing\thing""","State=""OFFLINE""",otherthing,
Link,"Name=""Something\somethingo""","Timeout=""300""",
"Target=""\\\thing\thing\thing""","State=""ONLINE""",otherthing,
"Target=""\\\\thing\thing\thing""","State=""OFFLINE""",otherthing,
Link,"Name=""something\something""","Timeout=""1800""",
"Target=""\\thing\thing\thing""","State=""ONLINE""",otherthing,
"Target=""\\thing\thing\thing""","State=""OFFLINE""",otherthing,

is some example data.

I want to have a single line of data starting from each entry beginning 'Link'
and adding entries from the following lines until we reach the next line containing 'Link' when I want to start a new line and repeat the process.
The number of lines containing 'Target' can vary.

I guess something in awk would be the preferred but i'll try anything.

thanks
Ad

---------- Post updated at 04:32 AM ---------- Previous update was at 04:13 AM ----------

Solved:

Code:
awk '/Link/{if (x)print x;x="";}{x=(!x)?$0:x","$0;}END{print x;}' file.txt


Last edited by zaxxon; 04-02-2015 at 08:26 AM.. Reason: tags; zaxxon: more tags ;)
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 lines till a particular word

Hi Experts, I have a requirement like, I have to search between 2 words (<deviceDetails> and </deviceDetails>) and merge all lines in between into 1 line. Example: <deviceDetails><subscriberName>#UNKNOWN#</subscriberName> <customerNumber>#UNKNOWN#</customerNumber>... (5 Replies)
Discussion started by: satyaatcgi
5 Replies

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

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

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