Visit Our UNIX and Linux User Community


combining perl regex'es into a single command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting combining perl regex'es into a single command
# 1  
Old 09-23-2009
combining perl regex'es into a single command

Hi Gurus, I have a working solution for munging my data but just wondered if there was a way I could streamline it into a single command at all ?

Code:
my $filesystem = "backup/server56/oracle/"


$filesystem =~ s/\/+$// ;              # remove the trailing slash(es) from the path specified
$filesystem =~ s/^backup\/// ;             # remove the "backup/"  from the path specified
chomp ($filesystem);                   # remove new line (if any)

this would leave $filesystem looking like this
Code:
server56/oracle


Is there any way of getting those three lines into one ?

any help on this would be great
# 2  
Old 09-23-2009
Code:
$ perl -we '$filesystem = "backup/server56/oracle/"; $filesystem=~s#.+/(\w+?/\w+?)/?$#$1#; print $filesystem,"\n";'
server56/oracle

# 3  
Old 09-23-2009
thank you pludi , im not sure i fully understand whats going on here but at a guess

Code:
$filesystem=~s#.+/(\w+?/\w+?)/?$#$1#;

a) We are using hashes as delimiters

b) The .+/ would match more than one character except for a newline up to the first forward slash, i guess I am ok to put ^ in front of the dot to ensure we are matching against the beginning of the string ??

c) The bracket section is matching 1 or more word then slash then 1 or more words again ??? (to be honest im a little confused why this is in brackets)

