Passing backslash character to awk variable | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Passing backslash character to awk variable

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-18-2012
Mudshark Mudshark is offline
Registered User
 
Join Date: Apr 2005
Last Activity: 8 September 2014, 3:34 AM EDT
Location: U.K.
Posts: 25
Thanks: 6
Thanked 1 Time in 1 Post
Passing backslash character to awk variable

Hi All.
I have a file that contains some special characters and I'm trying to use AWK to search for lines between <pattern1> and <pattern2>.
As an example:
I need the lines between the line containing ' select_id="x_0 ' and the line containing the next instance of ' from '. This is a file called 'x.txt':


Code:
blah
set $a x_0 $b
blah (
q_db_task=1000, select_id="x_0", text=" select ",
blah=rec (
table="green", text="min(blue)", text="max(brown)",
text="sum(green.PURPLE)"
text="sum(green.BLACK)", text="sum(green.YELLOW)",
zed_var="$d MYCALC1SUM"
text="sum(green.PINK)",
)
from=" from ",
tablist="green", where="
blah
zed_var="$G 1", text="
blah
)
blah
from=" from",
blah

If I use the following then I get the output I need :

Code:
awk  '/select_id=\"x_0/,/from/' x.txt

(with the double quote escaped.)
However - the first search pattern might change so I need to pass that to awk as a variable.
E.G.

Code:
srvr# awk -v sqry='select_id=\\"x_0' '/sqry/,/from/' x.txt
awk: syntax error near line 1
awk: bailing out near line 1
and same using nawk returns nothing.
srvr# nawk -v sqry='select_id=\\"x_0' '/sqry/,/from/' x.txt
srvr#

But this works to match the first pattern using similar syntax.

srvr#
Code:
nawk -v sqry='select_id=\\"x_0' '$0 ~ sqry' x.txt

q_db_task=1000, select_id="x_0", text=" select ",

I think I fixed it using awk in a script :

Code:
BEGIN {awkvar2=sqry;awkvar3="from"}

($0 ~ awkvar2),($0 ~ awkvar3)

BUT - Any help to understand why the pattern match doesn't work would be much appreciated.

Last edited by Scrutinizer; 11-18-2012 at 07:54 AM.. Reason: code tags
Sponsored Links
    #2  
Old 11-18-2012
msabhi's Avatar
msabhi msabhi is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 2 November 2013, 1:26 AM EDT
Location: In books/UNIX.com
Posts: 177
Thanks: 16
Thanked 61 Times in 60 Posts
How about sed?


Code:
var='select_id="x_0'  #you need not escape " when using single quote
sed "/$var/,/from/" x.txt

The Following User Says Thank You to msabhi For This Useful Post:
Mudshark (11-18-2012)
Sponsored Links
    #3  
Old 11-18-2012
Mudshark Mudshark is offline
Registered User
 
Join Date: Apr 2005
Last Activity: 8 September 2014, 3:34 AM EDT
Location: U.K.
Posts: 25
Thanks: 6
Thanked 1 Time in 1 Post
Thanks msabhi .
I'm using ksh on Solaris. This variation worked.

Code:
sed -n -e "/$var/,/from/p" x.txt

The Following User Says Thank You to Mudshark For This Useful Post:
only4satish (11-19-2012)
    #4  
Old 11-18-2012
Scrutinizer's Avatar
Scrutinizer Scrutinizer is online now Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 22 October 2014, 8:25 PM EDT
Location: Amsterdam
Posts: 9,538
Thanks: 284
Thanked 2,422 Times in 2,172 Posts
For reference, awk use of variables. There is no need to escape the double quote:

Code:
awk '$0~b,$0~e' b='select_id="x_0' e='from' infile


--
On Solaris, use /usr/xpg4/bin/awk
The Following 2 Users Say Thank You to Scrutinizer For This Useful Post:
Mudshark (11-18-2012), only4satish (11-19-2012)
Sponsored Links
    #5  
Old 11-18-2012
Don Cragun's Avatar
Don Cragun Don Cragun is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 22 October 2014, 8:15 PM EDT
Location: San Jose, CA, USA
Posts: 4,884
Thanks: 182
Thanked 1,641 Times in 1,392 Posts
Your problem in awk was that /sqry/ was looking for a line containing the literal string sqry not the string contained in the variable sqry. To do what you were trying to do in awk, you could have used:

Code:
awk -v sqry='select_id="x_0' '$0~sqry,/from/' x.txt

or on Solaris systems:

Code:
nawk -v sqry='select_id="x_0' '$0~sqry,/from/' x.txt

The Following 2 Users Say Thank You to Don Cragun For This Useful Post:
Mudshark (11-18-2012), only4satish (11-19-2012)
Sponsored Links
    #6  
Old 11-18-2012
only4satish only4satish is offline
Registered User
 
Join Date: Jan 2012
Last Activity: 21 August 2014, 5:14 AM EDT
Posts: 62
Thanks: 27
Thanked 2 Times in 2 Posts
print lines between <pattern1> and <pattern2>.

assuming pattren 1 is 'select'
pattern 2 is 'from'

a crude way of printing the lines between pattern1 & pattern2


Code:
 
v1=$(grep -n 'select'  x.txt | awk -F ":" '{ print $1}')
v2=$(grep -n 'from' x.txt | awk -F ":" '{ print $2}' )
 
sed -n ' '"$v1"','"$v2"' p' x.txt

it will print lines between pattern1 & pattern2
The Following User Says Thank You to only4satish For This Useful Post:
Mudshark (11-19-2012)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Passing variable to awk matteo86 Shell Programming and Scripting 3 06-09-2011 06:20 AM
passing double backslash(\\) as a path in unix defendersubbu Shell Programming and Scripting 3 03-09-2011 01:11 PM
Passing variable and wild card character to grep in Perl amit1_x Shell Programming and Scripting 1 08-31-2010 01:57 PM
remove backslash character from file MissI Shell Programming and Scripting 2 08-12-2009 02:06 PM
How ro handle backslash character in grep? rajbal Shell Programming and Scripting 1 06-18-2007 08:19 PM



All times are GMT -4. The time now is 08:40 PM.