awk syntax


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers awk syntax
# 1  
Old 02-24-2010
Pattern searching using variables in awk

Little bit confusing while using awk SmilieSmilie
In Sed while pattern search we can use "(double quotes)
i mean
Code:
$a=hello
$cat file.txt |sed -n "/$a/p"

this thing work fine But if i use it in awk it's not working How could i do the substitution of pattern by a variables and the result as the desired position mean the 2nd and 4th location data only not the whole line. if i go for this
Code:
$cat file.txt | awk "/$a/{print $2,$4}"

in $2,$4 at ,(comma) the syntax error is coming.

Thank & Regard's
Posix
-------------------
Luv to learn awk and sed

Last edited by posix; 02-25-2010 at 01:17 AM.. Reason: suitable title.
# 2  
Old 02-24-2010
The dollar sign '$' tells the shell that the characters trailing it form variable name. Besides the usual variables, there are also special ones like $@, $*, and $0 to $9.

Your sed command gets transformed like this:
Code:
cat file.txt |sed -n "/$a/p"
cat file.txt |sed -n /hello/p

With the awk command you've given, the shell tries to interpolate the $2 and $4 with the appropriate positional parameters, which probably aren't set. So the transformation would look like this:
Code:
cat file.txt | awk "/$a/{print $2,$4}"
cat file.txt | awk /hello/{print ,}

But fear not, there is help. awk supports the -v switch, which allows one to set awk variables from outside the awk program. So if you rewrite the line as
Code:
awk -vsearch=$a '$0 ~ search {print $2,$4}' file.txt

everything should work. And you're even saving on time since you don't have to run cat unnecessarily.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk: syntax error

awk -F, ' NR>1 { BEGIN{ chr=$2 } END{ for (k in chr) {print k} } } ' $gene_file I've a really simple code. I want to store gene name and it's chromosome and in the end print them. I'm skipping line one as it contains headers. But I don't know why I get error as: (2 Replies)
Discussion started by: genome
2 Replies

2. Shell Programming and Scripting

Help with the awk syntax

Hello Experts: While writing a script to help one of the posts on here, I end up writing a wrong one. I am very much eager to know how this can be corrected. Aim was to not print specified columns - lets say out of 100 fields, need to print all but 5th, 10th, 15th columns. Someone already... (13 Replies)
Discussion started by: juzz4fun
13 Replies

3. Shell Programming and Scripting

Syntax Problem with awk

Hello, I have perl script,which take some part of data in the file. the below command works fine in normal cmd prompt. `awk '/CDI/ && // && !/Result for/ {print $3 $5 > "final.txt"}' datalist.txt`; `nawk -F"" '{print $2}' finalcdi.txt`; But not working. Please use code tags, thanks. (5 Replies)
Discussion started by: rasingraj
5 Replies

4. Shell Programming and Scripting

AWK Function syntax

Hi, I would like to know what is the correct syntax to perform a function in awk. Although I have seen several examples, not get it to work, this is what I'm trying: #!/bin/bash awk function multi (number) { return number * 3 } print multi (4)Thanks (2 Replies)
Discussion started by: Godie
2 Replies

5. Shell Programming and Scripting

awk syntax

Hi I have a bash file which will split a big file to many small files. But I got a syntax error.H="$(head -1 CCC.tped)" awk 'print $0 > $1 ".tped"' CCC.tped for f in $(ls *.tped); do echo "$H\n" "$(cat $f)" >$f; done And -bash-4.1$ bash split awk: print $0 > $1".tped" awk: ^ syntax error... (3 Replies)
Discussion started by: zhshqzyc
3 Replies

6. AIX

Help with syntax using AWK

I have a file which is comma separated and has quotes. I can use this command and awk -F"," '{ if ($4=="01" print $0 }' test.txt But this doesn't fetch me the data.since it has quotes. If the data has no quotes,the above command works fine. In Unix you can skip quote \" but this doesn't work.... (7 Replies)
Discussion started by: ganesnar
7 Replies

7. Shell Programming and Scripting

AWK syntax help

i have a ksh code that needs to be written in AWK. can someone please help me here? :( if }" | grep -c "$2") -gt 0 ] ; then print - "found $2 in array ignore" else print - "did not find $2 in array ignore" fi ignore=4ty56r ignore=er45ty . . . ignore=frhtg2 (27 Replies)
Discussion started by: usustarr
27 Replies

8. Shell Programming and Scripting

AWK syntax

Hi I am trying to understand AWK syntax so I tried this command which gives me the home directory of root awk 'BEGIN { FS = ":"} {if ($1 == "root") print $6 }' /etc/passwd I would know what are the following commands doing. The first one prints all /etc/passwd, second prints nothing. ... (4 Replies)
Discussion started by: wakatana
4 Replies

9. Shell Programming and Scripting

awk syntax help

I don't get correct output when I run this command line: nmap -sP failedhost.com | grep -i failed | awk -F '{print $6}' I basically want it to return 'failedhost.com' but its just showing the output of the nmap scan. (8 Replies)
Discussion started by: streetfighter2
8 Replies

10. Shell Programming and Scripting

Help with Awk Syntax

I have written many awk commands which go in multiple lines. I have this confusion many times. Some time they work if i dont terminate them with "\" but some time error. Some time in "if" statements between if and else if i dont use ";" it gives error but sometimes it doesnt. The below... (4 Replies)
Discussion started by: pinnacle
4 Replies
Login or Register to Ask a Question