Skip to navigation

Archive for the 'Regular expressions' Category

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.)

Test your regular expressions online

Wednesday, June 17th, 2009

Speaking of regular expressions, I found out you can test them online at Neat.

Emacs regular expressions: match at least n occurrences of character class

Tuesday, June 16th, 2009

Aside: I just discovered the very useful Emacs regex builder tool. Type M-x regexp-builder. I wanted a regular expression to match the pattern of mutt mail edit buffers, to apply mail-mode, but I did not want to match the muttrc and mutt.hooks files I have. Mail edit buffers get a pattern that starts with “mutt”, […]

Filename pattern matching via bash extended globbing and find regular expressions in shell scripts

Tuesday, January 20th, 2009

I was confused that bash would expand some patterns nicely to match filenames when I was on the commandline, but the same pattern would not work from inside a shell script. For example, the following pattern would nicely match all files in the current directory not having an “Rnw” or “tex” extension on the commandline: […]

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]*$//’