Unix/Linux Go Back    

Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Merge multiple lines into a single line

Shell Programming and Scripting

awk, sed, solved

Thread Tools Search this Thread Display Modes
Old Unix and Linux 07-17-2017
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Join Date: Jul 2012
Last Activity: 23 September 2017, 6:14 PM EDT
Location: San Jose, CA, USA
Posts: 10,508
Thanks: 542
Thanked 3,675 Times in 3,135 Posts
Hi dwdnet,
Here is a copy of the script with comments explaining what each line of code is doing:

awk '	# Use awk to run the following script.
# Function called to add the terminating <newline> to the end of a partial
# output line.
function p() {
	# Determine whether or not a partial record exists...
	if(d) {
		# ... A partial record does exist.  Add the terminating
		#  <newline>,and clear the d and s flags.
		print ""
		d = s = 0
{	# For every line read from the input file, remove a <carriage-return>
	# character if one is present.
	sub(/\r/, "")
/^timestamp=/ {
	# We have found the 1st line of a new input record.
	# If we have a partial output record, finish it.
NF {	# For each input line that contains at least one non-<cspace>, non-<tab>
	# character...
	# ... (1) discard leading and trailing <space> and <tab> characters and
	# convert strings of internal <space> and <tab> characters to a single
	# <space> character.
	$1 = $1
	# ... (2) discard a trailing <semi-colon> if there is one, but set flag
	# so we know that we found one.
	ns = sub(/;$/, "")
	# ... (3) print a <semi-colon> if one was present at the end of the
	# previous non-empty input line, a separating <space> character if this
	# is not the 1st input line in a record, and the contents of the
	# compacted current input line (without the terminating <newline>
	# character).
	printf("%s%s%s", (s ? ";" : ""), (d ? " " : ""), $0)
	# ... (4) set the s flag to note whether or not we found a <semi-colon>
	# at the end of this input line.
	s = ns
	# ... and (5) set the d flag to note that we have now printed at least
	# one part of an output record.
	d = 1
END {	# When we hit EOF on the input file, terminate the last partial output
	# record if there is one.
}' file	# terminate the awk script and name the input file to be processed.

I hope this helps. Ask more questions if you still don't understand how something works.
The Following User Says Thank You to Don Cragun For This Useful Post:
dwdnet (07-17-2017)
Sponsored Links
Old Unix and Linux 07-17-2017
dwdnet dwdnet is offline
Registered User
Join Date: Jul 2017
Last Activity: 21 July 2017, 10:51 AM EDT
Posts: 12
Thanks: 17
Thanked 0 Times in 0 Posts
Thanks Don, definitely does help. I'll have to take some time to digest this.
Sponsored Links

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need help combining txt files w/ multiple lines into csv single cell - also need data merge jetsetter UNIX for Dummies Questions & Answers 2 02-04-2013 09:12 AM
Merge a group of lines into single line prasanth_babu UNIX for Advanced & Expert Users 8 10-18-2010 01:01 PM
merge lines into single line based on symbol \t repinementer Shell Programming and Scripting 5 07-15-2009 09:16 AM
Help on Merge multi-lines into one single line happyday Shell Programming and Scripting 5 05-25-2009 07:30 AM
Merge multi-lines into one single line happyday Shell Programming and Scripting 4 05-09-2009 01:00 PM

All times are GMT -4. The time now is 09:26 PM.