Home > bash > sed mimic tr

sed mimic tr

Problem: Use sed to replace new lines in a file instead of tr.

$ cat file


$ cat file | tr ‘\n’ ‘, ‘

apples, peaches, pears, mangoes

$ sed ‘:mylbl;N;s/\n/,/;t mylbl’ file

apples, peaches, pears, mangoes

Sed explained. (Atleast that I how I understand it)

    # Create a label. - :mylbl
    # Move to the next line - N
    # substitute new line with a comma. - s/\n/,/
    # Move to a label only if a substitution was done. - t mylbl 
    sed ':mylbl;N;s/\n/,/;t mylbl' file 

source: www.unix.com

Categories: bash Tags:
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: