Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

sed - using regex and | need help

Shell Programming and Scripting


Tags
sed regex

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 12-25-2008   -   Original Discussion by TehOne
TehOne TehOne is offline
Registered User
 
Join Date: Oct 2008
Last Activity: 29 August 2012, 9:54 AM EDT
Posts: 82
Thanks: 0
Thanked 1 Time in 1 Post
Unix or Linux Question sed - using regex and | need help

From my understanding when using regex1|regex2 the matching process tries each alternative in turn, from left to right, and the first one that succeeds is used.
When im trying to extract the name from those examples:

A) name.can.be.different.20.03.2009.boom
B) name.can.be.different.20.03.09.boom
C) name_can_be_different_2009_boom

by using:


Code:
sed 's/\(.*\)[._]\([0-9][0-9].[0-9][0-9].[0-9][0-9][0-9][0-9]\|[0-9][0-9].[0-9][0-9].[0-9][0-9]\|[0-9][0-9][0-9][0-9]\)[._]\(.*\)/\1/'

It gives:

A) name.can.be.different.20.03 (WRONG)
B) name.can.be.different (GOOD)
C) name_can_be_different (GOOD)

Why does it fail on A) ? By my order of regex pattern it's suppose to match on
[0-9][0-9].[0-9][0-9].[0-9][0-9][0-9][0-9] but it doesn't ?
Sponsored Links
    #2  
Old Unix and Linux 12-26-2008   -   Original Discussion by TehOne
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
 
Join Date: Jun 2007
Last Activity: 11 November 2016, 3:44 AM EST
Location: Beijing China
Posts: 1,305
Thanks: 0
Thanked 26 Times in 26 Posts

Code:
sed 's/\([^0-9]*\)[._]\(.*\)/\1/' filename

Sponsored Links
    #3  
Old Unix and Linux 12-26-2008   -   Original Discussion by TehOne
Franklin52's Unix or Linux Image
Franklin52 Franklin52 is offline
Registered User
 
Join Date: Feb 2007
Last Activity: 14 October 2017, 3:52 AM EDT
Location: The Netherlands
Posts: 7,746
Thanks: 139
Thanked 558 Times in 520 Posts
Another approach with sed:


Code:
sed 's/[._][0-9].*//' file

Regards
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
regex clx Shell Programming and Scripting 3 10-13-2008 06:24 AM
Regex pppswing Shell Programming and Scripting 5 06-30-2008 11:22 PM
Regex deepakpv Shell Programming and Scripting 6 03-28-2007 05:18 AM
Regex?? Please help lunac UNIX for Dummies Questions & Answers 7 01-30-2007 01:13 PM
sed regex Shakey21 UNIX for Dummies Questions & Answers 4 01-31-2002 09:16 PM



All times are GMT -4. The time now is 05:30 PM.