|
Re: sed pattern recognition with consecutive lines: msg#00072editors.sed.user
Replying to Grendelos, who asked: > I have been playing with this all morning and have not found the > solution so hopefully someone has a suggestion. Read the sed FAQ. > What I am trying to > do is have sed recognize a pattern of words falling on consecutive > lines, for example if I had an example file structured as: > > I > have > been > there > and > I > do > not > like > it > > and I would like to make the file look like this: > > I > have > been > there > and > We > can > like > it > > My thought was something like: > > sed 's/I\ndo\nnot/We\ncan/' example.file > > but this does not work. I can make the newline (\n) work in the > replacement part, but it does not seem to work in the pattern > recognition part. > > Any ideas? >From the sed FAQ: # 5.10. Why can't I match or delete a newline using the \n escape # sequence? Why can't I match 2 or more lines using \n? http://sed.sourceforge.net/sedfaq5.html#s5.10 At this point, I would invoke the UNIX 'tr' utility to help out: tr -s ' \n\r' ' ' <file | sed 's/I do not/We can/' | tr ' ' '\n' tr will "translate" incoming code. The -s option switch will "squeeze" multiple occurrences of characters, and on my Windows platform, I wanted to tranfer any runs of space, newline, or carriage return into a single space character on the output. Then I set sed to transform the output, and use 'tr' in reverse order to add the newlines again. If you don't need the last instance of tr, then change it to fmt (format) or fold or whatever you need. -- Eric Pement ------------------------ Yahoo! Groups Sponsor --------------------~--> Get Bzzzy! (real tools to help you find a job). Welcome to the Sweet Life. http://us.click.yahoo.com/KIlPFB/vlQLAA/TtwFAA/dkFolB/TM --------------------------------------------------------------------~-> -- Yahoo! Groups Links <*> To visit your group on the web, go to: http://groups.yahoo.com/group/sed-users/ <*> To unsubscribe from this group, send an email to: sed-users-unsubscribe@xxxxxxxxxxxxxxx <*> Your use of Yahoo! Groups is subject to: http://docs.yahoo.com/info/terms/ |
|
| <Prev in Thread] | Current Thread | [Next in Thread> |
|---|---|---|
| Previous by Date: | Re: sed one-liner?: 00072, Eric Pement |
|---|---|
| Next by Date: | Re: sed one-liner?: 00072, Angus Leeming |
| Previous by Thread: | sed pattern recognition with consecutive linesi: 00072, grendelos |
| Next by Thread: | Re: sed one-liner?: 00072, Eric Pement |
| Indexes: | [Date] [Thread] [Top] [All Lists] |
| News | FAQ | advertise |