sed / awk to concatenate lines until blank line

Tags
shell scripts

 
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-30-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-30-2009 at 12:29 AM..
# 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.

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Replacing lines matching a multi-line pattern (sed/perl/awk) thefang Shell Programming and Scripting 10 02-26-2014 08:47 AM
sed - How to insert line before the first blank line following a token jcdole Shell Programming and Scripting 6 12-29-2013 12:17 PM
String search and print next all lines in one line until blank line jaydeep_sadaria Shell Programming and Scripting 7 10-01-2013 06:21 AM
Awk, sed - concatenate lines starting with string locoroco Shell Programming and Scripting 3 01-22-2013 08:54 PM
awk or sed - Convert 2 lines to 1 line guinch Shell Programming and Scripting 8 04-28-2012 07:40 AM
sed or awk delete character in the lines before and after the matching line KC_Rules Shell Programming and Scripting 5 02-08-2012 12:07 PM
awk concatenate every line of a file in a single line sdf Shell Programming and Scripting 8 01-17-2012 07:07 PM
sed show lines text between 2 blank lines unclecameron Shell Programming and Scripting 2 12-03-2011 02:18 AM
Logfile - extracting certain lines to concatenate into 1 line BecTech Shell Programming and Scripting 3 03-30-2009 08:28 PM
need to concatenate two lines if the line doesnt end with quotes laxmi131 UNIX for Advanced & Expert Users 9 10-27-2008 08:22 AM
sed: delete regex line and next line if blank one71 Shell Programming and Scripting 2 09-18-2008 06:53 AM
sed and blank lines alrinno Shell Programming and Scripting 6 07-17-2008 02:42 PM
concatenate and display 2 lines as 1 with a condition for 2 line ? vithala Shell Programming and Scripting 7 07-11-2008 02: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 02:27 PM
delete blank lines with sed tmxps Shell Programming and Scripting 2 08-21-2007 08:01 PM