Unix/Linux
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
Go Back   Web Development Archives FAQs Unix/Linux

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Web Development Archives Sponsor:
  #1  
Old July 3rd, 2008, 12:49 PM
pk
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
RegEx needed

Thursday 3 July 2008 19:23, StephaneLeFou wrote:

Hi,
>

From a shell script, I need a RegEx in order to extract the "20" from
the following string:
>

"/myfolder/incoming/test;20;000000010"

Regexes do not extract anything by themselves, they just do pattern
matching. To extract the data, you have to use a program like sed:

echo "/myfolder/incoming/test;20;000000010" | sed 's/[^;]*;\([^;]*\)
[^;]*/\1/'

(on a single line)

--
All the commands are tested with bash and GNU tools, so they may use
nonstandard features. I try to mention when something is nonstandard (if
I'm aware of that), but I may miss something. Corrections are welcome.

Reply With Quote
  #2  
Old July 3rd, 2008, 12:49 PM
Bit Twister
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
RegEx needed

Thu, 3 Jul 2008 10:23:19 -0700 (PDT), StephaneLeFou wrote:
Hi,
>

From a shell script, I need a RegEx in order to extract the "20" from
the following string:
>

"/myfolder/incoming/test;20;000000010"

line="/myfolder/incoming/test;20;000000010"
set -- $(IFS=';'; echo $line )
echo $2


Reply With Quote
  #3  
Old July 3rd, 2008, 12:49 PM
StephaneLeFou
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
RegEx needed

Hi,

From a shell script, I need a RegEx in order to extract the "20" from
the following string:

"/myfolder/incoming/test;20;000000010"

Thanks to everyone for your help.

Reply With Quote
  #4  
Old July 3rd, 2008, 01:30 PM
pk
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
RegEx needed

Thursday 3 July 2008 19:44, StephaneLeFou wrote:

I see. What I'd like to do here is to send "20" from the above string
into another variable.
>

I tried your code but it finds:
"20;000000010"
instead of:
"20"

You're right. My newsreader somehow cut a ";". Here's the full command,
along with a way to assign it to a variable:

var=$(echo "/myfolder/incoming/test;20;000000010" |
sed 's/[^;]*;\([^;]*\);[^;]*/\1/')

That should work.



--
All the commands are tested with bash and GNU tools, so they may use
nonstandard features. I try to mention when something is nonstandard (if
I'm aware of that), but I may miss something. Corrections are welcome.

Reply With Quote
  #5  
Old July 3rd, 2008, 02:09 PM
Bit Twister
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
RegEx needed

Thu, 03 Jul 2008 20:57:20 +0200, pk wrote:

Yes, it's just that that "Regex" word made me think of something that uses
regexesbut of course there are many ways, even some that do not require
spawning another process (like the one that uses "set").

No idea about your unix shell, but on linux, set is a bash builtin so
there would not be "spawning another process"

You would not need the echo if you change the IFS. Example:

_ifs_bkup="${IFS}"
IFS=';'

while read line ; do
set -- $line
_save_arg2=$2
done < some_input_filename_here

IFS="${_ifs_bkup}"

(some other code needing normal IFS separation)


Reply With Quote
  #6  
Old July 4th, 2008, 06:01 AM
pk
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
RegEx needed

Thursday 3 July 2008 21:09, Bit Twister wrote:

Thu, 03 Jul 2008 20:57:20 +0200, pk wrote:
>
>Yes, it's just that that "Regex" word made me think of something that
>uses regexesbut of course there are many ways, even some that do not
>require spawning another process (like the one that uses "set").
>

No idea about your unix shell, but on linux, set is a bash builtin so
there would not be "spawning another process"

That's exactly what I said. I said that the solution that uses "set" does
NT require spawning a new process.

--
All the commands are tested with bash and GNU tools, so they may use
nonstandard features. I try to mention when something is nonstandard (if
I'm aware of that), but I may miss something. Corrections are welcome.

Reply With Quote
  #7  
Old July 4th, 2008, 08:39 AM
Bit Twister
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
RegEx needed

Fri, 4 Jul 2008 13:55:44 +0100, Geoff Clare wrote:
Bit Twister wrote:
>
>line="/myfolder/incoming/test;20;000000010"
>set -- $(IFS=';'; echo $line )
>echo $2
>

This is a neat trick that means you don't need to save and restore IFS,
but it's worth noting that it relies on there being no blanks in the
pathname:
>

$ line="/my folder/incoming/test;20;000000010"
$ set -- $(IFS=';'; echo $line )
$ echo $2
folder/incoming/test

That bites that space/blank became a field separator.
I had assumed since I set IFS=';' it would only parse fields on the
semicolon. :-(

It could also be improved by using "set -f" to turn off globbing, and
using printf instead of echo.

Except for possible gain of formatting controls with printf, I do not see the
advantage of using printf since echo is also a builtin.


Reply With Quote
  #8  
Old July 4th, 2008, 09:19 AM
Joachim Schmitz
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
RegEx needed

Bit Twister wrote:
Fri, 4 Jul 2008 13:55:44 +0100, Geoff Clare wrote:
>Bit Twister wrote:

<snip>
>It could also be improved by using "set -f" to turn off globbing, and
>using printf instead of echo.
>

Except for possible gain of formatting controls with printf, I do not
see the advantage of using printf since echo is also a builtin.
echo is not portable as it's behavng differetnly in different shells. printf
is PSIX and as such portable.

Bye, Jojo



Reply With Quote
Reply

Viewing: Web Development Archives FAQs Unix/Linux > RegEx needed


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

vB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT