Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I have the following file :
First string
second string
third string932
--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".

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

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?

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.

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.

![]() |
![]() |
![]() |

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