Skip to navigation

Archive for the 'Sed' Category

Replace DOS line feeds (CRLF line terminators) using sed

Wednesday, March 20th, 2013

A sed command stumbled on the fact that a text file came from Windows. file filename.csv gave ASCII text, with CRLF line terminators To fix it: sed -i ‘s/^M//’ filename.csv Where the “^M” is input by pressing Ctrl-V then ENTER. Credits.

Use sed to remove the first column in a comma separated file (csv)

Monday, August 2nd, 2010

Here’s how: sed -i ‘s/[^,]*,//’ file.csv Note the [^,]* bit, which matches everything that is not a comma. Don’t use .*, because this will greedily match commas, too. (Usually, cut is useful for these sort of things, but cut cannot readily replace the file in place.)

Shell script to periodically check mail logs (and msmtpQ queue) and notify user by mail

Sunday, April 13th, 2008

As said in this previous post, I generally really like the speed of having set sendmail_wait = -1 in my ~/.muttrc, but mutt doesn’t let me know if there are any error messages. That’s not good, and it would be ironic to gain a few seconds when hitting y in mutt just to nervously dig […]

Sed example 2

Monday, June 25th, 2007

Replacing whitespace, lifted directly from the handy collection of sed one-liners at Sourceforge: Delete all leading whitespace from line (tabs and spaces): sed ‘s/^[ \t]*//’ Delete whitespace at end of line: sed ‘s/[ \t]*$//’ Delete leading and trailing whitespace: sed ‘s/^[ \t]*//;s/[ \t]*$//’

Sed example 1

Tuesday, May 1st, 2007

Many things are easiest to learn by looking at examples. I find this to be especially true for sed. Sed stands for “stream editor”, and it is very handy to perform bulk editing of text files. To get started, I refer you to this very nice tutorial. I’m planning to archive some of the small […]