Combining multiple block of lines in one comma separated line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Combining multiple block of lines in one comma separated line
# 1  
Old 07-09-2014
Combining multiple block of lines in one comma separated line

Hi Everyone,

On my Linux box I have a text file having block of few lines and this block lines separated by one blank line. I would like to format and print these lines in such a way that this entire block of lines will come as single comma separated line & again next block of lines in next single line and so on.

Following is the pattern of data in text file,

Code:
Name:		abc
CName:		xyz
Three:		def
Event:		Off
Five:		server1
Six:		2014-06-04 03:02:05.353
Number:		123456789
Priority:	High
Nine:		9
Data:		abc def ghi jkl mno pqr stv
Count:		9

Name:		tmp
CName:		bar
Three:		foo
Event:		On
Five:		server2
Six:		2014-06-04 05:10:05.353
Number:		123456789
Priority:	Low
Nine:		9
Data:		abc def ghi jkl mno pqr stv
Count:		9

And after formatting/parsing output look like below,

Code:
2014-06-04 03:02:05.353,	Name:abc,CName:xyz,Three:def,Event:Off,Five:server1,Number:123456789,Priority:High,Data:abc def ghi jkl mno pqr stv,Count:9
2014-06-04 05:10:05.353,	Name:tmp,CName:bar,Three:foo,Event:On,Five:server2,Number:123456789,Priority:Low,Data:abc def ghi jkl mno pqr stv,Count:9

I really need your valuable inputs to get this ....

Thanks in advance.
# 2  
Old 07-09-2014
An awk approach:
Code:
awk '
        NF {
                if ( $0 ~ /Six/ )
                {
                        H = $0
                        sub(/Six:[ \t]*/, X, H)
                }
                else
                {
                        gsub("\t", X, $0)
                        S = S ? S OFS $0 : $0
                }
                next
        }
        !NF {
                print H, S
                S = ""
        }
        END {
                print H, S
        }
' OFS=, file

# 3  
Old 07-09-2014
Hi Yoda, thanks for your quick reply. I would like to know if if i can run this code as one liner ? If yes how to do that . Tks
# 4  
Old 07-09-2014
Quote:
Originally Posted by gr8_usk
Hi Yoda, thanks for your quick reply. I would like to know if if i can run this code as one liner ? If yes how to do that . Tks
Just put the code in one line!
Code:
awk 'NF{if($0~/Six/){H=$0;sub(/Six:[ \t]*/,X,H)}else{gsub("\t",X,$0);S=S?S OFS $0:$0}next}!NF{print H,S;S=""}END{print H,S}' OFS=, file

This User Gave Thanks to Yoda For This Post:
# 5  
Old 07-09-2014
Try
Code:
awk '{gsub("[\t ]*","");$1=$6"\t"$1; sub (/^[^:]*:/,"",$1); $6=""}1'  RS= ORS="\n" FS="\n" OFS="," file

This User Gave Thanks to RudiC For This Post:
# 6  
Old 07-10-2014
Hi Yoda & RudiC, thank you very much for help both the solutions worked for me.
# 7  
Old 08-29-2014
Hello,

One more approach for same.

Code:
awk -F":" -vs1="," -vpun=",\t" '/Six:/ {s=1;match($0,/[0-9][0-9][0-9][0-9]\-.*/);val=substr($0,RSTART,RLENGTH)} !/Six:/ {s=0} !s{gsub(/^[[:space:]]+/,X,$2);a=a?a s1 $1 OFS $2:$1 OFS $2} /Count:/ {p=1} {if(p){gsub(/^\:\,/,X,a);print val pun a;s=0;p=0;a=""}}' OFS=":" filename

Output will be as follows.

Code:
2014-06-04 03:02:05.353,        Name:abc,CName:xyz,Three:def,Event:Off,Five:server1,Number:123456789,Priority:High,Nine:9,Data:abc def ghi jkl mno pqr stv,Count:9
2014-06-04 05:10:05.353,        Name:tmp,CName:bar,Three:foo,Event:On,Five:server2,Number:123456789,Priority:Low,Nine:9,Data:abc def ghi jkl mno pqr stv,Count:9

