Passing backslash character to awk variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Passing backslash character to awk variable
# 1  
Old 11-18-2012
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 08:54 AM.. Reason: code tags
# 2  
Old 11-18-2012
How about sed?

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

This User Gave Thanks to msabhi For This Post:
# 3  
Old 11-18-2012
Thanks msabhi .
I'm using ksh on Solaris. This variation worked.
Code:
sed -n -e "/$var/,/from/p" x.txt

This User Gave Thanks to Mudshark For This Post:
# 4  
Old 11-18-2012
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
These 2 Users Gave Thanks to Scrutinizer For This Post:
# 5  
Old 11-18-2012
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

These 2 Users Gave Thanks to Don Cragun For This Post:
# 6  
Old 11-18-2012
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
This User Gave Thanks to only4satish For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk with passing variable

I have file called in in.txt contains with the below lines I want to display the lines between the value which I would be passing. one two three four five ten six seven eight Expected output if I have passed one and ten two three four five (8 Replies)
Discussion started by: mychbears
8 Replies

2. Shell Programming and Scripting

[BASH] Getting a semi-tailing backslash when passing (escaped) variables to script

Heyas Figured me had a 'typo' in tui-conf-set, i went to fix it. Now, i also figured, it might be nice to have tui-conf-set report (to console, not only exit code) wether it could save the variable to the file or not. This said, I appended this code: (the tui-title and tui-echo lines are... (3 Replies)
Discussion started by: sea
3 Replies

3. Shell Programming and Scripting

Replace special characters with backslash and character

Hi, I have a string wherein i need to replace special characters with backslash and that character. Ex: If my string is a=qwerty123@!, then the new string should be a_new=qwerty123\@\!\, Thanks (3 Replies)
Discussion started by: temp_user
3 Replies

4. Shell Programming and Scripting

awk programming -Passing variable to awk for loop

Hi All, I am new to AWK programming. I have the following for loop in my awk program. cat printhtml.awk: BEGIN -------- <some code here> END{ ----------<some code here> for(N=0; N<H; N++) { for(M=5; M<D; M++) print "\t" D ""; } ----- } ... (2 Replies)
Discussion started by: ctrld
2 Replies

5. Shell Programming and Scripting

Passing awk variable argument to a script which is being called inside awk

consider the script below sh /opt/hqe/hqapi1-client-5.0.0/bin/hqapi.sh alert list --host=localhost --port=7443 --user=hqadmin --password=hqadmin --secure=true >/tmp/alerts.xml awk -F'' '{for(i=1;i<=NF;i++){ if($i=="Alert id") { if(id!="") if(dt!=""){ cmd="sh someScript.sh... (2 Replies)
Discussion started by: vivek d r
2 Replies

6. Shell Programming and Scripting

Passing variable to awk

Hi, I'm new with this stuff, but I hope you can help me. This is what I'm trying to do: for id in $var; do awk '{if ($1 == $id) print $2}' merg_data.dat > neigh.tmp done I need that for every "id", awk search the first column of the file merg_data.dat which contains "id" and... (3 Replies)
Discussion started by: matteo86
3 Replies

7. Shell Programming and Scripting

passing double backslash(\\) as a path in unix

Hi All, I am trying to add a tag in the *.imp file. This is a piece of code which I am giving in my template file and my script reads this template file attachment and passes to windows server(as they have provided the below file path). ... (3 Replies)
Discussion started by: defendersubbu
3 Replies

8. Shell Programming and Scripting

Passing variable and wild card character to grep in Perl

HI All, I have a script that needs to find out a list of files in a directory, i pass the search parameter as an argument. opendir ( DIR, $dir ) || die "Error in opening dir $dirname\n"; @filename1 = (grep {/$File_pattern/ } readdir(DIR)); The problem is my file patterns are like... (1 Reply)
Discussion started by: amit1_x
1 Replies

9. Shell Programming and Scripting

remove backslash character from file

How do I remove a backslash character \ from a file? I have used sed -e "s|\||g" filename > newfile I have done several permutations on this to no avail such as: sed -e "s|`\`||g" filename > newfile sed -e "s|""\""||g" filename > newfile What am I doing wrong?:confused: ... (2 Replies)
Discussion started by: MissI
2 Replies

10. Shell Programming and Scripting

How ro handle backslash character in grep?

Hi , I am doing invert grep using -v but the string contain "/" which break the grep command and it do not skip the lines with "/" on it. Diffu.txt ======== 1159c1159 < <td align="right" valign="middle" class="paddingRight2px" id="featureListItemChannelButton7466"> --- > <td... (1 Reply)
Discussion started by: rajbal
1 Replies
Login or Register to Ask a Question