Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 17 Jan 2015 14:56:13 +0000
From:      Mike Clarke <jmc-freebsd2@milibyte.co.uk>
To:        freebsd-questions@freebsd.org
Cc:        zep <zgreenfelder@gmail.com>, Manish Jain <bourne.identity@hotmail.com>
Subject:   Re: Setting up an alias for 'pkg info | awk'
Message-ID:  <2057440.qDmOUYx2b0@curlew.lan>
In-Reply-To: <54BA6112.1070406@gmail.com>
References:  <mailman.60.1421409601.72162.freebsd-questions@freebsd.org> <BLU437-SMTP5127A51A909BBF868937B4F64C0@phx.gbl> <54BA6112.1070406@gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On Saturday 17 Jan 2015 08:18:10 zep wrote:
> > I am trying to list out all the ports on my system with a Bash shell
> > alias :
> > 
> > alias pia="pkg info | awk '{print $1}'"
> > 
> > But this alias simply prints the output of 'pkg info'.
> > 
> > Is there some way to do this ?
> 
> it doesn't make any sense to me why the alias wouldn't be working,

$1 is in a string quoted with double quotes so gets evaluated (to a null 
string) when the alias is defined so you end up with {print } as the awk 
command:

$ alias pia="pkg info | awk '{print $1}'"
$ alias pia
pia='pkg info | awk '\''{print }'\'

Simpler solutions not requiring awk have been given elsewhere in this thread 
but the awk command can be made to work by using Dollar-Single Quotes to quote 
the $ character:

$ alias pia="pkg info | awk '{print $'$'1}'"
$ alias pia
pia='pkg info | awk '\''{print $'\''$'\''1}'\'

$ pia | head -3
GentiumBasic-110_1
GeoIP-1.6.4
ImageMagick-6.9.0.2,1

An alternative would be to break the alias string down into 3 concatenated 
strings which is a bit more error prone to type but makes the resulting alias 
look a bit less like Klingon poetry:

$ alias pia="pkg info | awk '{print "'$1'"}'"
$ alias pia
pia='pkg info | awk '\''{print $1}'\'
$ pia | head -3
GentiumBasic-110_1
GeoIP-1.6.4
ImageMagick-6.9.0.2,1


-- 
Mike Clarke



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?2057440.qDmOUYx2b0>