Rick Emory
Rick Emory

Reputation: 43

Shell awk command with FS

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?

Upvotes: 0

Views: 497

Answers (3)

Ruslan Osmanov
Ruslan Osmanov

Reputation: 21492

awk -F= '/^HAMMER=/ { print $2 }' file

Upvotes: 0

Kent
Kent

Reputation: 195059

either :

awk -F'=' '$1=="HAMMER"{print $2}' file

or:

grep -oP '(?<=^HAMMER=).*' file

Upvotes: 0

oliv
oliv

Reputation: 13249

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.

Upvotes: 1

Related Questions