6

I have a text file which I want to filter using awk. The text file looks like this:

foo 1
bar 2
bar 0.3
bar 100
qux 1033

I want to filter those files with awk inside a bash script.

#!/bin/bash

#input file
input=myfile.txt

# I need to pass this as parameter
# cos later I want to make it more general like
# coltype=$1
col1type="foo"   

#Filters
awk '$2>0 && $1==$col1type' $input

But somehow it failed. What's the right way to do it?

4 Answers 4

12

pass it in using -v option of awk. that way, you separate out awk variables and shell variables. Its neater also without extra quoting.

#!/bin/bash

#input file
input=myfile.txt

# I need to pass this as parameter
# cos later I want to make it more general like
# coltype=$1
col1type="foo"   

#Filters
awk -vcoltype="$col1type" '$2>0 && $1==col1type' $input
Sign up to request clarification or add additional context in comments.

2 Comments

The '-v' notation is POSIX compliant; older (System V-ish) versions of awk may also allow parameter=value without the '-v' option. It is better to be explicit - use the '-v' unless there's a problem with it on your system.
If you do use the bare parameter=value syntax (without the -v prefix), it has to go between the '$2>0 ...' and the $input arguments. But I doubt there are many systems out there anymore that don't accept -v, and it is best to use it when possible. Also, this method is a bit more robust that John Kugelman's answer: what if coltype held the value xyz{next} {print "garbage"}?
6

"double quote single quote"

awk '{print "'$1'"}'


example:

$./a.sh arg1 
arg1


$cat a.sh 
echo "test" | awk '{print "'$1'"}'


linux tested

1 Comment

Thanks! I'm kind of newbee, searching for hours how to simply import shell variable string to (a bit complex) awk expression.
5

You need double quotes to allow variable interpolation, which means you then need to escape the other dollar signs with backslashes so $1 and $2 are not interpolated. Also you need double quotes around "$col1type".

awk "\$2>0 && \$1==\"$col1type\"" 

Comments

2

Single quotes inhibit variable expansion in bash:

awk '$2>0 && $1=='"$col1type"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.