Welcome to Coding : Sécurité Programmation Réseaux

Search   in  

 Create an Account Home | Submit News Your Account Content | Topics | Top 10  


Accueil
· Home
· Listing des Articles
· Top 10
· Repository des Exploits

Les sujets / parties
· C / C ++
· Visual Basic
· Asm
· Reseaux
· Java
· Securite
· Divers

Utile
· Listing des Articles

· Telecharger
· Le Forum
· Liens
· Proposer un article

Top20 des Downloads
· 1: Etude des reseaux generalites et protocoles
· 2: Cheval de troie en VB avec sources
· 3: Netcat 1.1
· 4: Keylogger
· 5: Etudes des reseaux hauts debits architectures et protocoles
· 6: Ecoute de port
· 7: Etude du Smart Spoofing
· 8: Win Packet Capture Utils
· 9: Tutorial on Traffic Interception on Switched Lan using ARP spoofing
· 10: Cours de C

User Info
Welcome, Anonymous
Nickname
Password
(Register)
Membership:
Latest: trapcodien
New Today: 1
New Yesterday: 0
Overall: 2207

People Online:
Visitors: 42
Members: 1
Total: 43

Online Now:
01: trapcodien

  
Coding : Sécurité Programmation Réseaux: Forums

Coding :: View topic - RegEx needed
 Forum FAQForum FAQ   SearchSearch   UsergroupsUsergroups   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

RegEx needed

 
Post new topic   Reply to topic    Coding Forum Index -> Scripting
View previous topic :: View next topic  
Author Message
StephaneLeFou
Guest





PostPosted: Fri Jul 04, 2008 4:10 am    Post subject: RegEx needed Reply with 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"

Thanks to everyone for your help.
Back to top
OldSchool
Guest





PostPosted: Fri Jul 04, 2008 4:10 am    Post subject: Re: RegEx needed Reply with quote

On Jul 3, 1:47 pm, StephaneLeFou <stephanele...@gmail.com> wrote:
Quote:
On Jul 3, 1:42 pm, Bit Twister <BitTwis...@mouse-potato.com> wrote:

On 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

This works.  What's IFS for?
Thank you.

IFS is input field separator...

this should also work

line="/myfolder/incoming/test;20;000000010"
echo $line | cut -d \; -f 2
Back to top
Bit Twister
Guest





PostPosted: Fri Jul 04, 2008 4:10 am    Post subject: Re: RegEx needed Reply with quote

On Thu, 3 Jul 2008 10:47:22 -0700 (PDT), StephaneLeFou wrote:
Quote:
On Jul 3, 1:42 pm, Bit Twister <BitTwis...@mouse-potato.com> wrote:

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

This works. What's IFS for?

I think it is Internal Field Separator :)

Some light reading found at
http://tldp.org/LDP/abs/html/index.html
Back to top
pk
Guest





PostPosted: Fri Jul 04, 2008 4:10 am    Post subject: Re: RegEx needed Reply with quote

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.
Back to top
StephaneLeFou
Guest





PostPosted: Fri Jul 04, 2008 4:10 am    Post subject: Re: RegEx needed Reply with quote

On Jul 3, 1:42 pm, Bit Twister <BitTwis...@mouse-potato.com> wrote:
Quote:
On 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

This works. What's IFS for?
Thank you.
Back to top
StephaneLeFou
Guest





PostPosted: Fri Jul 04, 2008 4:10 am    Post subject: Re: RegEx needed Reply with quote

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"

Thanks.
Back to top
Bit Twister
Guest





PostPosted: Fri Jul 04, 2008 4:10 am    Post subject: Re: RegEx needed Reply with quote

On Thu, 3 Jul 2008 10:23:19 -0700 (PDT), 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"

line="/myfolder/incoming/test;20;000000010"
set -- $(IFS=';'; echo $line )
echo $2
Back to top
pk
Guest





PostPosted: Fri Jul 04, 2008 4:10 am    Post subject: Re: RegEx needed Reply with quote

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.
Back to top
Sashi
Guest





PostPosted: Fri Jul 04, 2008 8:10 am    Post subject: Re: RegEx needed Reply with quote

On Jul 3, 1:29 pm, pk <p...@pk.invalid> wrote:

Quote:

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

(on a single line)

You might be a fan of sed but doesn't cut automatically suggest itself
for this particular case?

result=$(echo "/myfolder/incoming/test;20;000000010" | cut -f2 -d\Wink
Back to top
Bit Twister
Guest





PostPosted: Fri Jul 04, 2008 8:10 am    Post subject: Re: RegEx needed Reply with quote

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

IFS="${_ifs_bkup}"

(some other code needing normal IFS separation)
Back to top
pk
Guest





PostPosted: Fri Jul 04, 2008 8:10 am    Post subject: Re: RegEx needed Reply with quote

On Thursday 3 July 2008 20:51, Sashi wrote:

Quote:
On Jul 3, 1:29 pm, pk <p...@pk.invalid> wrote:


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

(on a single line)

You might be a fan of sed but doesn't cut automatically suggest itself
for this particular case?

result=$(echo "/myfolder/incoming/test;20;000000010" | cut -f2 -d\Wink

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.
Back to top
pk
Guest





PostPosted: Fri Jul 04, 2008 6:10 pm    Post subject: Re: RegEx needed Reply with quote

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

Quote:
On 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 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.
Back to top
Joachim Schmitz
Guest





PostPosted: Sat Jul 05, 2008 4:10 am    Post subject: Re: RegEx needed Reply with quote

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

is POSIX and as such portable.

Bye, Jojo
Back to top
Geoff Clare
Guest





PostPosted: Sat Jul 05, 2008 4:10 am    Post subject: Re: RegEx needed Reply with quote

Bit Twister wrote:

Quote:
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

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

--
Geoff Clare <netnews@gclare.org.uk>
Back to top
Bit Twister
Guest





PostPosted: Sat Jul 05, 2008 4:10 am    Post subject: Re: RegEx needed Reply with quote

On Fri, 4 Jul 2008 13:55:44 +0100, Geoff Clare wrote:
Quote:
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. :-(

Quote:
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.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    Coding Forum Index -> Scripting All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
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

Powered by phpBB © 2001, 2005 phpBB Group
PHP-Nuke Copyright © 2005 by Francisco Burzi. This is free software, and you may redistribute it under the GPL. PHP-Nuke comes with absolutely no warranty, for details, see the license.
Page Generation: 0.41 Seconds