[plug] Using sed to remove \n from a text file.

The Thought Assassin assassin at penguincare.com.au
Thu Jul 11 14:44:51 WST 2002


On Thu, 11 Jul 2002, The Thought Assassin wrote:
> On Thu, 11 Jul 2002, Buddrige, David wrote:
> > What I am wanting to do is use a sed script to remove any occurence of "-\n"
> > and replace it with no characters at all.
> If a line contains more than one dash, that won't work, you'll need to
> branch back to the beginning of the script. I'll get back to you in 5. :)

OK, that was easier than I thought:
sed ':a; /-$/N; s/-\n//; ta' filename

and now a word of explanation:
sed edits one line at a time. On each line, it runs through the list of
four semicolon-seperated commands, then goes to the next line.

Command 1: ':a'      This is a tag for branching to.
Command 2: '/-$/N'   This invokes the N command on lines ending with a -.
                     The N command appends the next line into the buffer.
Command 3: 's/-\n//; Replaces '-\n' by '', now they're on the same line.
Command 4: 'ta'      Branches back to the 'a' tag, _if_ the 's' succeeded.

Hope this helps,

-Greg



More information about the plug mailing list