Home Man
Search
Today's Posts
Register

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.

sed / awk to concatenate lines until blank line

Tags
shell scripts

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 03-29-2009
sed / awk to concatenate lines until blank line

Sample input (line feed indicated by [LF])
---------------
The red fox jumped [LF]
over the brown fence of the [LF]
red hous [LF]
[LF]
[LF]
[LF]
He then went into the[LF]
orchard[LF]
[LF]

---------------
Desired Output
---------------
The red fox jumped over the brown fence of the red house
[LF]
He then went into the orchard
[LF]


any input on a sed/awk one liner to achieve this would be immensely helpful
# 2  
Old 03-29-2009
Code:
awk  'BEGIN { RS = ""; OFS = " "}
            {$1 = $1; print }'

# 3  
Old 03-29-2009
perl:

Code:
undef $/;
open $fh,"<","a.pl";
my $str=<$fh>;
my @arr=split(/\n\n+/,$str);
map {my $t=$_; $t=~s/\n/ /g; print $t,"\n\n";} @arr;

sed:

Code:
sed -n '/^$/ !{
	1{
	h
	}
	1 !{
	H
	}
}
/^$/ {
 x
 /^$/ !{
 	s/\n//g
 	p
 	x
 	p
 	x
 	d
 }
}
${
	x
	/^$/ !{
	 s/\n//g
   p
   d
  }
}' a


Last edited by summer_cherry; 03-29-2009 at 11:29 PM..
# 4  
Old 03-30-2009
cfajohnson, your solution removed all the blank lines......
summer_cherry: I tried playing around with your solution. It came closest to what I am looking for, but I guess my requirements are a little more complicated and I didn't outline them correctly before.

Here's what I have done so far
1> remove all blank lines using sed -e 's/^$//g' file.txt > file1.txt

2> Now I have file in the following format (delimiter indicated by DL, Line Feed by LF)
------------------------
Sample Input
------------------------
[DL]
The fox jumped over the red[LF]
fence into[LF]
the orchard to eat some[LF]
grapes[LF]
[DL]
Then it came back to the[LF]
yard to hunt some[LF]
sheep[LF]
[DL]
The farmer chased[LF]
the fox away[LF]

---------------------
Expected Output
---------------------
[DL]
The fox jumped over the red fence into the orchard to eat some grapes
[DL]
Then it came back to the yard to hunt some sheep
[DL]
The farmer chased the fox away
# 5  
Old 03-30-2009
I am also trying to achieve this output, but I have failed with various sed solutions that I have tried.
-------------------------
SAMPLE INPUT
-------------------------
Name: ABCD
Address: ABCD Road, ABCD City, ABCD State
ZipCode: 0000
Telno: 999-999-9999

Name: EFGH
Address Temp Road, Temp City, Temp State
Telno 999-999-9999

----------------------
EXPECTED OUTPUT
----------------------

Name: ABCD
Address: ABCD Road, ABCD City, ABCD State
ZipCode: 0000
Telno: 999-999-9999

Name: EFGH
Address Temp Road, Temp City, Temp State
ZipCode: <--- This is the new entry
Telno 999-999-9999

Basically, the requirement is to insert a word between two words if the word does not already exist.
# 6  
Old 03-30-2009
Quote:
Originally Posted by dunstonrocks
I am also trying to achieve this output, but I have failed with various sed solutions that I have tried.
-------------------------
SAMPLE INPUT
-------------------------
Name: ABCD
Address: ABCD Road, ABCD City, ABCD State
ZipCode: 0000
Telno: 999-999-9999

Name: EFGH
Address Temp Road, Temp City, Temp State
Telno 999-999-9999

----------------------
EXPECTED OUTPUT
----------------------

Name: ABCD
Address: ABCD Road, ABCD City, ABCD State
ZipCode: 0000
Telno: 999-999-9999

Name: EFGH
Address Temp Road, Temp City, Temp State
ZipCode: <--- This is the new entry
Telno 999-999-9999

Basically, the requirement is to insert a word between two words if the word does not already exist.
Suppose you want to insert the text for the name EFGH:

Code:
awk '/EFGH/{print;getline;print;print "Zipcode:..";next}1'

Regards
# 7  
Old 03-30-2009
No, I don't know for which record I would need to insert the missing field.

I know the order of fields in a record.
So if any field is missing in a record, insert it at an appropriate place in that record.
Do the same for all records.
Login to Reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
String search and print next all lines in one line until blank line jaydeep_sadaria Shell Programming and Scripting 7 10-01-2013 05:21 AM
Delete lines containing and remove the blank line at the same time cokedude Shell Programming and Scripting 2 06-04-2012 06:34 AM
Print lines between a regExp & a blank line dips_ag Shell Programming and Scripting 3 11-30-2011 07:16 AM
insert blank line between lines Pouchie1 Shell Programming and Scripting 3 04-07-2011 08:40 PM
Logfile - extracting certain lines to concatenate into 1 line BecTech Shell Programming and Scripting 3 03-30-2009 07:28 PM
need to concatenate two lines if the line doesnt end with quotes laxmi131 UNIX for Advanced & Expert Users 9 10-27-2008 07:22 AM
concatenate and display 2 lines as 1 with a condition for 2 line ? vithala Shell Programming and Scripting 7 07-11-2008 01:01 AM
Need solution concatenate and display 2 lines as 1 with a condition for 2 line ? vithala UNIX for Advanced & Expert Users 1 07-10-2008 01:27 PM
Strip one line from 2 blank lines in a file tipsy Shell Programming and Scripting 6 06-23-2008 08:14 AM
Read lines till a blank line is encountered saurabhsinha23 UNIX for Dummies Questions & Answers 1 12-19-2007 03:26 PM


All times are GMT -4. The time now is 06:52 PM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password