regex - display all occurrences of match


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers regex - display all occurrences of match
# 15  
Old 02-16-2009
Thanks.. that helped. I was running from the same directory where the script was and didn't realize the need for '.'

Quote:
Originally Posted by vgersh99
how do you run the script?
Do you have '.' in your PATH?
Specify an absolute/relative path to the script.
# 16  
Old 02-16-2009
How do I get a regexp to match all these strings containing 'ABCD.' Basically I know the pattern I need to match ( in this case 'ABCD.'). I am new to this and little confused by how to enforce '.' in the pattern

IN real world, I am trying to get all the table names by parsing a log file. I know the schema name which is ABCD; want to get all the strings which are ABCD.anycombination of alphanumeric chars including '_'

For eg:

ABCD.aAbcd_ayNg_kkk_kkk
ABCD.aAbcd_ayNg_kkk_kkk_MMM
ABCD.aAbcd_xyz_kkk_kkk
ABCD.bAccd_axyz_kkk
ABCD.aAbcd_ayNg_kkk_kkk




Quote:
Originally Posted by tektips
Thanks.. that helped. I was running from the same directory where the script was and didn't realize the need for '.'
# 17  
Old 02-16-2009
assuming tec.txt:
Code:
aaaaaaaaaa ABCD_EFGH_XYZ.Table_Name1 bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
bbbbbbbbbb ABCD_EFGH_XYZ.Table_Name1 cccccccccccccccccccccccccccccccccccccccc
cccccccccccccccccccccccccccccccccc ABCD_EFGH_XYZ.Table_Name2 ddddddddddddddd
aaaaaaaaaa ABCD_XYZ_EFGH.Table_Name1 bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
aaaaaaaaaa ABCD_XYZ_1234.Table_Name1 bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
bbbbbbbbbb ABCD_EFGH_XYZ.ORDER_SUMM cccccccccccccccccccccccccccccccccccccccc
bbbbbbbbbb ABCD_EFGH_XYZ.PAYMENT_DETAIL cccccccccccccccccccccccccccccccccccccccc
bbbbbbbbbb ABCD_EFGH_XYZ.ORDER_SUMM cccccccccccccccccccccccccccccccccccccccc

and previously posted test.awk:
Code:
#!/usr/bin/nawk -f
{
    while (match($0, pat)) {
       printf("%s\n", substr($0, RSTART, RLENGTH) OFS);
       $0=substr($0, RSTART+RLENGTH)
    }
}

test.awk -v pat='ABCD[A-Za-z_.][A-Za-z_.]*' tec.txt
# 18  
Old 02-17-2009
Thanks vgersh99 for all your help.

Tried this out and For me, catch with this is it does not yield be the table name ( string after the period).

When I run the script on my test file, my result set is

ABCD_PL_OFR_TB.
ABCD_PROBE_TB.
ABCD_PROHIT_TB.
ABCD_RETMAIL_TB.
ABCD_RWD_TB.
ABCD_SOL_TB.
ABCD_STG_TB.
ABCD_STMT_AGGN_TB.

What I am actually looking to get is

ABCD_PL_OFR_TB.TABLE_A_B
ABCD_PROBE_TB.TABLE_B
ABCD_PROHIT_TB.TABLE_C_K
ABCD_RETMAIL_TB.TABLE_D
ABCD_RWD_TB.TABLE_E_J_H
ABCD_SOL_TB.TABLE_F_N
ABCD_STG_TB.TABLE_G_H_K_L
ABCD_STMT_AGGN_TB.TABLE_H

Also I want the script to be able to capture instances of strings like ABCD.TABLE_A_B_C as well. ( ABCD followed by a period and a Table Name)

Would appreciate your guidance





Quote:
Originally Posted by vgersh99
assuming tec.txt:
Code:
aaaaaaaaaa ABCD_EFGH_XYZ.Table_Name1 bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
bbbbbbbbbb ABCD_EFGH_XYZ.Table_Name1 cccccccccccccccccccccccccccccccccccccccc
cccccccccccccccccccccccccccccccccc ABCD_EFGH_XYZ.Table_Name2 ddddddddddddddd
aaaaaaaaaa ABCD_XYZ_EFGH.Table_Name1 bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
aaaaaaaaaa ABCD_XYZ_1234.Table_Name1 bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
bbbbbbbbbb ABCD_EFGH_XYZ.ORDER_SUMM cccccccccccccccccccccccccccccccccccccccc
bbbbbbbbbb ABCD_EFGH_XYZ.PAYMENT_DETAIL cccccccccccccccccccccccccccccccccccccccc
bbbbbbbbbb ABCD_EFGH_XYZ.ORDER_SUMM cccccccccccccccccccccccccccccccccccccccc

and previously posted test.awk:
Code:
#!/usr/bin/nawk -f
{
    while (match($0, pat)) {
       printf("%s\n", substr($0, RSTART, RLENGTH) OFS);
       $0=substr($0, RSTART+RLENGTH)
    }
}

