Regex extraction


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Regex extraction
# 1  
Old 06-19-2008
Regex extraction

Hello,

I need your help to extract text from following:

./sherg_fyd_rur:blkabl="R23.21_BL2008_0122_1"
./serge_a75:rlwual="/main/r23.21=26-Mar-2008.05:00:20UTC@R11.31_BL2008_0325"
./serge_a75:blkabl="R23.21_BL2008_0325"
./sherg_proto_npiv:bkguals="R23.21_BL2008_0302 I80_11.31_LR"


I need to extract the text starting after ./ up to : (example from last line "sherg_proto_npiv") and the date only after BL (from example last line 2008_0302).

Any help is greatly appreciated.

Thank you.

abrouf
# 2  
Old 06-19-2008
I beleive in Perl it would be

if (/\.\/(.+):.*BL(.*)/) {
$mytext = $1;
$mydate = $2;
}


in sed the syntax will be slightly different:
/\.\/(.+):.*BL(.*)/p $1 $2
but this I did not check on a computer. Let me know if it does not work and I will fix it.
# 3  
Old 06-19-2008
Regex extraction

Hello odashe,

Thank you for your quick response. I chose sed and ran the script, but it's returning with a parse error as follows:

cat <filename> | sed 's/\.\/(.+):.*BL(.*)/p' $1 $2
sed: Function s/\.\/(.+):.*BL(.*)/p cannot be parsed.

Actually I'm new in scripting, could you please explain the meaning of those?

Thanks again.

abrouf
# 4  
Old 06-19-2008
Regex extraction

I also used perl, but getting the following errors when using debugging mode:

perl -d regex1.pl < reg.txt
Default die handler restored.

Loading DB routines from perl5db.pl version 1.07
Editor support available.

Enter h or `h h' for help, or `man perldebug' for more help.

main:Smilieregex1.pl:5): if (/\.\/(.+):.*BL(.*)/) {
DB<1>

reg.txt is the input file.

Without debugging mode it returns nothing.

Any advise would be greatly appreciated.

abrouf
# 5  
Old 06-20-2008
Here is a sed solution based on multiple back references.
Code:
$ sed -e 's/\(^\.\/\)\(.*\)\(:.*=".*_BL\)\(...._....\)\(.*\)/\2 \4/' file
sherg_fyd_rur 2008_0122
serge_a75 2008_0325
serge_a75 2008_0325
sherg_proto_npiv 2008_0302

# 6  
Old 06-20-2008
Regex extraction

Hello fpmurphy,

It worked Smilie, I really appreciate your help and this will encourage me to be a script expert.
Is it possible to explain few outlines of syntax, I'm also trying to understand and trying to find out those from sed document.

Thank you.
abrouf
# 7  
Old 06-20-2008
The following samle script may help you understand back the regular expression used to produce the output you wanted.
Code:
#!/usr/bin/ksh

TMP=file.$$

cat <<EOT >$TMP
./sherg_fyd_rur:blkabl="R23.21_BL2008_0122_1"
./serge_a75:rlwual="/main/r23.21=26-Mar-2008.05:00:20UTC@R11.31_BL2008_0325"
./serge_a75:blkabl="R23.21_BL2008_0325"
./sherg_proto_npiv:bkguals="R23.21_BL2008_0302 I80_11.31_LR"
EOT

print "Stage 1"
sed -e 's/\(^\.\/\)//' $TMP
print "Stage 2"
sed -e 's/\(^\.\/\)\(.*\)\(:.*=\)/\2 /' $TMP
print "Stage 3"
sed -e 's/\(^\.\/\)\(.*\)\(:.*=".*_BL*\)/\2 /' $TMP
print "Stage 4"
sed -e 's/\(^\.\/\)\(.*\)\(:.*=".*_BL\)\(...._....\)\(.*\)/\2 \4/' $TMP

rm $TMP

exit 0

Login or Register to Ask a Question

Previous Thread | Next Thread

10 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

Sequence extraction

i want to extract specific region of interest from big file. i have only start position, end position and seq id, see my query is: I have file1 is this >GL3482.1 GAACTTGAGATCCGGGGA GCAGTGGATCTCCACCAG CGGCCAGAACTGGTGCAC CTCCAGGCCAGCCTCGTC CTGCGTGTC >GL3550.1... (14 Replies)
Discussion started by: harpreetmanku04
14 Replies

3. Shell Programming and Scripting

Perl, RegEx - Help me to understand the regex!

I am not a big expert in regex and have just little understanding of that language. Could you help me to understand the regular Perl expression: ^(?!if\b|else\b|while\b|)(?:+?\s+){1,6}(+\s*)\(*\) *?(?:^*;?+){0,10}\{ ------ This is regex to select functions from a C/C++ source and defined in... (2 Replies)
Discussion started by: alex_5161
2 Replies

4. UNIX for Dummies Questions & Answers

read regex from ID file, print regex and line below from source file

I have a file of protein sequences with headers (my source file). Based on a list of IDs (which are included in some of the headers), I'd like to print out only the specified sequences, with only the ID as header. In other words, I'd like to search source.txt for the terms in IDs.txt, and print... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

5. Shell Programming and Scripting

extraction

I have following input @xxxxxx@ I want to extract what's between @....@ that is : xxxx using SED command (6 Replies)
Discussion started by: xerox
6 Replies

6. Shell Programming and Scripting

Converting perl regex to sed regex

I am having trouble parsing rpm filenames in a shell script.. I found a snippet of perl code that will perform the task but I really don't have time to rewrite the entire script in perl. I cannot for the life of me convert this code into something sed-friendly: if ($rpm =~ /(*)-(*)-(*)\.(.*)/)... (1 Reply)
Discussion started by: suntzu
1 Replies

7. UNIX for Dummies Questions & Answers

Extraction of Filename

HI, i would like to ask for your help. how will i be able to extract part of the filename? FILENAME: 000_20071222083029135_evPDSN02.CANCEL i want to get only 000_20071222083029135.CANCEL Thanks and Good Day! :) (2 Replies)
Discussion started by: drco29
2 Replies

8. Programming

extraction from a path

Hi, Can you help me on this two problems? how can i get : from input: /ect/exp/hom/bin ==> output: exp and from input: aex1234 =====>output: ex thanks, (1 Reply)
Discussion started by: yeclota
1 Replies

9. Shell Programming and Scripting

AWK extraction

Hi all, Can anyone please help me in parsing the following file. Suppose the file is called, example.lst, and has the following content in it. (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP) (Host = 192.168.2.2) ... (3 Replies)
Discussion started by: harris2107
3 Replies

10. UNIX for Dummies Questions & Answers

help on file extraction

Hello, Im trying to extract a portion of a big file. Using unique pattern /occurrence , (ex. loginname1,logoff and loginname2,logoff ), I like to print the lines that contain the patterns and the lines between them. Also, create a file for every login occurrence. Thanks for everyone's... (1 Reply)
Discussion started by: apalex
1 Replies
Login or Register to Ask a Question