This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Linux / UNIX and the fly likes Using variables as search patterin in awk command Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "Using variables as search patterin in awk command" Watch "Using variables as search patterin in awk command" New topic
Author

Using variables as search patterin in awk command

Swapnil Bharaskar
Greenhorn

Joined: Apr 18, 2013
Posts: 1
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
Ranch Hand

Joined: Aug 27, 2012
Posts: 1035
    
  10

The shells I have worked with only expand variables within double quotes and not within single quotes.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Yeah, Richard's got it. You need to use double quotes and escape the AWK variable, like



[Jess in Action][AskingGoodQuestions]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Using variables as search patterin in awk command
 
Similar Threads
awk script inside a shell script
split function problem in AWK
Execute String as a Unix Command
Search on a string
using sed to chnage data in file