This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using variables as search patterin in awk command

 
Swapnil Bharaskar
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am using awk command to print specific column from a file. I have used two variable viz., "filename" and "CURR_DATE".

-------------------------file structure-----------------------------------------------------------

ABCD|7654|90987
BNMK|47514|4754

----------------------------------------------------------------------------------------------------

Below are the values of varibles:
filename=ABC
CURR_DATE=20130418

Now I want to print second column when search pattern is matched. However, when I give variables in awk command, it returns me blank.

awk -F"|" '/$filename/ {print $2}' /home/abc/Info/test_$CURR_DATE.log

And when I give actual values rather than varibles, it gives me proper output.

awk -F"|" '/ABC/ {print $2}' /home/abc/Info/test_20130418.log

please help me so that I can get get the value of second column

Thanks,
Swapnil
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The shells I have worked with only expand variables within double quotes and not within single quotes.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, Richard's got it. You need to use double quotes and escape the AWK variable, like

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic