[Techtalk] text parsing question

Gebhard Dettmar gebhard.dettmar at student.hu-berlin.de
Thu Oct 26 18:48:08 UTC 2006


On Thursday 26 October 2006 19:27, gossamer axe wrote:
> Hi!
>
> I have a text file that looks like
> ,
> question
> ,answer
> ,question2
> ,answer2
> ,
> Now, I have to hand edit it to bring
> ,
> question
> ,answer
> to one line.  I was wondering if I can do something like that with sed
> automatically...Based on the fact that each future line will have 3
> present lines.
>
> [...]
Hmm, just to make sure I understood your question - you want to bring 
,
question
,answer
to
question, answer
(=one line)
This should work in sed with \n
Unfortunately it doesn't (with my GNU sed version 3.02 ). So I took perl, 
which is the only language I understand Regexes with (at least sometimes 
;-)

Having this in 'testfile',
---snip---
,
question
,answer
,question2
,answer2
,
---snap---
perl -pi -e 's/(,$)|([nr2])\n/$2 /' testfile
brings
question ,answer ,question2 ,answer2

Maybe this points you in the right direction
Regards,
Gebhard




More information about the Techtalk mailing list