LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-11-2021, 08:43 AM   #1
FraMei
LQ Newbie
 
Registered: May 2021
Posts: 1

Rep: Reputation: Disabled
shell variable as search pattern in awk


Hello,

I have a textfile with multible lines seperatetd with tabs


X one two three
X one two threetest

Now I want to manipulate all lines which contains a "X" as first field and a shell variable as part of field 3


Code:
export pattern=test

cat test.txt  | awk 'BEGIN { FS=OFS="\t" } $1=="X" && $3~"$pattern"  {{ $1="R" } { $3="TEST2" }} { print }'
This works:

Code:
cat test.txt | awk 'BEGIN { FS=OFS="\t" } $1=="X" && $3~"test"  {{ $1="R" } { $3="TEST2" }} { print }'
Any clue?

Thanks

Frank
 
Old 05-11-2021, 09:02 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,490

Rep: Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532
probably:
Code:
awk 'BEGIN { FS=OFS="\t" } $1=="X" && $3~"'"$pattern"'"  {{ $1="R" } { $3="TEST2" }} { print }' test.txt
 
Old 05-11-2021, 09:22 AM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,611
Blog Entries: 3

Rep: Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859
You can pass the evnrionment variable into AWK for use within the script:

Code:
export pattern=test

awk -v pat=${pattern} 'BEGIN { FS=OFS="\t" } 
        $1=="X" && $4~pat  {$1="R"; $3="TEST2"} 
        { print }' test.txt
The AWK variable name can be the same as the shell variable, or not. The above is different just to be clear what is the AWK variable and what is the shell variable. Within the single quotes ' nothing from the shell will be parsed anyway.

Also, the pattern 'test' was in the 4th field, if I read correctly.
 
Old 05-11-2021, 09:25 AM   #4
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 1,339

Rep: Reputation: 1133Reputation: 1133Reputation: 1133Reputation: 1133Reputation: 1133Reputation: 1133Reputation: 1133Reputation: 1133Reputation: 1133

Whan Pan64 said. This isn't Awk-specific, and the general pattern is two single quoted strings with a double-quoted variable, no spaces between them, i.e: 'literal'"$variable"'literal'

However, there is an Awk-specific option if you prefer, using --assign var="$variable" (with -v shortform option).


Last edited by boughtonp; 05-11-2021 at 09:27 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
vi search multiple pattern and remove only exact matching pattern amateurscripter Linux - Newbie 4 05-07-2018 01:19 PM
[SOLVED] Pass search results to awk, and use awk output to search other files bspears1 Linux - Newbie 8 07-21-2012 09:17 AM
Search and replace Pattern preceeding another pattern nbkisnz Programming 3 05-13-2012 01:50 PM
[SOLVED] /bin/bash if statement pattern search, end of pattern special character? headhunter_unit23 Programming 3 04-29-2010 08:05 AM
AWK a variable Ouptut to a new variable and using the new variable with the old one alertroshannow Linux - Newbie 4 02-16-2009 12:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration