|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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. |
|
#2
|
|||
|
|||
|
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 |
|
#3
|
|||
|
|||
|
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. |
|
#4
|
|||
|
|||
|
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. |
|
#5
|
|||
|
|||
|
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) |
|
#6
|
|||
|
|||
|
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. |
|
#7
|
|||
|
|||
|
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. |
|
#8
|
|||
|
|||
|
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 |
![]() |
| Viewing: Web Development Archives > FAQs > Unix/Linux > RegEx needed |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|