Useing bash variable with awk

 

We can use bash variable with awk script/command.

First define required variables:

VAR1="india"

VAR2="putin"

My input file names.txt with following content:

uk
us
india
obama
putin
vatican

I will use my previous posts example to print text between two patterns, so I will get text from india to putin with following awk expression uing variables VAR1 & VAR2 that are defined above:

$ awk "/$VAR1/,/$VAR2/ { print }" names.txt

or

$ awk "/${VAR1}/,/${VAR2}/ { print }" names.txt

Output:

For both command we will get same output

india
obama
putin

Similarly you can use bash variable with sed also. For more details refer url where I explained with example.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s