awk not working in bash -c "somecommand | awk '{print $2}'" -


this command works expected:

(dpkg -l | egrep "linux.+3.8.0.*44" | awk '{print $2}') linux-generic-lts-raring linux-headers-3.8.0-44 linux-headers-3.8.0-44-generic linux-headers-generic-lts-raring linux-image-3.8.0-44-generic linux-image-generic-lts-raring 

however, when run bash -c doesn't work:

bash -c "(dpkg -l | egrep "linux.+3.8.0.*44" | awk '{print $2}')" ii  linux-generic-lts-raring         3.8.0.44.44                       generic     linux kernel image , headers ii  linux-headers-3.8.0-44           3.8.0-44.66~precise1              header files related linux kernel version 3.8.0 ii  linux-headers-3.8.0-44-generic   3.8.0-44.66~precise1              linux kernel headers version 3.8.0 on 64 bit x86 smp ii  linux-headers-generic-lts-raring 3.8.0.44.44                       generic linux kernel headers ii  linux-image-3.8.0-44-generic     3.8.0-44.66~precise1              linux kernel image version 3.8.0 on 64 bit x86 smp ii  linux-image-generic-lts-raring   3.8.0.44.44                       generic linux kernel image 

i have no clue why. it's if pipe breaking.

because you're in double quotes, $2 gets expanded shell, not passed literally awk. , print in awk, default, prints $0, ie. entire line of input, if it's expanded empty value, there's problem.

bash -c "(dpkg -l | egrep 'linux.+3.8.0.*44' | awk '{print \$2}')" 

by way -- there's no point using grep , awk separately.

bash -c "dpkg -l | awk '/linux.+3.8.0.*44/ { print \$2 }'" 

if enclosing shell bash, 1 can use $'' alternate means provide literal strings:

# $'' works bash, ksh, etc., won't interpret $2, , lets # pass literal quotes bash -c $'dpkg -l | awk \'/linux.+3[.]8[.]0.*44/ { print $2 }\'' 

by contrast, if enclosing shell baseline posix, including literal single-quotes inside single-quoted string gets bit more complicated.

bash -c 'dpkg -l | awk '"'"'/linux.+3[.]8[.]0.*44/ { print $2 }'"'"'' 

Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -