Is it possible to change the default field separator in awk

awk

I'm always using awk to manipulate comma separated files, so the first line in my code is always changing the field separator to "," like so:

awk 'BEGIN {FS=","}
$2 < 20 {print $1}' myfile.csv

Is it possible to change awk's defaults so that comma is the default FS? It's not a big problem, but it would just make things a bit neater. I tried googling, but didn't find anything useful; it might be that this isn't possible, but I thought I'd ask!

Best Answer

EDIT: In general, as has been highlighted in the comments, the safe way to use an alias is to use a different name, eg awkc

Create an alias which sets the Field Seperator to a comma. Put the alias in ~/.bashrc, or if ~/.bashrc sources (includes) ~/.bash_aliases, then put it there. You can subsequently override it again in your command to whateveryou like,

Be aware that if you use 'awk' as the name of your alias, your script is totally not portable. It will only run properly on a system which has that particular alias.

alias awkc='awk -F","'

Example (using the alias)

echo "a,b c" | awkc '{print $1" -- "$2}'  

Output

a -- b c