Monday, July 28, 2008

[code] sed, awk, and tab

Never realized that sed doen't recognize tab as a escape character. So to use regular expression to search and replace tab characters out of a file, use awk instead.

With sed (very dangerous to put tab character like this because it's easy to accidently copy and paste, and turn tab into space):

sed -e 's/ /x/g' myfile.txt

Better with awk:

awk '{gsub("\t","x");print}' myfile.txt


  1. Nice workaround with awk.

    - Oscar the Grouch

  2. POSIX compliant:

    sed "s/$(printf '\t')/x/g" myfile.txt

    With bash:

    sed s/$'\t'/x/g myfile.txt

    Anyway, it would be nice if you allowed some HTML tags for your commenters to use...