Help with regex construction


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with regex construction
# 1  
Old 09-23-2010
Help with regex construction

I am trying to construct a regular expression that will filter a log file containing the following table:
Code:
  aP      [1,P1]
  mP      [3,P2] [4,P2(1)]
 mC,mI    [5,C2]
  oP      [16,P222] [17,P222(1)] [18,P2(1)2(1)2] [19,P2(1)2(1)2(1)]
  oC      [21,C222] [20,C222(1)]
  oF      [22,F222]
  oI      [23,I222] [24,I2(1)2(1)2(1)]
  tP      [75,P4] [76,P4(1)] [77,P4(2)] [78,P4(3)] [89,P422] [90,P42(1)2]
          [91,P4(1)22] [92,P4(1)2(1)2] [93,P4(2)22] [94,P4(2)2(1)2]
          [95,P4(3)22] [96,P4(3)2(1)2]
  tI      [79,I4] [80,I4(1)] [97,I422] [98,I4(1)22]
  hP      [143,P3] [144,P3(1)] [145,P3(2)] [149,P312] [150,P321] [151,P3(1)12]
          [152,P3(1)21] [153,P3(2)12] [154,P3(2)21] [168,P6] [169,P6(1)]
          [170,P6(5)] [171,P6(2)] [172,P6(4)] [173,P6(3)] [177,P622]
          [178,P6(1)22] [179,P6(5)22] [180,P6(2)22] [181,P6(4)22] [182,P6(3)22]
  hR      [146,R3] [155,R32]
  cP      [195,P23] [198,P2(1)3] [207,P432] [208,P4(2)32] [212,P4(3)32]
          [213,P4(1)32]
  cF      [196,F23] [209,F432] [210,F4(1)32]
  cI      [197,I23] [199,I2(1)3] [211,I432] [214,I4(1)32]

This basically consists of a list (in no particular order) of two strings, one consisting of number and the other a mixture of numbers, letters and (). The two strings are separated by a comma and are enclosed within [ ].

What I want to have is an awk/sed one-liner that will output the second string when the number string is specified. For example, if I search with 155 this will spit out R32.

At the moment I have this monstrosity:
Code:
awk /155/ | awk 'BEGIN{FS=" "}{for (i=1; i<=NF; i++) print $i}' | awk /155/ | cut -d "," -f 2 | cut -d "]" -f 1

which works for 155, but I cannot use it for 1, for example, where P1 would be the correct output.

Any help much appreciated!
# 2  
Old 09-23-2010
Code:
var=155
awk -v s=$var 's {gsub(/\[|\]/,"");for (i=1; i<=NF; i++) {split($i,a,","); if (a[1]==s) print a[2]} }' infile

This User Gave Thanks to rdcwayx For This Post:
# 3  
Old 09-23-2010
With sed:
Code:
sed -n '/\[155,/ s/.*155,\([^]]*\)].*/\1/p' file

This User Gave Thanks to Franklin52 For This Post:
# 4  
Old 09-23-2010
Great stuff! Thanks a lot.
# 5  
Old 09-23-2010
Code:
$ ruby -00 -ne 'BEGIN{num=155};a=$_.scan(/\[.*?\]/);p a.grep(/\[\b#{num}\b/)[0].split(/\[|\]|,/)[-1]' file
"R32"

$ ruby -00 -ne 'BEGIN{num=1};a=$_.scan(/\[.*?\]/);p a.grep(/\[\b#{num}\b/)[0].split(/\[|\]|,/)[-1]' file
"P1"

# 6  
Old 09-23-2010
And Perl:
Code:
# var=76                                                                          
# perl -lne 'if (/[^\]]+(\[[^\]]*\])\s+(\['"${var}"',([^\]]+)\])/) {print $3;}' infile

# 7  
Old 09-23-2010
Another one in Perl -

Code:
$
$
$ cat f20
  aP      [1,P1]
  mP      [3,P2] [4,P2(1)]
 mC,mI    [5,C2]
  oP      [16,P222] [17,P222(1)] [18,P2(1)2(1)2] [19,P2(1)2(1)2(1)]
  oC      [21,C222] [20,C222(1)]
  oF      [22,F222]
  oI      [23,I222] [24,I2(1)2(1)2(1)]
  tP      [75,P4] [76,P4(1)] [77,P4(2)] [78,P4(3)] [89,P422] [90,P42(1)2]
          [91,P4(1)22] [92,P4(1)2(1)2] [93,P4(2)22] [94,P4(2)2(1)2]
          [95,P4(3)22] [96,P4(3)2(1)2]
  tI      [79,I4] [80,I4(1)] [97,I422] [98,I4(1)22]
  hP      [143,P3] [144,P3(1)] [145,P3(2)] [149,P312] [150,P321] [151,P3(1)12]
          [152,P3(1)21] [153,P3(2)12] [154,P3(2)21] [168,P6] [169,P6(1)]
          [170,P6(5)] [171,P6(2)] [172,P6(4)] [173,P6(3)] [177,P622]
          [178,P6(1)22] [179,P6(5)22] [180,P6(2)22] [181,P6(4)22] [182,P6(3)22]
  hR      [146,R3] [155,R32]
  cP      [195,P23] [198,P2(1)3] [207,P432] [208,P4(2)32] [212,P4(3)32]
          [213,P4(1)32]
  cF      [196,F23] [209,F432] [210,F4(1)32]
  cI      [197,I23] [199,I2(1)3] [211,I432] [214,I4(1)32]
$
$ x=155
$ perl -lne "/$x,(.*?)]/ && print \$1" f20
R32
$
$ x=24
$ perl -lne "/$x,(.*?)]/ && print \$1" f20
I2(1)2(1)2(1)
$
$ x=19
$ perl -lne "/$x,(.*?)]/ && print \$1" f20
P2(1)2(1)2(1)
$
$

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sendmail K command regex: adding exclusion/negative lookahead to regex -a@MATCH

I'm trying to get some exclusions into our sendmail regular expression for the K command. The following configuration & regex works: LOCAL_CONFIG # Kcheckaddress regex -a@MATCH +<@+?\.++?\.(us|info|to|br|bid|cn|ru) LOCAL_RULESETS SLocal_check_mail # check address against various regex... (0 Replies)
Discussion started by: RobbieTheK
0 Replies

2. Shell Programming and Scripting

Grep command construction

Hi, I have an array variable "arr" that reads string from a file "vari.txt". Thus, the array will be of variable length depending how many entries are present in "vari.txt" I use a for loop to traverse through the array. However, i need a grep command to grep for a file "output.txt" and... (3 Replies)
Discussion started by: mohtashims
3 Replies

3. Web Development

Website construction nowaday!

Hello, Actually I am not sure how to ask this question, which is about the technique advance on website construction/design. Compared with HTML/CSS, which ones are the most popular for website construction? My colleague recommends Drupal for Linux platform. Any other? Thanks! Yifang (4 Replies)
Discussion started by: yifangt
4 Replies

4. Shell Programming and Scripting

case construction for basic Arithmetics calculation

the scrip (q4.sh) should perform the following calcuation (+, -, / and *) it should be used like this: q4.sh number1 operation number2 I wrote it already but the "*" does not work. #!/bin/bash #Date: 2010.10.19 # un script qui utilisera une instruction case pour effectuer des opérations... (6 Replies)
Discussion started by: flash80
6 Replies

5. Shell Programming and Scripting

Matrix construction

Hi I have to construct a rectangular matrix with 6012 rows and 2221 columns. Here the rows and columns were given by alphanumeric ids in a file named row.txt and column.txt respectively. with this row nd column ids I have to construct a matrix ie 6012*2221 and compare the column ids with... (0 Replies)
Discussion started by: riyabio
0 Replies

6. Shell Programming and Scripting

Matrix construction

Hi experts How to construct a rectangular matrix for a text file with 6012 rows and 2221 columns. Thank You (1 Reply)
Discussion started by: riyabio
1 Replies

7. Cybersecurity

Password Construction

Could someone please help, I am new to unix and I am trying to do the following: o Each password must have at least eight characters. Only the first eight characters are significant. PASSLENGTH is found in /etc/default/passwd and is set to 6. ... (1 Reply)
Discussion started by: tumbikikani
1 Replies
Login or Register to Ask a Question