Regular exp in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Regular exp in awk
# 1  
Old 08-23-2010
Regular exp in awk

Hi all,
One small doubt, reg exp in awk, is it possible to extract the match of regular expression like in perl?

eg:
B R16288 Map3_NoCat
B R16067 Map4_NoCat
B R16647 Map3_NoCat
B R16450 Map3_NoCat
B R16106 Map6_NoCat
B R16000 Map3_NoCat
B R16395 Map3_NoCat
B R16243 Map3_NoCat
B R16023 Map12_NoCat
B R16421 Map3_NoCat

if I wanna extract the digits after Map, in perl I could say

Code:
 /Map(\d*)/  print $1

is there any direct way in awk for it?
# 2  
Old 08-23-2010
awk:
Code:
awk -F"[ _]" '{sub(/Map/,"",$3); print $3}' infile

sed:
Code:
sed 's/.*Map\([0-9]\+\)_.*/\1/g' infile

# 3  
Old 08-23-2010
Code:
#!/bin/bash
while read -r line
do
   line=${line##*Map}
   echo ${line%_*}
done < myfile

# 4  
Old 08-23-2010
@zaxxon:
in your awk script
Code:
Code:
  awk -F"[ _]" '{sub(/Map/,"",$3); print $3}' infile

you r deleting the 'map', but what i want is to store the digits after map in order to do some logical test (say if the digit is > 5 then print the whole line)

Geo
# 5  
Old 08-23-2010
No, in traditional awk you can't do that, as awk doesn't support back references. If you use Gnu awk (gawk), you could use the gensub()-function, which supports back references. If you don't have gawk, use sed instead.

HTH

Chris
# 6  
Old 08-23-2010
@gvj

Yes, since that was what you asked for - if you have additional requests, please let it not sound as if there was missing someting in the answer to the original question, ty Smilie

Code:
awk -F"[ _]" '{a=$0; sub(/Map/,"",$3); if($3 > 5) print a}' infile
B R16106 Map6_NoCat
B R16023 Map12_NoCat


Last edited by zaxxon; 08-23-2010 at 07:19 AM..
# 7  
Old 08-23-2010
Quote:
Originally Posted by gvj
what i want is to store the digits after map in order to do some logical test (say if the digit is > 5 then print the whole line)

Geo
Another approach:
Code:
awk -F "Map|_" '$2 > 5' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to use regular exp to replace the character in the file?

Hi Unix Gurus, yesterday I asked a question and got answer, it works fine. I have one more thing need to help in the code awk '{print substr($0,1,3)"xxx"substr($0,7)}' file If I have 50 charactor's need to be replaced, is there any easy way to use reg exp or I have to input 50 XXXXx......... (12 Replies)
Discussion started by: ken6503
12 Replies

2. Shell Programming and Scripting

awk regular expression

Hello, I have big files which I wanna filter them based on first column. first column should be one of these strings: chr2L || chr2R || chr3L || chr3R || chr4 || chrX and something like chr2Lh or chrY or chrM3L is not accepted. I used the following command: awk '{ if ($1=="chr2L" ||... (5 Replies)
Discussion started by: @man
5 Replies

3. Shell Programming and Scripting

scripting/awk help : awk sum output is not comming in regular format. Pls advise.

Hi Experts, I am adding a column of numbers with awk , however not getting correct output: # awk '{sum+=$1} END {print sum}' datafile 2.15291e+06 How can I getthe output like : 2152910 Thank you.. # awk '{sum+=$1} END {print sum}' datafile 2.15079e+06 (3 Replies)
Discussion started by: rveri
3 Replies

4. Shell Programming and Scripting

Regular expression in AWK

Hello world, I was wondering if there is a nicer way to write the following code (in AWK): awk ' FNR==NR&&$1~/^m$/{tok1=1} FNR==NR&&$1~/^m10$/{tok1=1} ' my_file In fact, it looks for m2, m4, m6, m8 and m10 and then return a positive flag. The problem is how to define 10 thanks... (3 Replies)
Discussion started by: jolecanard
3 Replies

5. Shell Programming and Scripting

Awk regular expressions

Hi Experts, Can you please help me out for the below scenario, I have a variable length file with the fixed number of columns, in which the fields are delimited by pipe symbol (|). From that file I have to extract the lines which has the following scenario, The field1 in a... (1 Reply)
Discussion started by: chella
1 Replies

6. UNIX for Dummies Questions & Answers

extract a part of a path like (input: /etc/exp/home/bin ====> output: exp)

Hi, I need to make some extraction . with the following input to get the right output. input: /etc/exp/home/bin ====> output: exp and input: aex1234 ===> output: ex Thanks for your help, (4 Replies)
Discussion started by: yeclota
4 Replies

7. Shell Programming and Scripting

usage...sed/awk/reg-exp ..in shell scripting

in shell scripting there is extensive usage of i> regular expression ii>sed iii>awk can anyone tell me the suitable contexts ...i mean which one is suitable for what kind of operation. like the reg-exp and sed seems to be doing the same job..i.e pattern matching (1 Reply)
Discussion started by: mobydick
1 Replies

8. UNIX for Dummies Questions & Answers

regular expression and awk

I can print a line with an expression using this: awk '/regex/' I can print the line immediately before an expression using this: awk '/regex/{print x};{x=$0}' How do I print the line immediately before and then the line with the expression? (2 Replies)
Discussion started by: nickg
2 Replies

9. Shell Programming and Scripting

awk and regular expression

Ive got a file with words and also numbers. Bla BLA 10 10 11 29 12 89 13 35 And i need to change "10,29,89,25" and also remove anything that contains actually words... (4 Replies)
Discussion started by: maskot
4 Replies

10. Shell Programming and Scripting

Using reg. exp. variable in AWK??

Any idea please: How to pass a reg. exp. variable to awk call in a shell??? Thank u #!/bin/sh reg_exp=name awk '/reg_exp/{ print; }' $1 (5 Replies)
Discussion started by: andy2000
5 Replies
Login or Register to Ask a Question