0

I need to check a lot of configuration files and like to cat them without the lines beginning with comments.

I am typing the following in the shell:

egrep -v '^[[:blank:]]*#' *filename* | awk 'NF'

which works:

$ egrep -v '^[[:blank:]]*#' /etc/nginx/nginx.conf | awk 'NF'
user nginx nginx;
worker_processes 4;
pid /var/run/nginx.pid;
events {
    worker_connections 768;
}
http {
    sendfile on;
}

instead of typing it each time, I wish to make an alias on my batch, but I can't find a way to do so (neither to find info on the net for that :( ).

I tried :

testfunc () { egrep -v '^[[:blank:]]*#' $0 | awk 'NF'; }

but it doesn't work :-(

$ testfunc /etc/nginx/nginx.conf 

nothing is returned :( What do I miss ??

fedorqui
  • 275,237
  • 103
  • 548
  • 598
Romain Jouin
  • 4,448
  • 3
  • 49
  • 79

1 Answers1

4

alias cannot get parameters. Instead, use a function, which in fact is what you are already using.

For this, use $1 and not $0 as you are doing ($1 means the first argument you provide).

All together, this should work:

testfunc () { egrep -v '^[[:blank:]]*#' "$1" | awk 'NF'; }
                                         ^^

Note also that this could be handled with a single awk:

testfunc () {  awk '!/^\s*#/ && NF' "$1"; }
                    ^^^^^^^     ^^
                       |        lines with some content
lines not starting with (spaces) + #
fedorqui
  • 275,237
  • 103
  • 548
  • 598