LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-21-2012, 11:16 PM   #1
jawaharm
LQ Newbie
 
Registered: Jan 2012
Posts: 1

Rep: Reputation: Disabled
BASH - unable to replace variable in script


MYVARIABLE=8100 #declaring a variable

cat /mydir/test.log | awk ' BEGIN {tmp =1} { if ($0 ~ /191:$MYVARIABLE/ && $0 ~ /Request URL is/) { (tmp=tmp+1) }} END {print tmp}'



I tried declaring a variable "MYVARIABLE", and then tried using it in the next statement.. The problem is, when a statement is enclosed in SINGLE QUOTES, variable substitution doesn't occur. I tried using '\047' for single quotes, and that didn't help either.. How do I get around this problem.

Any help is appreciated.
 
Old 01-21-2012, 11:51 PM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
What happens when you use double quotes?
 
Old 01-22-2012, 12:19 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
When using double quotes, bash will change the $0 strings to whatever the value of $0 is. This can be avoided by "backslash escaping" the $0s, changing them to \$0
 
Old 01-22-2012, 04:49 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
As you realized, single quotes prevent shell variable expansion. If you replace them with double quotes, it will expand, but then the shell will also attempt to interpret other values as well, particularly the "$0". You can use creative quoting and escaping so that only the value you want is expanded, but that gets tricky, tiresome, and hard to read.

The better option is to import the shell variable value into an awk variable. This is usually done with the -v option.


Oh, and useless use of cat.

Code:
awk -v var="$MYVARIABLE" 'BEGIN {tmp = 1} { if ($0 ~ var && $0 ~ /Request URL is/) { (tmp=tmp+1) }} END {print tmp}' /mydir/test.log
Here are a few useful awk references:
http://www.grymoire.com/Unix/Awk.html
http://www.gnu.org/software/gawk/man...ode/index.html
http://www.pement.org/awk/awk1line.txt
http://www.catonmat.net/blog/awk-one...ined-part-one/

Finally, environment variables are generally all upper-case. So while not absolutely necessary, it's good practice to keep your own user variables in lower-case or mixed-case, to help differentiate them.

Last edited by David the H.; 01-22-2012 at 04:53 AM. Reason: some rewording
 
Old 01-22-2012, 06:59 AM   #5
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
FWIW You don't need to explicitly name $0...

Code:
MYVARIABLE=8100 #declaring a variable

awk "/191:$MYVARIABLE/ && /Request URL is/ { tmp++ } END { print ++tmp }" /mydir/test.log
But I think it's better to use single quotes and use -v option for awk expression (in case some awk words could be interpreted by the shell)

Grep also works:
Code:
MYVARIABLE=8100 #declaring a variable

matches=$(grep -c -e "191:$MYVARIABLE" -e "Request URL is" /mydir/test.log)
echo $(($matches + 1))

Last edited by Cedrik; 01-22-2012 at 08:29 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 04:11 AM
how to make bash replace the value of a variable? rabbit2345 Programming 3 04-18-2009 11:02 PM
Problem with bash script - variable name within variable name steven.c.banks Linux - Newbie 3 03-10-2009 03:08 AM
Bash Script Help - Trying to create a variable inside script when run. webaccounts Linux - Newbie 1 06-09-2008 02:40 PM
bash script to replace char in variable gmartin Programming 5 11-08-2004 01:46 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration