-2

I can not use unix $variable in Fiexd search of awk command.

Please see below my commands.

a="NEW_TABLES NEW_INSERT"
b="NEW"
echo $a | awk -v myvar=$b -F'$0~myvar' '{print $2}'

is not returning any output

but if manually enter the $b value there , its working as below

echo $a | awk -v -F'NEW' '{print $2}'

    outputs: 
 TABLES NEW_INSERT
8
  • @downvoter: Explain the reason Commented Feb 28, 2014 at 16:50
  • 1
    I didn't downvote... Also, it would be good to give a sample of what's in the $a variable. Commented Feb 28, 2014 at 16:51
  • 1
    OK, just updated to show it. Commented Feb 28, 2014 at 16:53
  • 3
    your use of awk is somewhat unorthodox. it would be better if you showed inputs explained what you are trying to achieve. Commented Feb 28, 2014 at 16:57
  • 1
    Also it would be important to indicate what is your expected output. Commented Feb 28, 2014 at 16:59

2 Answers 2

2

This should make it:

$ a="NEW_TABLES NEW_INSERT"

$ echo $a | awk -F"NEW_" '{print $2}'
TABLES 

$ b="NEW_"
$ echo $a | awk -F"$b" '{print $2}'
TABLES 
Sign up to request clarification or add additional context in comments.

2 Comments

if i use semi colon ';' in the awk it throws error. awk -F'\;' '{print $1}' Output : awk: warning: escape sequence \;' treated as plain ;'
No need to escape the ;. Use directly awk -F';'
1

Your quotings are all messed up and you can use your variable to split the line using split function:

a="NEW_TABLES NEW_INSERT"
b="NEW"
echo $a | awk -v myvar="$b" '{split($0,ary,myvar);print ary[2]}'

Outputs:

_TABLES

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.