Visit Our UNIX and Linux User Community


awk joining multiple lines based on field count


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk joining multiple lines based on field count
# 1  
Old 08-29-2016
RedHat 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
Code:
hname01			windows	appnamec1eda_p1,                                                            appnamec2eda_p2,
				Vappnamec3eda_p2,
halkat-A		rhel	VPC1E3DEP0,                                                                 VP27C1E4DEP1
vimame-B		vmw	PX0427E3DBF1,
				VPC1E4DAEP1
poxdem-A		windows	VP27C1DE0,
				VP27C1DE0,
				VP27C1DE0,

Expected OUTPUT
Code:
hname01			windows	appnamec1eda_p1,appnamec2eda_p2,Vappnamec3eda_p2,
halkat-A		rhel	VPC1E3DEP0,VP27C1E4DEP1
vimame-B		vmw	PX0427E3DBF1,VPC1E4DAEP1
poxdem-A		windows	VP27C1DE0,VP27C1DE0,VP27C1DE0,

# 2  
Old 08-29-2016
Your expected output is inconsistent as to whether there should be a trailing comma at the of your output lines. Do you want all of the commas in the input to be copied to the output, or do you want to remove the trailing comma?

What operating system are you using?

What shell are you using?
# 3  
Old 08-29-2016
Hi Don,

Thanks for the response. I would like to keep the comma at the end. Appreciate your help.

---------- Post updated at 05:00 PM ---------- Previous update was at 04:59 PM ----------

I am using RHEL 5.0 and I am in bash shell.
# 4  
Old 08-29-2016
Try:
Code:
awk '
BEGIN {	FS = ",[[:space:]]*"
	OFS = ","
}
/^[^[:space:]]/ {
	if(NR > 1)
		print ""
	for(i = 1; i <= NF; i++)
		if($i != "")
			printf("%s%s", $i, OFS)
	next
}
{	sub(/^[[:space:]]*/, "", $1)
	for(i = 1; i <= NF; i++)
		if($i != "")
			printf("%s%s", $i, OFS)
}
END {	print ""
}' INPUT

which, with your sample input, produces the output:
Code:
hname01			windows	appnamec1eda_p1,appnamec2eda_p2,Vappnamec3eda_p2,
halkat-A		rhel	VPC1E3DEP0,VP27C1E4DEP1,
vimame-B		vmw	PX0427E3DBF1,VPC1E4DAEP1,
poxdem-A		windows	VP27C1DE0,VP27C1DE0,VP27C1DE0,

If someone else would like to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 08-30-2016
Try also
Code:
awk '
                {gsub (/[       ]+/, " ")
                 DL = ""
                }
!/^[    ]+/     {sub (/ /, "    ")
                 DL = RS
                }
                {printf "%s%s", DL, $0
                }
END             {printf RS
                }
' file
hname01	windows appnamec1eda_p1, appnamec2eda_p2, Vappnamec3eda_p2,
halkat-A	rhel VPC1E3DEP0, VP27C1E4DEP1
vimame-B	vmw PX0427E3DBF1, VPC1E4DAEP1
poxdem-A	windows VP27C1DE0, VP27C1DE0, VP27C1DE0,

This User Gave Thanks to RudiC For This Post:
# 6  
Old 08-30-2016
Thank you Don and RudiC,

Both of these codes work like charm.

Previous Thread | Next Thread
Test Your Knowledge in Computers #720
Difficulty: Medium
Alan Minsky was an adviser on Stanley Kubrick's movie 2001: A Space Odyssey.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to adjust text and count based on value in field

The below awk executes as is and produces the current output. It isvery close but what Ican not seem to do is add the -exon..., the ... portion comes from $1 and the _exon is static and will never change. If there is + sign in $4 then the ... is in acending order or sequential. If there is a - in... (2 Replies)
Discussion started by: cmccabe
2 Replies

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

3. Shell Programming and Scripting

awk to remove lines where field count is greather than 1 in two fields

I am trying to remove all the lines and spaces where the count in $4 or $5 is greater than 1 (more than 1 letter). The file and the output are tab-delimited. Thank you :). file X 5811530 . G C NLGN4X 17 10544696 . GA G MYH3 9 96439004 . C ... (1 Reply)
Discussion started by: cmccabe
1 Replies

4. Shell Programming and Scripting

awk Parse And Create Multiple Files Based on Field Value

Hello: I am working parsing a large input file which will be broken down into multiples based on the second field in the file, in this case: STORE. The idea is to create each file with the corresponding store number, for example: Report_$STORENUM_$DATETIMESTAMP , and obtaining the... (7 Replies)
Discussion started by: ec012
7 Replies

5. Shell Programming and Scripting

Awk: print lines with one of multiple pattern in the same field (column)

Hi all, I am new to using awk and am quickly discovering what a powerful pattern-recognition tool it is. However, I have what seems like a fairly basic task that I just can't figure out how to perform in one line. I want awk to find and print all the lines in which one of multiple patterns (e.g.... (8 Replies)
Discussion started by: elgo4
8 Replies

6. Shell Programming and Scripting

Joining lines in TXT file based on first character

Hi, I have a pipe delimeted text file where lines have been split over 2 lines and I need to join them back together. For example the file I have is similar to the following: aaa|bbb |ccc ddd|eee fff|ggg |hhh I ideally need to have it looking like the following aaa|bbb|ccc ddd|eee... (5 Replies)
Discussion started by: fuji_s
5 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. Windows & DOS: Issues & Discussions

Gawk on Windows: Joining lines only if 1st field matches

Hi.. i have two files:: file_1:: mOnkey huMAnfile_2:: Human:hates:banana i:like:*** Monkey:loves:banana dogs:kill:catsdesired output:: Monkey:loves:banana Human:hates:bananaso only when the 1st field matches from both files print it from file_2 ((case-sensitive)) i also would like... (21 Replies)
Discussion started by: M@LIK
21 Replies

9. Shell Programming and Scripting

Multiple pattern matching using awk and getting count of lines

Hi , I have a file which has multiple rows of data, i want to match the pattern for two columns and if both conditions satisfied i have to add the counter by 1 and finally print the count value. How to proceed... I tried in this way... awk -F, 'BEGIN {cnt = 0} {if $6 == "VLY278" &&... (6 Replies)
Discussion started by: aemunathan
6 Replies

10. UNIX for Dummies Questions & Answers

Joining files based on multiple keys

I need a script (perl or awk..anything is fine) to join 3 files based on three key columns. The no of non-key columns can vary in each file. The columns are delimited by semicolon. For example, File1 Dim1;Dim2;Dim3;Fact1;Fact2;Fact3;Fact4;Fact5 ---- data delimited by semicolon --- ... (1 Reply)
Discussion started by: Sebben
1 Replies

Featured Tech Videos