Posted: Fri Jul 04, 2008 4:10 am Post subject: Re: RegEx needed
On Thursday 3 July 2008 19:44, StephaneLeFou wrote:
Quote:
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.
Posted: Fri Jul 04, 2008 4:10 am Post subject: Re: RegEx needed
On Jul 3, 1:29 pm, pk <p...@pk.invalid> wrote:
Quote:
On 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.
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"
Posted: Fri Jul 04, 2008 4:10 am Post subject: Re: RegEx needed
On Thursday 3 July 2008 19:23, StephaneLeFou wrote:
Quote:
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.
Posted: Fri Jul 04, 2008 8:10 am Post subject: Re: RegEx needed
On Thu, 03 Jul 2008 20:57:20 +0200, pk wrote:
Quote:
Yes, it's just that that "Regex" word made me think of something that uses
regexes...but 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
Yes, it's just that that "Regex" word made me think of something that uses
regexes...but of course there are many ways, even some that do not require
spawning another process (like the one that uses "set").
--
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.
Yes, it's just that that "Regex" word made me think of something that
uses regexes...but 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
NOT 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.
Posted: Sat Jul 05, 2008 4:10 am Post subject: Re: RegEx needed
Bit Twister wrote:
Quote:
On 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
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum