LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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-10-2013, 11:32 AM   #1
RudraB
Member
 
Registered: Mar 2007
Distribution: Fedora
Posts: 264

Rep: Reputation: 23
pattern matching with awk with variable


Hello friends,
I am trying to match a pattern like
Code:
species,subl,cmp=    1   2
as
Code:
  for (( j=1; j <= $c_subl; j++))
  do
    for (( k=1; k<=$c_comp; k++))
    do
      echo $k
      awk -v x=$j -v y=$k '/species,subl,cmp=    y   x/{print}' $i
    done
  done
but it just not working. If I use
Code:
awk -v x=$j -v y=$k '/species,subl,cmp=    1   2/{print}' $i
then everything is fine. but I need them to vary.
where I am going wrong, please?
 
Old 05-10-2013, 12:35 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Well as far as the awk part goes, everything between // is treated as a regular expression, hence your 'x' and 'y' items are simply x & y characters.
You will need to use computed regular expressions
However, looking at the code you may wish to rethink it altogether as I fail to see how the 2 loops are beneficial. Each to their own I guess
 
1 members found this post helpful.
Old 05-12-2013, 02:38 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
When a variable is used in a test expression it's contents are treated as a regex, without the need for /../ brackets. So I suggest modifying it like this:

Code:
for (( j=1; j <= $c_subl; j++ )) ;do

    for (( k=1; k<=$c_comp; k++ )); do

        pattern="species,subl,cmp=[[:blank:]]+$k[[:blank:]]+$j"

        awk -v "ss=$search" 'ss { print }' "$i"

    done

done
I agree with grail though that running multiple awk's in a double loop is quite inefficient. It seems to me that it would be better to build a list of all possible matching patterns first, then use a simple "grep -f" to extract them. For this I recommend a function and a process substitution.

Code:
genlist(){
    local cnt1 cnt2
    for (( cnt1=1; cnt1<=$1; cnt1++ )); do
        for (( cnt2=1; cnt2<=$2; cnt2++ )); do
            echo "species,subl,cmp=[[:blank:]]+$cnt1[[:blank:]]+$cnt2"
        done
    done
}

grep -E -f <( genlist "$c_comp" "$c_subl" ) "$infile"
I also recommend using variable names that more clearly describe their function, BTW. And don't forget to always quote them!


Actually, come to think of it, we could probably just use this single awk command instead:

Code:
awk -v "v1=$c_comp" -v "v2=$c_subl" '/species,subl,cmp/ && $2<=v1 && $3 <=v2 { print }' "$infile"

Last edited by David the H.; 05-12-2013 at 02:41 PM. Reason: addendum
 
1 members found this post helpful.
Old 05-12-2013, 03:52 PM   #4
RudraB
Member
 
Registered: Mar 2007
Distribution: Fedora
Posts: 264

Original Poster
Rep: Reputation: 23
Quote:
Originally Posted by David the H. View Post

Code:
awk -v "v1=$c_comp" -v "v2=$c_subl" '/species,subl,cmp/ && $2<=v1 && $3 <=v2 { print }' "$infile"
David, thanks! its really worked, and worked really fast.
 
  


Reply



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
[SOLVED] awk pattern matching between huge files? rare_aquatic_badger Programming 8 05-19-2012 06:43 AM
[SOLVED] awk with pipe delimited file (specific column matching and multiple pattern matching) lolmon Programming 4 08-31-2011 12:17 PM
[SOLVED] awk pattern matching philipz *BSD 1 05-05-2010 02:21 PM
Pattern matching in a text file - use of AWK?? wtaicken Programming 19 02-06-2009 05:54 PM
complicated pattern matching with awk or sed... alirezan1 Linux - Newbie 1 10-10-2008 06:45 PM

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

All times are GMT -5. The time now is 10:10 AM.

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