test.awk -v pat='ABCD[A-Za-z_.][A-Za-z_.]*' tec.txt
# 19  
Old 02-17-2009
assuming tec.txt1:
Code:
aaaaaaaaaaaaaaaaa ABCD_PL_OFR_TB.TABLE_A_B bbbbbbbbb  bbbbbbbbb
cccccccccccc c c c  cccccc ABCD_PROBE_TB.TABLE_B xxxxxxxxxxxxx xxxxxxxxxx
ia b c sdlkjfalskdjalsdj ABCD_PROHIT_TB.TABLE_C_K asd asd
asdf   asdf    ABCD_RETMAIL_TB.TABLE_D x v
iasdf as df ABCD_RWD_TB.TABLE_E_J_H
asdf    asdf    asdf ABCD_SOL_TB.TABLE_F_N asdkfj
asdf asdf  ABCD_STG_TB.TABLE_G_H_K_L asdfasdf
 asdf asdf   ABCD_STMT_AGGN_TB.TABLE_H asd

executing:
Code:
test.awk -v pat='ABCD[A-Za-z_.][A-Za-z_.]*' tec.txt1

yeilds:
Code:
ABCD_PL_OFR_TB.TABLE_A_B
ABCD_PROBE_TB.TABLE_B
ABCD_PROHIT_TB.TABLE_C_K
ABCD_RETMAIL_TB.TABLE_D
ABCD_RWD_TB.TABLE_E_J_H
ABCD_SOL_TB.TABLE_F_N
ABCD_STG_TB.TABLE_G_H_K_L
ABCD_STMT_AGGN_TB.TABLE_H

Looks fine to me.
What is your data file? Please use BB Codes when posting code/data samples.

Also, if your desired pattern requirement changes, you'll have to adjust the value of the variable 'pat' passed into the script from the command line.

You'll have to understand the solution and be able to adjust it based on your potentially changing requirements.

Good luck.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count the number of string occurrences to display 0 entries in output

Hello Friends, Can somebody assist an issue I am having? I have a separate file with a list of account ids XXX200B02Y01 XXX200B03Y01 XXX200B05Y01 XXX200B07Y01 XXX200B08Y01 I call the file, and run an egrep against a directory and logfiles AccountID=$(cat... (2 Replies)
Discussion started by: liketheshell
2 Replies

2. 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

3. Shell Programming and Scripting

Display match or no match and write a text file to a directory

The below bash connects to a site, downloads a file, searches that file based of user input - could be multiple (all that seems to work). What I am not able to figure out is how to display on the screen match found or no match found" and write a file to a directory (C:\Users\cmccabe\Desktop\wget)... (4 Replies)
Discussion started by: cmccabe
4 Replies

4. UNIX for Advanced & Expert Users

sed REGEX to print multiple occurrences of a pattern from a line

I have a line that I need to parse through and extract a pattern that occurs multiple times in it. Example line: getInfoCall: info received please proceed, getInfoCall: info received please proceed, getInfoCall: info received please proceed, getInfoCall: info received please proceed,... (4 Replies)
Discussion started by: Vidhyaprakash
4 Replies

5. Shell Programming and Scripting

Print occurrences for pattern match

Hi All, I want to print all the occurrences for a particular pattern from a file. The catch is that the pattern search is partial and if any word in the file contains the pattern, that complete word has to be printed. If there are multiple words matching the pattern on a specific line, then all... (2 Replies)
Discussion started by: decci_7
2 Replies

6. Shell Programming and Scripting

Only Regex pattern match help

Hi We have a tool to monitor logs in our environment. The tool accepts log pattern match only using regex and I accept I am a n00b in that:confused:. I had been banging my head to make it work without much success and at last had to turn on to my last option to post it here. I had got great... (2 Replies)
Discussion started by: radioactive9
2 Replies

7. Shell Programming and Scripting

Regex find first 5-7 occurrences of a set of digits within a string

Using these strings as an example: <a onclick="doShowCHys=1;ShowWindowN(0,'/daman/man.php?asv4=145148&amp;playTogether=True',960,540,943437);return false;" title=""> <a onclick="doShowCHys=1;ShowWindowN(0,'/daman/man.php?asv4=1451486&amp;playTogether=True',960,540,94343);return false;" title=""> <a... (12 Replies)
Discussion started by: metallica1973
12 Replies

8. UNIX for Dummies Questions & Answers

awk display the match and 2 lines after the match is found.

Hello, can someone help me how to find a word and 2 lines after it and then send the output to another file. For example, here is myfile1.txt. I want to search for "Error" and 2 lines below it and send it to myfile2.txt I tried with grep -A but it's not supported on my system. I tried with awk,... (4 Replies)
Discussion started by: eurouno
4 Replies

9. UNIX for Advanced & Expert Users

Regex to match IP address

What do you think of this regex to match IP address? I have been reading up on regex and have seen some really long ones for IP. Would this fail in any scenarios? (+\.){3}* (5 Replies)
Discussion started by: glev2005
5 Replies

10. Shell Programming and Scripting

regex to match basename

Hi Can somebody please help me know how do i match the basename using a regular expression using posix standard in shell script suppose i want to match /u01/Sybase/data/master.dbf the result should be master.dbf as i want to match everything after the last / regards (8 Replies)
Discussion started by: xiamin
8 Replies
Login or Register to Ask a Question