aspose file tools*
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
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: 1084
    
  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: 24187
    
  34

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



[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using variables as search patterin in awk command