Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 05-18-2012
Registered User
 
Join Date: May 2012
Posts: 12
Thanks: 7
Thanked 0 Times in 0 Posts
Help with sed regular expression

Hi all,

I want to get a substring from a string based on given delimiter, for example:

Code:
str="foo|bar|baz"

with delimiter "|",
I want to get one substring at each time with the order number the substring in the whole string,
given 1 to get "foo",
given 2 to get "bar",
given 3 to get "baz",
I guess it could be done with sed and an regular expression, but I don't know how to write it.

Thanks.
Roy987
Sponsored Links
    #2  
Old 05-18-2012
michaelrozar17's Avatar
Registered User
 
Join Date: Jul 2010
Location: IN
Posts: 879
Thanks: 13
Thanked 180 Times in 171 Posts
Command cut would do the same job

Code:
echo $str | cut -d'|' -f1 # try -f2 or -f3

The Following User Says Thank You to michaelrozar17 For This Useful Post:
Roy987 (05-18-2012)
Sponsored Links
    #3  
Old 05-18-2012
Registered User
 
Join Date: May 2012
Posts: 12
Thanks: 7
Thanked 0 Times in 0 Posts
Thanks.
It worked.
Sponsored Links
Closed Thread

Tags
regular expression, sed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Perl: How to read from a file, do regular expression and then replace the found regular expression jessy83 Programming 1 12-05-2011 03:10 PM
help in regular expression shekhar2010us Shell Programming and Scripting 1 08-12-2011 03:59 PM
Integer expression expected: with regular expression ketkee1985 Shell Programming and Scripting 3 03-14-2011 02:49 PM
awk and regular expression maskot Shell Programming and Scripting 4 05-22-2007 07:22 AM
Regular Expression + Aritmetical Expression Z0mby Shell Programming and Scripting 2 05-21-2002 10:59 AM



All times are GMT -4. The time now is 07:25 AM.