awk fails when using variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk fails when using variable
# 1  
Old 12-13-2012
awk fails when using variable

This works when I try to execute normally :

Code:
echo | format | nawk '/pci@1f,4000/{print x}; {x=$0 }'

But fails when define a variable and put it in a file:

Code:
cat test
c=pci@1f,4000
echo | format | nawk "/$c/{print x}; {x=$0 }"

./test
nawk: syntax error at source line 1
 context is
        /pci@1f,4000/{print x}; >>>  {x=. <<< /test }
nawk: illegal statement at source line 1

Any suggestions.

Last edited by Scrutinizer; 12-13-2012 at 01:03 AM.. Reason: code tags
# 2  
Old 12-13-2012
Change as below and try..
Code:
$ c="pci@1f,4000"
$ nawk '/c/{print x}; {x=$0 }'

# 3  
Old 12-13-2012
That doesn't work either.

I thought " " should be used instead of ' ' because when using variables double quotes should be used. Btw.

Code:
c=pci@1f,4000
echo | format | nawk "/$c/ {print}"

This works perfectly fine.
And I am using Solaris. "grep -B" isn't working. Any other code?

Last edited by Scrutinizer; 12-13-2012 at 04:23 AM.. Reason: icode -> code tags
# 4  
Old 12-13-2012
In the first sample you use single quotes, in the second you use double quotes. Because of these $0 is processed as a shell variable, so you would need to escape that $-sign (x=\$0). Or try:
Code:
c=pci@1f,4000
echo | format | nawk -v c="$c" '$0~c{print x} {x=$0 }'

which should work as long as c does not contain special (extended regular expression) characters, in which case they would need to be escaped...


--
Quote:
Originally Posted by michaelrozar17
Change as below and try..
Code:
$ c="pci@1f,4000"
$ nawk '/c/{print x}; {x=$0 }'

There is no need for the double quotes here. /c/ matches the character "c"

Last edited by Scrutinizer; 12-13-2012 at 01:54 AM..
These 2 Users Gave Thanks to Scrutinizer For This Post:
# 5  
Old 12-13-2012
Thanks for that code. It worked.

Can you please elaborate where exactly I was going wrong. Thanks.
# 6  
Old 12-13-2012
Hi aksijain, I wrote something in my post, does that help?
# 7  
Old 12-13-2012
@Scrutinizer

Since both the variable and its value contains character c, i was getting the correct result while testing.However i must have verified again the command.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Looping on variable having new line \n fails

I have a for loop that constructs a variable "filelistonly" having entries separated by "\n" new line. The second, third & fourth while loops are my attempt to iterate the variable "filelistonly" upon new line "\n", however non of them work. Below is my script: //First Loop for i in... (2 Replies)
Discussion started by: mohtashims
2 Replies

2. Shell Programming and Scripting

Expansion of variable inside Single Quotes Fails!!

I am unable to expand the value of entry variable inside the nawk command. I tried three different nawk command as below but none of them substitute the value of entry variable. ls *.txt | while IFS='' read -r entry; do #nawk '/<name>/{A=1;++i} A{print >> ("cmd"i"_"$entry)}... (9 Replies)
Discussion started by: mohtashims
9 Replies

3. Shell Programming and Scripting

awk works on Linux but fails on Solaris

On linux i have the below command working fine. awk '/<app-deploy>/{A=1;++i} /<\/app-deploy>/{print >> "found"i".tmp";A=0} A{;print >> "found"i".tmp"}' deploy.xml But the same is failing on Solaris Output: awk: syntax error near line 1 awk: bailing out near line 1 uname -a SunOS mymac 5.10... (5 Replies)
Discussion started by: mohtashims
5 Replies

4. Shell Programming and Scripting

sed variable expansion fails for substitution in range

I'm trying to change "F" to "G" in lines after the first one: 'FUE.SER' 5 1 1 F0501 F0401 F0502 2 1 F0301 E0501 F0201 E0502 F0302 3 1 F0503 E0503 E0301 E0201 E0302 E0504 F0504 4 1 F0402 F0202 E0202 F0101 E0203 F0203 F0403 5 1 F0505 E0505 E0303 E0204 E0304 E0506... (10 Replies)
Discussion started by: larrl
10 Replies

5. UNIX for Dummies Questions & Answers

Find command fails when a space is in the directory path variable

I have a script like this running under OS X 10.8. The problem arises when the find command encounters a space in the path name. I need the "dir" variable as I'll be extending the script to more general use. #!/bin/bash CFS=$IFS IFS=$(echo) set dir = "/Users/apta/Library/Mail\... (3 Replies)
Discussion started by: apta
3 Replies

6. Shell Programming and Scripting

SH script, variable built command fails, but works at command line

I am working with a sh script on a solaris 9 zone (sol 10 host) that grabs information to build the configuration command line. the variables Build64, SSLopt, CONFIGopt, and CC are populated in the script. the script includes CC=`which gcc` CONFIGopt=' --prefix=/ --exec-prefix=/usr... (8 Replies)
Discussion started by: oly_r
8 Replies

7. Shell Programming and Scripting

echoing complex awk command into file fails

Using hp-ux's shell, I'm trying to echo a complex awk command into a script file for later use. But it fails on a newline character and splits the rest of the command onto the next line. echo ' printf("%s: TOTAL = %18.0lf\n", FILENAME, TOTAL) >> "TOTAL.TXT";' >>awk.script Looks... (3 Replies)
Discussion started by: Scottie1954
3 Replies

8. Shell Programming and Scripting

awk substr fails

Hi all, I want to get each line of a data file from position 464 plus 8 characters. I tried in two different ways, and the results were different. I'd like to know why. First method, using awk: awk '{print substr($0,464,8)}' CONCIL_VUELTA_ALF_100112_0801.okSecond method, using scripting:... (5 Replies)
Discussion started by: AlbertGM
5 Replies

9. Shell Programming and Scripting

using awk for setting variable but change the output of this variable within awk

Hi all, Hope someone can help me out here. I have this BASH script (see below) My problem lies with the variable path. The output of the command find will give me several fields. The 9th field is the path. I want to captured that and the I want to filter this to a specific level. The... (6 Replies)
Discussion started by: Cowardly
6 Replies

10. Shell Programming and Scripting

csh fails when setting variable

I have a csh that is called from autosys. It fails when it hits this code env | grep Rep if ( $status == 0 ) then echo "" else setenv REP "" endif However if I run it from the command line, as opposed to from autosys (job schduler) it runs fine. I thought it might be some kind of... (2 Replies)
Discussion started by: gillbates
2 Replies
Login or Register to Ask a Question