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.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Loop problem with one more problem aliahsan81 Shell Programming and Scripting 3 01-07-2009 02:02 AM
problem in finding a hardware problem girish.batra SUN Solaris 8 09-09-2008 11:10 AM
ssh script problem problem pcjandyala Shell Programming and Scripting 2 07-31-2008 04:27 PM
problem with dd command or maybe AFS problem Anta Shell Programming and Scripting 0 08-25-2006 11:10 AM
SSH Problem auth problem budrito UNIX for Advanced & Expert Users 1 03-17-2004 10:12 AM

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 01-22-2009
mierdatuti mierdatuti is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 20
problem with sed

Hi,

I have this:

bash-2.03$ echo $kk
<programme start="20090124053000 +0100" channel=\CTele-205.laguiatv.com">

I would like to extract= 20090124053000

But I do:

echo $kk |sed -e 's/.*start="\(.*\) .*/\1/'

and it gives me:

20090124053000 +0100"

I can't understand why this command doesn't works. Any help??

Many thanks!
  #2 (permalink)  
Old 01-22-2009
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
Moderator
  
 

Join Date: Sep 2007
Location: Germany
Posts: 2,311
It does not work because sed takes your 1st grouping between the ( and ) and finds a .* which means any number of any characters and even a blank. Then you try to tell it that a blank shows up, marking the end for the grouping, which it can't parse correctly since there could be a blank inside the brackets too (at least I explain it myself with this thesis of ambigous input). So you have to tell it, that the pattern/grouping exists of any character but NOT a blank:


Code:
..| sed -e 's/.*start="\([^ ]*\) .*/\1/'

I changed the . in front of the * inside the \( and \) to [^ ] which means "anything but no blank". This should work:


Code:
echo ${kk}| sed -e 's/.*start="\([^ ]*\) .*/\1/'
20090124053000

  #3 (permalink)  
Old 01-22-2009
mierdatuti mierdatuti is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 20
Quote:
Originally Posted by zaxxon View Post
It does not work because sed takes your 1st grouping between the ( and ) and finds a .* which means any number of any characters and even a blank. Then you try to tell it that a blank shows up, marking the end for the grouping, which it can't parse correctly since there could be a blank inside the brackets too (at least I explain it myself with this thesis of ambigous input). So you have to tell it, that the pattern/grouping exists of any character but NOT a blank:


Code:
..| sed -e 's/.*start="\([^ ]*\) .*/\1/'

I changed the . in front of the * inside the \( and \) to [^ ] which means "anything but no blank". This should work:


Code:
echo ${kk}| sed -e 's/.*start="\([^ ]*\) .*/\1/'
20090124053000

Many thanks! It works! and than you for the explication
  #4 (permalink)  
Old 01-22-2009
JerryHone JerryHone is offline
Registered User
  
 

Join Date: Nov 2006
Location: UK
Posts: 178
It's known as "greedy matching". The regular expression will always try and match the biggest string that satisfies the specified pattern, which in your case includes the embedded space.
In Perl, you can specify "non greedy" matching by following the * with a ?.

I think you could also have done...

Code:
echo ${kk}| sed -e 's/.*="\([0-9]*\) .* /\1/'

...to isolate the datestamp.

Jerry
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 12:34 PM.


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