Mailing List Archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [tlug] Sed question



On Mon, Feb 17, 2003 at 12:27:52AM -0800, Matt Gross wrote:
> I'm looking a quick sed fix to a problem I'm faced
> with.  I have a file with many lines of data.  What I
> need to do is concatenate the second line to the end
> of the first line.  That's all.  Does anyone know how
> this can easily be done with sed?  Is there an easier
> way to do this other than sed?
> 

You can use a combination of tr and sed to do the magic, but
I don't think it is the best solution[1], but here it is regardless:

cat <file.txt> | tr "\n" "^G" | sed -e 's/^G/ /' | tr "^G" "\n"

This is needed because sed "wisely" strips the \n before processing
and tacks it back on after its finish. Then to make things worse it
doesn't include a join range function.  Its exactly things like this
that led to awk and perl, and we are all better for them.[2]

--Matt

[1] this solution assumes that you don't have actual ^G enbedded in
the text and you have to use (ctl-v ctl-g) to create the control
chars so the substition will work.

[2] though I know some might violently disagree with in reference to
perl.  What ya going to do though?


Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links