Computing.Net > Forums > Unix > SED - copy lines to next line ....

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

SED - copy lines to next line ....

Reply to Message Icon

Name: mud_sed
Date: April 4, 2007 at 08:34:25 Pacific
OS: Solaris
CPU/Ram: 512MB
Product: Sun
Comment:

I have the following file :
First string


second string
third string

932

--end of file----
The output should look like:
First string
First string
First string
second string
third string
third string
932
932
--end of file----

I want keep copying a line to next line(s) till I reach another string/number using "sed".




Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: April 4, 2007 at 11:53:58 Pacific
Reply:

In my opinion, sed is not the best tool for this problem. It will take a sed programmer who is smarter than I am. If this is homework and you have to use sed, take a look at this link:

http://www.student.northpark.edu/pe...

In the above thread, Eric Pement explains how to use if comparisons in sed. Perhaps it will help you.

In my shell solution below, I read the file a line at a time and if the line is defined I print it out and save it in another variable - saveline. If the line is undefined, I print the variable saveline:

#!/bin/ksh

while read line
do
if [[ -z "$line" ]]
then
echo "$saveline"
else
saveline="$line"
echo "$line"
fi
done < file


0

Response Number 2
Name: mud_sed
Date: April 4, 2007 at 13:24:08 Pacific
Reply:

excellent ..it works...thanks .
No I don't NEED to use 'sed' ..I thought it might be the best way...But I was wrong :)

Another question . If I have a file with tab seperated columns ..how can I use your script only for one particular column?



0

Response Number 3
Name: nails
Date: April 4, 2007 at 14:58:12 Pacific
Reply:

Actually, you don't have to do anything, and the script should work as it does now. That's because the default field separater, IFS, in the shell is whitespace.

Whitespace is defined as spaces, tabs, and carriage returns.

BTW, you're welcome.


0

Response Number 4
Name: James Boothe
Date: April 9, 2007 at 10:10:58 Pacific
Reply:

sed -e '/[^ ]/{h;b}' -e g junk.txt

The sed for this is pretty simple actually. If the line contains any characters, it will print it and shove it into the hold buffer. If it is an empty line (or only blanks), it will print whatever is in the hold buffer.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: SED - copy lines to next line ....

sed/awk - line feeds? www.computing.net/answers/unix/sedawk-line-feeds/5237.html

copy lines in vi from other file www.computing.net/answers/unix/copy-lines-in-vi-from-other-file/8359.html

Script to Read a .CSV file www.computing.net/answers/unix/script-to-read-a-csv-file-/7784.html