I have created a script with an awk command that reads:
myVar=$(awk -v FS="HAMMER=" 'NF>1{print $2}' TEST.properties)
echo "Appliances="$myVar
The file TEST.properties
contains the following:
...
HAMMER=foo1,foo2
JACKHAMMER=foo3
...
the above command returns
foo1,foo2
foo3
How should I modify the command to find only HAMMER
and not every word containing HAMMER
?
Use a start of the line ^
in your field separator FS
:
awk -v FS="^HAMMER=" 'NF>1{print $2}'
But if you have key=value construction, you'd better use:
awk -v FS='[=,]' '$1=="HAMMER"{for(i=2;i<=NF;i++} print $i}'
The field separator is set to either =
or ,
. If the first parameter is your keyword, print all other parameters of that line.
either :
awk -F'=' '$1=="HAMMER"{print $2}' file
or:
grep -oP '(?<=^HAMMER=).*' file
awk -F= '/^HAMMER=/ { print $2 }' file
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.