d) The /?$ bit is matching against 0 or 1 instances of "/" at the end of the string .... I have changed this to /*$ just in case somebody puts more than one slash at the end

e) I really have no idea why there is a $1 in the 'substitute to' section of the statement ....what does this signify ?, I assume it must be a variable as opposed to a regex because wouldnt it replace the whole line with a number 1 ?? im very confused over how that bit works



Additionally, i tried adding a newline after the original string (\n) and your statement didnt remove it. Is it the case that I would still have to run a chomp on it afterwards or can that be integrated too ?

---------- Post updated at 02:50 PM ---------- Previous update was at 12:22 PM ----------

aha, figured it out ... the $1 is a back-reference and effectively whatever is matched between the parentheses in the "substitute-from" section, will populate the "substitute-to" section.. took me a while to get my head around but now i understand


thanks pludi
# 4  
Old 09-23-2009
Quote:
Originally Posted by hcclnoodles
Code:
$filesystem=~s#.+/(\w+?/\w+?)/?$#$1#;

a) We are using hashes as delimiters
Correct, keeps you from escaping every forward slash (First virtue of a programmer: lazyness).
Quote:
Originally Posted by hcclnoodles

b) The .+/ would match more than one character except for a newline up to the first forward slash, i guess I am ok to put ^ in front of the dot to ensure we are matching against the beginning of the string ??
Almost. '.+' means any character, one or more times, with greedy matching. Meaning "as much as possible", so for most strings you can leave the caret out.
Quote:
Originally Posted by hcclnoodles
c) The bracket section is matching 1 or more word then slash then 1 or more words again ??? (to be honest im a little confused why this is in brackets)
\w means any word character (alphanumeric + '_'), which is matched one or more times with non-greedy matching (aka 'as few as possible'), so it matches only up to the next forward slash. Repeat for the part after the slash.
Quote:
Originally Posted by hcclnoodles

d) The /?$ bit is matching against 0 or 1 instances of "/" at the end of the string .... I have changed this to /*$ just in case somebody puts more than one slash at the end
Correct

Quote:
Originally Posted by hcclnoodles
e) I really have no idea why there is a $1 in the 'substitute to' section of the statement ....what does this signify ?, I assume it must be a variable as opposed to a regex because wouldnt it replace the whole line with a number 1 ?? im very confused over how that bit works
$1 holds the contents of the first capturing group from the last regular expression. Anything between '(' and the matching ')' is a capturing group (can be nested, too). Example:
Code:
$var = "Hello World";
$var =~ /(\w+) (\w+)/;

$1 would be "Hello", and $2 would be "World" (sans the quotes).

Quote:
Originally Posted by hcclnoodles
Additionally, i tried adding a newline after the original string (\n) and your statement didnt remove it. Is it the case that I would still have to run a chomp on it afterwards or can that be integrated too ?
Best bet would be chomp. It's possible to match the newline inside the regex using the 'm' and 's' switches, but my personal preference is chomp, as it's intent is more clear.
# 5  
Old 09-23-2009
thank you for the time and effort responding my post pludi. As usual, thoroughly informative..

Not sure if you saw my edit to the post directly above your last one, but i just managed to figure out the back reference in parentheseis stuff before you posted which is great, but you have clarified the overall picture for me

thanks again


ps: im not sure what they do, but i have awarded you 50,000 bits (not sure if thats good or bad)

Previous Thread | Next Thread
Test Your Knowledge in Computers #683
Difficulty: Medium
According to both NetMarketShare and WikiMedia, Safari has a desktop market share of over 6% in 2019.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Log4j combining lines to single line

Hi, Our log4j file contents look like this: 2018-11-20T00:06:58,888 INFO ql.Driver: Executing command(queryId=hive_20181120000656_49af4ad0-1d37-4312-872c-a247ed80c181): CREATE TABLE RESULTS.E7014485_ALL_HMS_CAP1 AS SELECT name,dept from employee Where employee='Jeff'... (4 Replies)
Discussion started by: wahi80
4 Replies

2. Shell Programming and Scripting

Combining lines into a single line

i have a file (where the column values are separated by ' and the text can be enclosed in ~) which contains data in form of 4461,2,~Basic: 2 Years/Unlimited Miles Drivetrain: Gas Engine 2 Years/Unlimited Miles Duramax Engine 3 Years/Unlimited... (2 Replies)
Discussion started by: rahulchandak
2 Replies

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

4. Shell Programming and Scripting

How to add line breaks to perl command with large text in single quotes?

Below code extracts multiple field values from XML into array and prints all in one line. perl -nle '@r=/(?: jndiName| authDataAlias| value| minConnections| maxConnections| connectionTimeout| name)="(+)/g and print join ",",$ENV{tIPnSCOPE},$ENV{pr ovider},$ENV{impClassName},@r' server.xml ... (4 Replies)
Discussion started by: kchinnam
4 Replies

5. Shell Programming and Scripting

Combining two scripts into a single script

Hi Folks, I have two scripts that are used to start and stop services these scripts are at the location /opt/app/tre , so that start.sh internally starts the components and stop.sh internally stop all the components, now rite now if I have to stop the services then i need to go first the... (9 Replies)
Discussion started by: punpun66
9 Replies

6. Shell Programming and Scripting

Combining columns from multiple files into one single output file

Hi, I have 3 files with one column value as shown File: a.txt ------------ Data_a1 Data_a2 File2: b.txt ------------ Data_b1 Data_b2 Data_b3 Data_b4 File3: c.txt ------------ Data_c1 Data_c2 Data_c3 Data_c4 Data_c5 (6 Replies)
Discussion started by: vfrg
6 Replies

7. Programming

PERL:Combining multiple lines to single line

Hi All I need a small help for the below format in making a small script in Perl or Shell. I have a file in which a single line entries are broken into three line entries. Eg: I have a pen and notebook. All i want is to capture in a single line in a separate file. eg: I have a pen and... (4 Replies)
Discussion started by: Kalaiela
4 Replies

8. Shell Programming and Scripting

Combining multiple rows in single row based on certain condition using awk or sed

Hi, I'm using AIX(ksh shell). > cat temp.txt "a","b",0 "c",bc",0 "a1","b1",0 "cc","cb",1 "cc","b2",1 "bb","bc",2 I want the output as: "a","b","c","bc","a1","b1" "cc","cb","cc","b2" "bb","bc" I want to combine multiple lines into single line where third column is same. Is... (1 Reply)
Discussion started by: samuelray
1 Replies

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

10. Shell Programming and Scripting

combine two perl lines into a single perl command

Hi Everyone, i have a string 00:44:40 so: $tmp=~ s/://gi; $tmp=~s/({2})({2})({2})/$1*3600+$2*60+$3/e; the output is 2680. Any way to combine this two lines into a single line? Thanks (4 Replies)
Discussion started by: jimmy_y
4 Replies

Featured Tech Videos