how do i strip this line using perl regex.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how do i strip this line using perl regex.
# 1  
Old 03-18-2008
how do i strip this line using perl regex.

I have a variable dynamically generated
$batch = /dataload/R3P/interface/Bowne/reports/RDI00244.rpt

Now I'd like to strip '/dataload/R3P/interface/Bowne/reports/RDI' and '.rpt' from this variable

my output should be only 00244
how to do this using perl regex.I'm a newbie to perl and would like to know how to do this.

thanks and regards,
Ram.
# 2  
Old 03-18-2008
Hello...

There are a few ways to do this, but if what you want is ALWAYS the digits in the middle of the file name, then this is probably easiest...

First, at the top of your program, put "use File::Basename;" This is a standard module included with Perl... Basename gives you the filename at the end of a path... Suppose the whole path name is in $myfullpath

use File::Basename

$myFile = Basename($myfullpath); # $myFile now RDI00244.rpt

$myFile =~ /(\d)/; #The () around \d (digits) captures the match so...
$mydigits = $1 # $1 because it's the first parentheses set in the patern

or you could do....

$myFile =~ s/(\d)/$1/; # now a direct substitution of the digits into $myFile

Hope that helps
matthew rapaport (quine@sonic.net)
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

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

2. Shell Programming and Scripting

Perl regex to remove a segment in a line

Hello, ksh on Sun5.8 here. I have a pipe-delimited, variable length record file with sub-segments identified with a tilda that we receive from a source outside of our control. The records are huge, and Perl seems to be the only shell that can handle the huge lines. I am new to Perl, and am... (8 Replies)
Discussion started by: gary_w
8 Replies

3. Shell Programming and Scripting

How to strip '$' sign on the line

echo "dddd$dddd"|sed 's/$//' will return dddd echo "dddd$ddd"|tr -d '$' will return dddd I need to replace it with any other character or just live a blank space instead Thank you! (8 Replies)
Discussion started by: zam
8 Replies

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

5. Shell Programming and Scripting

perl regex multi line cut

hello mighty all there's a file with lots of comments.. some of them looks like: =comment blabla blablabla bla =cut i'm trying to cut this out completely with this code: $line=~s/^=.+?=cut//sg; but no luck also tryed to change it abit but still I don't understand how the... (9 Replies)
Discussion started by: tip78
9 Replies

6. Shell Programming and Scripting

Perl REGEX - How do extract a string in a line?

Hi Guys, In the following line: cn=portal.090710.191533.428571000,cn=groups,dc=mp,dc=rj,dc=gov,dc=br I need to extract this string: portal.090710.191533.428571000 As you can see this string always will be bettween "cn=" and "," strings. Someone know one regular expression to... (4 Replies)
Discussion started by: maverick-ski
4 Replies

7. Shell Programming and Scripting

Trying to strip some character in perl

I have some character strings and I would like to know how can I remove them using perl. My file contains the following output shown below. An example of a string in the file: + netmck(uid=500)-root + netmck(uid=500)-root + netmck(uid=500)-root + netmck(uid=0)-root . . . I would... (1 Reply)
Discussion started by: smlushing
1 Replies

8. Shell Programming and Scripting

Strip one line from 2 blank lines in a file

Hi Is there any command to scan thru a file looking for 2 consecutive blank lines and if any remove one of them. Please let me know. Regards, Tipsy (6 Replies)
Discussion started by: tipsy
6 Replies

9. Shell Programming and Scripting

strip first 4 and last 2 lines from a file using perl

Hi I have a file from which i need to remove the first 4 and the last 2 lines.. i know how to do it with sed but i need to do it in a perl script.. can you please help me how to do that. Thanks (10 Replies)
Discussion started by: meghana
10 Replies
Login or Register to Ask a Question