The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-21-2008
phreezr phreezr is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 11
Question Another sed question

Hello, I am very new to shell scripting and have a directory path such as:
/usr/dev/blah/Arch/release/812-1235-P05/files/list and I want to output:

812-1235-P05

I think using sed with a regex like [0-9]-[0-9]-[0-9]?[a-z] would be the way to go, but I am having much trouble getting it to work. Any suggestions? Thank you.
  #2 (permalink)  
Old 10-21-2008
wempy's Avatar
wempy wempy is offline
Registered User
  
 

Join Date: Jun 2006
Location: Harpenden, UK
Posts: 208
you need to add some quantifiers and limiters in there
Code:
sed 's/.*\([0-9]\{3\}-[0-9]\{4\}-[0-9A-Z]\{3\}\).*/\1/'
which will look for anything followed by \( exactly 3 digits followed by a - followed by exactly 4 digits, followed by a - followed by a combination of 3 letters and/or digits \) followed by anything and replace it all with the part above in \( .. \)

Last edited by wempy; 10-21-2008 at 11:38 AM.. Reason: for clarity
  #3 (permalink)  
Old 10-21-2008
phreezr phreezr is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 11
Thank you so much for your reply. Looks like it should work, I am piping
/usr/dev/blah/Arch/release/812-1235-P05/files/list which is from a previous grep command into sed so like
Code:
grep command_to_get_path | sed 's/.*\([0-9]\{3\}-[0-9]\{4\}-[0-9A-Z]\{3\}\).*/\1/'
and it just displays the whole path again, it doesn't strip out everything around the 812-1235-P05. Am I doing something completely wrong? I must be.

Thanks again!
  #4 (permalink)  
Old 10-21-2008
wempy's Avatar
wempy wempy is offline
Registered User
  
 

Join Date: Jun 2006
Location: Harpenden, UK
Posts: 208
That is strange, if I pipe that string directly into sed on my machine I get the required result. Can you run the grep without the sed and show us the exact output of the grep (including all spacing and 'wierd' chars).

my sed version:

chris@druid: ~ $ sed --version
GNU sed version 4.1.5

Last edited by wempy; 10-21-2008 at 11:57 AM.. Reason: added sed version
  #5 (permalink)  
Old 10-21-2008
phreezr phreezr is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 11
I got it, my directory path also has a timestamp at the end of it and I didn't enclose it with quotes and it used some <>'s Newbie mistake I guess. Thank you much for all your help...certainly made my day easier.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:18 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0