sed


If you are like me, at some point you have tried to remove newlines from a file with SED.
I have encountered this many times, the nuance always seems to escape me the next time I need it.

The reason it can’t be done in the manner you are most likely trying (sed ’s/\n//g’ infile > outfile), is because SED operates on one line at a time, and generally ignores the newline character.

An easy way to accomplish this is to use awk, or tr:

awk '{ORS=" "; print $0}' infile > outfile
or
cat infile|tr '\n' ' '

If you are stubborn, and still want to use sed;
sed -n ':a;N;$!ba;s/\n//g;p' infile >outfile

I get several requests for directory data in CSV format. Until openldap has a csv output option, we will use SED to save the day!

ldapsearch -h localhost -b o=mydomain,c=us uid=sbak* mail telephonenumber

dn: GID=0000001,o=mydomain,c=us
telephoneNumber: 1-734-555-0001
mail: sbak@mydomain.com

dn: GID=0000002,o=mydomain,c=us
telephoneNumber: 1-734-555-0002
mail: sbaker@mydomain.com

If we redirected the above date to a file 11.txt we can than run this command to place a comma at the end of every line:

sed -n '1h;2,$H;${g;s/\n/,/g;p}' 11.txt > 22.txt

Next we will use a simple sed to replace the double comma with a newline:

sed 's/,,/\n/g' 22.txt > 33.csv

Lets look at 33.csv to see the finished product:

sbak@fedoraCore:~ $ cat 33.csv
dn: GID=0000001,o=mydomain,c=us,telephoneNumber: 1-734-555-0001,mail: sbak@mydomain.com
dn: GID=0000002,o=mydomain,c=us,telephoneNumber: 1-734-555-0002,mail: sbaker@mydomain.com

Now you can send your managers data that they can open with excel!