Scripting - How To Use A Variable Inside An Awk Statement - Unix & Linux Stack Exchange

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Join us in building a kind, collaborative learning community via our updated Code of Conduct.

Unix & Linux Stack Exchange is a question and answer site for
users of Linux, FreeBSD and other Un*x-like operating
systems. Join them; it only takes a minute:

Sign up

Here's how it works:


Anybody can ask a question
Anybody can answer
The best answers are voted up and rise to the top

How to use a variable inside an awk Ask Question

statement? [duplicate]

This question already has an answer


here:
Pass shell variable as a /pattern/
to awk 5 answers

I have a script like this

#!/bin/ksh
echo "Enter the matching pattern"
read pattern
path= /home/siva/
echo "Navigating to $path"
cd $path
cat filename|awk '/$pattern/ {for (i=1; i<=10; i++) {getline;print}}'

i am not able to get the entered


$pattern when i execute the script.
This site uses cookies to deliver our services and to show you relevant ads and job listings. By using our site, you
acknowledge that you
awkhave read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Your use of
scripting
Stack Overflow’s Products and Services, including the Stack Overflow Network, is subject to these policies and terms.
edited Nov 29 '16 at 13:07
fedorqui
3,892 2 18 53

asked Nov 29 '16 at 12:53


Siva
1

marked as duplicate by
steeldriver, Stéphane
Chazelas, dhag, techraf,
GAD3R Nov 29 '16 at 14:02
This question has been asked
before and already has an
answer. If those answers do not
fully address your question,
please ask a new question.

Try to format your question. – Kalavan


Nov 29 '16 at 13:06

2 You mean something like awk -v


pattern=$pattern '/$pattern/ {for
(i=1; i<=10; i++)
{getline;print}}' ? please try to
edit your code so it's readable –
Dani_l Nov 29 '16 at 13:06

1 @Dani_l no, this would look for a


literal $pattern . Instead, you have
to say $0 ~ pattern . In general, you
cannot use variables within / /
because awk does not have a way to
distinguish them from literal text. –
fedorqui Nov 29 '16 at 13:08

2 Answers

echo | awk -v variable='This is variab

You pass variables with -v keyword.


And don't use $ for variable - it is not
a bash. Awk uses $ to access a field.

edited Nov 29 '16 at 16:18

answered Nov 29 '16 at 13:06


Kalavan
This site uses cookies to deliver our services
536 2 and
8 to show you relevant ads and job listings. By using our site, you
acknowledge that you have read and understand our , , and our . Your use of
Stack Overflow’s Products
I sawand
yourServices, including
answer after the Stack Overflow Network, is subject to these policies and terms.
already
posting my comment – Dani_l Nov 29
'16 at 13:07

1 Note that some awk implementations


need the variabl=... to be a
separate argument from -v , so awk
-v variable=... is more portable
than awk -vvariable=... . –
Stéphane Chazelas Nov 29 '16 at
14:37

#!/bin/ksh
echo "Enter the matching pattern"
IFS= read -r pattern
path=/home/siva/
echo "Navigating to $path"
cd "$path" || exit
awk -v pattern="$pattern" '$0 ~ patter

1. Use awk -v to pass variables into


the awk script
2. cat is unnecessary - awk can
handle files directly

edited Nov 29 '16 at 13:30


Stéphane Chazelas
281k 53 518 849

answered Nov 29 '16 at 13:09


Dani_l
2,997 9 28

awk -v pattern=$pattern '/$0 ~ pattern/


{for (i=1; i<=10; i++) {getline;print}}'
filename' - This doesn't work, – Siva
Nov 29 '16 at 13:19

Try without the slashes, as per my last


edit – Dani_l Nov 29 '16 at 13:23

Dani... Perfect. That worked!! – Siva


Nov 29 '16 at 13:24

1 Using -v mangles backslash


characters, using environment
variables and
ENVIRON["the_variable"] in awk
is a better approach considering that
regular expressions often contain
This site uses cookiesbackslash
to deliver characters
our services
– and to show you relevant ads and job listings. By using our site, you
Stéphane Chazelas Nov
acknowledge that you have read and understand 29our
'16 at , , and our . Your use of
13:31
Stack Overflow’s Products and Services, including the Stack Overflow Network, is subject to these policies and terms.
2 a=aaa defines a shell variable. You
need to export it if you want it passed
as an environment variable to awk ,
or use a=aaaa awk 'BEGIN{print
ENVIRON["a"]}' . See the dup
question for details. –
Stéphane Chazelas Nov 29 '16 at
14:28

This site uses cookies to deliver our services and to show you relevant ads and job listings. By using our site, you
acknowledge that you have read and understand our , , and our . Your use of
Stack Overflow’s Products and Services, including the Stack Overflow Network, is subject to these policies and terms.

You might also like