Thanks,
R. Singh
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

AIX put comma separated data on its own line

In Linux you can do this to put comma separated data on its own line like this. sed 's/ */&\n/g' /tmp/ports sed 's/ */\n/g' /tmp/ports How do you do this in AIX? It is not working. Is there another way to do this? Something like this. 1, 2, 3, 4 To look like this. 1 2 3 4 (4 Replies)
Discussion started by: cokedude
4 Replies

2. Shell Programming and Scripting

Comma separated values to individual lines

My OS : RHEL 6.7 I have a text file with comma separated values like below $ cat testString.txt 'JOHN' , 'KEITH' , 'NEWMAN' , 'URSULA' , 'ARIANNA' , 'CHENG', . . . . I want these values to appear like below 'JOHN' , 'KEITH' , 'NEWMAN' , 'URSULA' , 'ARIANNA' , 'CHENG', .... (4 Replies)
Discussion started by: kraljic
4 Replies

3. Shell Programming and Scripting

awk to change comma separated line to horizontal

I am trying to change a file that looks like this: file, announcement,date, server, server01, server02, server06, file04, rec01, rec04, rec03... etc into a vertical file like this: file announcement date server server01 server02 server06 The file does not have to be sorted... (5 Replies)
Discussion started by: newbie2010
5 Replies

4. Shell Programming and Scripting

Make multiple lines into single quoted comma separated Linux

Hi, I want to change a file file1.txt: 1234 3456 2345 6789 3456 2333 4444 As, file2.txt in Linux: '1234','3456','2345','6789','3456','2333','4444' Could someone please help me. (Single liner sed, awk will be welcome!) (7 Replies)
Discussion started by: wiweq05
7 Replies

5. Shell Programming and Scripting

Reading Words separated by comma in line

Hi All, I am facing issue, to read words in line, line as follow and i want to read word at each comma 1,you,are,two So i want read like 1 you are two Thanks (1 Reply)
Discussion started by: sujit_kashyap
1 Replies

6. Shell Programming and Scripting

Assigning Multiple Comma Separated IP's To A Bash Array

I am in the process of creating a BASH shell scripts for a project at work. So the scenario is as such: I have a file with each line entry separated by ':' ... (3 Replies)
Discussion started by: metallica1973
3 Replies

7. Programming

PERL:Combining multiple lines to single line

Hi All I need a small help for the below format in making a small script in Perl or Shell. I have a file in which a single line entries are broken into three line entries. Eg: I have a pen and notebook. All i want is to capture in a single line in a separate file. eg: I have a pen and... (4 Replies)
Discussion started by: Kalaiela
4 Replies

8. UNIX for Dummies Questions & Answers

sort comma separated lines by specific columns

Hello, I have a file which lines' words are comma separated: aa, bb, cc, uu b, ee, ff bb, cc, zz, ee, ss, kk oo, bb, hh, uu a, xx, ww tt, aa, dd, yy aa, gg I want to sort first by second column and in case of tie by fourth column with sort command. So the output would be: ... (4 Replies)
Discussion started by: asanchez
4 Replies

9. Shell Programming and Scripting

printing sequence of line no. with comma separated

Kindly i want to concatenate every 12 lines ina file, using a comma separator between fields (each line)? can anyone help please? thanks a lot in advance. (5 Replies)
Discussion started by: m_wassal
5 Replies

10. Shell Programming and Scripting

Deleting a column in multiple files that are comma separated

Hi, I have a directory that contains say 100 files named sequencially like input_1.25_50_C1.txt input_1.25_50_C2.txt input_1.25_50_C3.txt input_1.25_50_C4.txt .. .. .. input_1.25_50_C100.txt an example of the content in each of the file is: "NAME" "MEM.SHIP" "cgd1_10" "cgd1_10"... (9 Replies)
Discussion started by: Lucky Ali
9 Replies
Login or Register to Ask a Question