👤


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.

sed / awk to concatenate lines until blank line

👤 Login to reply
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-29-2009
dunstonrocks dunstonrocks is offline
Registered User
 
Join Date: Mar 2009
Last Activity: 30 March 2009, 3:32 PM EDT
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
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
Sponsored Links
    #2  
Old 03-29-2009
cfajohnson's Unix or Linux Image
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
 
Join Date: Mar 2007
Last Activity: 11 July 2016, 2:55 PM EDT
Location: Toronto, Canada
Posts: 2,898
Thanks: 0
Thanked 134 Times in 118 Posts
Code:
awk  'BEGIN { RS = ""; OFS = " "}
            {$1 = $1; print }'

Sponsored Links
    #3  
Old 03-29-2009
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
 
Join Date: Jun 2007
Last Activity: 11 November 2016, 3:44 AM EST
Location: Beijing China
Posts: 1,305
Thanks: 0
Thanked 26 Times in 26 Posts
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
dunstonrocks dunstonrocks is offline
Registered User
 
Join Date: Mar 2009
Last Activity: 30 March 2009, 3:32 PM EDT
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
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
Sponsored Links
    #5  
Old 03-30-2009
dunstonrocks dunstonrocks is offline
Registered User
 
Join Date: Mar 2009
Last Activity: 30 March 2009, 3:32 PM EDT
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
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.
Sponsored Links
    #6  
Old 03-30-2009
Franklin52's Unix or Linux Image
Franklin52 Franklin52 is offline
Registered User
 
Join Date: Feb 2007
Last Activity: 5 June 2018, 9:29 AM EDT
Location: The Netherlands
Posts: 7,747
Thanks: 139
Thanked 558 Times in 520 Posts
Quote:
Originally Posted by dunstonrocks View Post
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
Sponsored Links
    #7  
Old 03-30-2009
dunstonrocks dunstonrocks is offline
Registered User
 
Join Date: Mar 2009
Last Activity: 30 March 2009, 3:32 PM EDT
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
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.
Sponsored Links
👤 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
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



All times are GMT -4. The time now is 10:55 PM.

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





Not a Forum Member?
Forgot Password?