Join multiple lines from text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Join multiple lines from text file
# 1  
Old 09-05-2016
Join multiple lines from text file

Hi Guys,

Could you please advise how to join multiple details lines into single row, with HEADER 1 as the record separator and comma(,) as the field separator.

Input:
Code:
HEADER 1, HEADER 2, HEADER 3,
11,22,33,

COLUMN1,COLUMN2,COLUMN3,

AA1,  BB1,  CC1,
END: ABC

HEADER 1, HEADER 2, HEADER 3,
44,55,66,

COLUMN1,COLUMN2,COLUMN3,

AA2,  BB2,  CC2,
AA3,  BB3,  CC3,
END: XYZ

Desired Output:
Code:
HEADER 1, HEADER 2,HEADER 3,COLUMN1,COLUMN2,COLUMN3,
11,22,33,AA1,BB1,CC1,
44,55,66,AA2,BB2,CC2,
44,55,66,AA3,BB3,CC3,

So far am figuring out with below:
Code:
awk -v RS="HEADER 1" '{ $1="HEADER 1 " $1 } NF>2 ' file.txt


Many thanks.
# 2  
Old 09-05-2016
Try
Code:
awk '/HEADER 1/ {HD = $0; getline INTRO; next} /COLUMN1/ {if (!L) print HD, $0; L = 1; next} NF && !/END/ {print INTRO, $0}' FS=, OFS="" file
HEADER 1, HEADER 2, HEADER 3,COLUMN1,COLUMN2,COLUMN3,
11,22,33,AA1,  BB1,  CC1,
44,55,66,AA2,  BB2,  CC2,
44,55,66,AA3,  BB3,  CC3,

As you're not too consistent with the spaces, I didn't do any attempts to meet your output in that respect.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 09-05-2016
Hello budz26,

Could you please try following and let me know if this helps.
Code:
awk 'BEGIN{print "HEADER 1, HEADER 2,HEADER 3,COLUMN1,COLUMN2,COLUMN3"} ($0 ~ /HEADER/ || $0 ~ /COLUMN/ || $0 ~ /^$/ || $0 ~ /^END/){next} ($0 ~ /^[0-9]/){P=$0;next} {$0= P $0;gsub(/, +/,",",$0);print}'   Input_file

Output will be as follows.
Code:
HEADER 1, HEADER 2,HEADER 3,COLUMN1,COLUMN2,COLUMN3
11,22,33,AA1,BB1,CC1,
44,55,66,AA2,BB2,CC2,
44,55,66,AA3,BB3,CC3

NOTE: Considering that your Input_file is same style as you have shown to us.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 4  
Old 09-05-2016
Great, many thanks RudiC! Smilie

---------- Post updated at 12:38 AM ---------- Previous update was at 12:34 AM ----------

It works, thanks R. Singh!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Join columns across multiple lines in a Text based on common column using BASH

Hello, I have a file with 2 columns ( tableName , ColumnName) delimited by a Pipe like below . File is sorted by ColumnName. Table1|Column1 Table2|Column1 Table5|Column1 Table3|Column2 Table2|Column2 Table4|Column3 Table2|Column3 Table2|Column4 Table5|Column4 Table2|Column5 From... (6 Replies)
Discussion started by: nv186000
6 Replies

2. Shell Programming and Scripting

Join common patterns in multiple lines into one line

Hi I have a file like 1 2 1 2 3 1 5 6 11 12 10 2 7 5 17 12 I would like to have an output as 1 2 3 5 6 10 7 11 12 17 any help would be highly appreciated Thanks (4 Replies)
Discussion started by: Harrisham
4 Replies

3. Shell Programming and Scripting

Join multiple lines

Hi I have a source file ( written i C ) where a funtion call is spread over multiple lines, for example : func( a, b, c ); I want this to be joined into one single line : func(a,b,c); How can this be done with awk and sed ? Regards. Hench (2 Replies)
Discussion started by: hench
2 Replies

4. UNIX for Dummies Questions & Answers

How to grep multiple lines from a text file using another text file?

I would like to use grep to select multiple lines from a text file using a single-column text file. Basically I want to only select lines from the first text file where the second column of the first text file matches the second text file. How do I go about doing that? Thanks! (5 Replies)
Discussion started by: evelibertine
5 Replies

5. Shell Programming and Scripting

Process multiple lines in a text file

Hi All I have text file like this: a=21ej c=3tiu32 e=hydkehw f=hgdiuw g=jhdkj a=klkjhvl b=dlkjhyfd a=yo c=8732 Any way I can process data from first a to just before of second a, and then second a to just before of 3rd one. Just fetching records like that will help, I mean... (3 Replies)
Discussion started by: sandipjee
3 Replies

6. Shell Programming and Scripting

How to get awk to edit in place and join all lines in text file

Hi, I lack the utter fundamentals on how to craft an awk script. I have hundreds of text files that were mangled by .doc format so all the lines are broken up so I need to join all of the lines of text into a single line. Normally I use vim command "ggVGJ" to join all lines but with so many... (3 Replies)
Discussion started by: n00ti
3 Replies

7. Shell Programming and Scripting

[bash help]Adding multiple lines of text into a specific spot into a text file

I am attempting to insert multiple lines of text into a specific place in a text file based on the lines above or below it. For example, Here is a portion of a zone file. IN NS ns1.domain.tld. IN NS ns2.domain.tld. IN ... (2 Replies)
Discussion started by: cdn_humbucker
2 Replies

8. Shell Programming and Scripting

How to use SED to join multiple lines?

Hi guys, anyone know how can i join multiples lines using sed till the end of a file and output to another file in a single line? The end of each line will be replaced with a special char "#". I am using the below SED command, however it seems to remove the last 2 lines. Also not all lines... (12 Replies)
Discussion started by: DrivesMeCrazy
12 Replies

9. Shell Programming and Scripting

join on a file with multiple lines, fields

I've looked at the join command which is able to perform what I need on two rows with a common field, however if I have more than two rows I need to join all of them. Thus I have one file with multiple rows to be joined on an index number: 1 randomtext1 2 rtext2 2 rtext3 3 rtext4 3 rtext5... (5 Replies)
Discussion started by: crimper
5 Replies

10. Shell Programming and Scripting

Awk Join multiple lines

Hi, I have data with broken lines: Sample data: "12"|"25"|"a"|"b"|"c"|"d"|"e"|"f"|"2453748"|"08:10:50" "16"|"25"|"a"|"b"|"c"|"d"|"e"|"f"|" 2453748"|"08:15:50" "16"|"25"|"a"|"b"|" c"|"d"|"e"|"f"|"2453748"|"08:19:50" "16"|"25"|"a"|"b"|"c"|"d"|"e"|"f"|"2453748"|"08:19:50" In the... (5 Replies)
Discussion started by: hitmansilentass
5 Replies
Login or Register to Ask a Question