Awk: Combine multiple lines based on number of fields


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk: Combine multiple lines based on number of fields
# 1  
Old 12-22-2015
Awk: Combine multiple lines based on number of fields

If a file has following kind of data, comma delimited
Code:
1,2,3,4
1
1
1,2,3,4
1,2
2
2,3,4

My required output must have only 4 columns with comma delimited

Code:
1,2,3,4
111,2,3,4
1,222,3,4

I have tried many awk command using ORS="" but couldnt progress
# 2  
Old 12-22-2015
Here is a solution
Code:
awk '{buf=buf $0} (split(buf,a,",")>=4) {print buf; buf=""}' file

or using the auto-split
Code:
awk -F, '{buf=buf $0; c+=NF} (c>=4) {print buf; buf=""; c=0}' file

# 3  
Old 12-22-2015
Or
Code:
awk -F, '{while (NF < 4) {getline X; $0 = $0 X}}1' file
1,2,3,4
111,2,3,4
1,222,3,4

# 4  
Old 12-22-2015
With sed
Code:
sed '
:loop
s/,/,/3
t
$!N
s/\n//
t loop
' file

# 5  
Old 01-01-2016
Hello mdkm,

Following may help you too in same.
1st code:
Code:
awk -F, '{ORS=NF<4?"":"\n";} 1;END{if(NF<4){print "\n"}}' Input_file

2nd code:
Code:
awk -F, 'NR==1&&NF==4{print;next}NF<4{A=A?A $0:$0} NF==4{if(A){print A $0;A=""}} END{if(A){print A}}' OFS=,  Input_file

Thanks,
R. Singh

Last edited by RavinderSingh13; 01-01-2016 at 05:08 AM..
# 6  
Old 01-01-2016
@Ravinder: Both of these methods will fail if the last line contains 4 fields...
# 7  
Old 01-01-2016
Hello Scrutinizer,

When I have following file with following input:
Code:
cat Input_file
2,3,4
1
1
1,2,3,4
1,2
2
2,3,4
5,6,7,8
1
2
3
4
5
6,7,8
9,10,11,12

Following codes give the results.
Code:
 awk -F, '{ORS=NF<4?"":"\n";} 1;END{if(NF<4){print "\n"}}'  Input_file

Output will be as follows.
Code:
 awk -F, 'NR==1&&NF==4{print;next}NF<4{A=A?A $0:$0} NF==4{if(A){print A $0;A=""}} END{if(A){print A}}' OFS=,  Input_file

It gives as same pattern as user requested output, but yes it doesn't give four fields in each output.

EDIT: As Scrutnizer mentioned in his next post#8, above solutions will not work as per OP's given Input_file because they work on same patteren appending the texts but they are not fulfilling the 4 fields conditions, because I have used different Input_file compare to OPs.


Thanks,
R. Singh

Last edited by RavinderSingh13; 01-01-2016 at 10:17 AM.. Reason: Added a comment for solutions, Thanks to S
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to print lines based on text in field and value in two additional fields

In the awk below I am trying to print the entire line, along with the header row, if $2 is SNV or MNV or INDEL. If that condition is met or is true, and $3 is less than or equal to 0.05, then in $7 the sub pattern :GMAF= is found and the value after the = sign is checked. If that value is less than... (0 Replies)
Discussion started by: cmccabe
0 Replies

2. Shell Programming and Scripting

awk to combine lines if fields match in lines

In the awk below, what I am attempting to do is check each line in the tab-delimeted input, which has ~20 lines in it, for a keyword SVTYPE=Fusion. If the keyword is found I am splitting $3 using the . (dot) and reading the portion before and after the dot in an array a. If it does have that... (12 Replies)
Discussion started by: cmccabe
12 Replies

3. Shell Programming and Scripting

awk joining multiple lines based on field count

Hi Folks, I have a file with fields as follows which has last field in multiple lines. I would like to combine a line which has three fields with single field line for as shown in expected output. Please help. INPUT hname01 windows appnamec1eda_p1, ... (5 Replies)
Discussion started by: shunya
5 Replies

4. Shell Programming and Scripting

awk sort based on difference of fields and print all fields

Hi I have a file as below <field1> <field2> <field3> ... <field_num1> <field_num2> Trying to sort based on difference of <field_num1> and <field_num2> in desceding order and print all fields. I tried this and it doesn't sort on the difference field .. Appreciate your help. cat... (9 Replies)
Discussion started by: newstart
9 Replies

5. Shell Programming and Scripting

Combine multiple unique lines from event log text file into one line, use PERL or AWK?

I can't decide if I should use AWK or PERL after pouring over these forums for hours today I decided I'd post something and see if I couldn't get some advice. I've got a text file full of hundreds of events in this format: Record Number : 1 Records in Seq : ... (3 Replies)
Discussion started by: Mayday22
3 Replies

6. Shell Programming and Scripting

awk split lines without knowing the number of fields a-priori

I want to use awk to split fields and put them into a file but I don't know the number of fields for example, in the following line Ports: 22/filtered/tcp//ssh///, 53/open/tcp//tcpwrapped///, 111/filtered/tcp//rpcbind///, 543/filtered/tcp//klogin///, 544/filtered/tcp//kshell///,... (3 Replies)
Discussion started by: esolvepolito
3 Replies

7. Shell Programming and Scripting

Combine multiple lines in file based on specific field

Hi, I have an issue to combine multiple lines of a file. I have records as below. Fields are delimited by TAB. Each lines are ending with a new line char (\n) Input -------- ABC 123456 abcde 987 890456 7890 xyz ght gtuv ABC 5tyin 1234 789 ghty kuio ABC ghty jind 1234 678 ght ... (8 Replies)
Discussion started by: ratheesh2011
8 Replies

8. Shell Programming and Scripting

How to combine two files based on fields?

I have two files which are as follows: File 1: 1 abc 250 2 pqr 300 3 xyz 100 File 2: 1 abc 230 2 pqr 700 3 xyz 500 Now I need output File, File 3as: S.No Name Count1 Count2 1 abc 250 230 2 pqr 300 700 3 xyz 100 500 NOTE: (13 Replies)
Discussion started by: karumudi7
13 Replies

9. Shell Programming and Scripting

AWK Duplicate lines multiple times based on a calculated value

Hi, I'm trying to create an XML sitemap of our dynamic ecommerce sites SEO Friendly URLs and am trying to create the initial page listing. I have a CSV file that looks like the following and need duplicate the lines based on a value which needs calculating. ... (2 Replies)
Discussion started by: jamesfx
2 Replies

10. Shell Programming and Scripting

How to (n)awk lines of CSV with certain number of fields?

I have a CSV file with a variable number of fields per record. How do I print lines of a certain number of fields only? Several permutations of the following (including the use of escape characters) have failed to retrieve the line I'm after (1,2,3,4)... $ cat myfile 1,2,3,4 1,2,3 $ # Print... (1 Reply)
Discussion started by: cs03dmj
1 Replies
Login or Register to Ask a Question