LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-06-2005, 05:40 PM   #1
marri
Member
 
Registered: May 2004
Posts: 38

Rep: Reputation: 15
splitting strings


hi
I am trying to split a string into two parts using either sed or echo but I'm just not able to do it!! I don't know why I always forget how to do it, but here are demo string:

/test/110/user/name
/system/110/hardware/name

I want to extract only what comes in front of and including "110".
I know it's quite simple but I can't seem to be able to do it today.

hope you can help
marri
 
Old 04-06-2005, 06:42 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917
sed 's|\([a-zA-Z]*/110\)\(.*\)|\1|' <filename>
should do ...


Cheers,
Tink
 
Old 04-07-2005, 05:17 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
In bash (only), you could do:
Code:
DIR=/test/110/user/name
EXTRACTED=${DIR%${DIR#*/110/}}
echo $EXTRACTED
 
Old 04-08-2005, 01:48 PM   #4
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440

Rep: Reputation: 52
Wow tinkster, nice sed usage. I have used sed before but not like that. I'm a bit confused. Can you explain what that does? This is what I think it does:

The line below will look for all alpha characters then a / followed by 110 and stops at 110 because of......(i dont understand where that part stops at).
([a-zA-Z]*/110\)\(.*\)

Then the line below will only match the first expression (the statement above).
|\1|

I usually just use sed 's/string/replace/g' in that way. I don't really know what happens when you start putting in '|' characters like that. Tinkster, if you can explain to me what the statement does I would totally appreciate it! Thank you so much.

-twantrd
 
Old 04-08-2005, 03:48 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917
I've just replaced the slashes in 's/string/replace/'
because the search-string has slashes itself, and
even though one can escape them with a back-
slash it's much more readable like that. Basically
you can choose any character to be the separator,
sed will use the one that follows the 's ... ;) ... could
have been a @ or ~

The term in the parenthesis is being preserved
by sed for later re-use.
Code:
's|\([a-zA-Z]*/110\)\(.*\)|\1|'
   |               ||    |
   |--saved as \1--||-\2-| \1 recalled ...

Cheers,
Tink

Last edited by Tinkster; 04-08-2005 at 03:52 PM.
 
Old 04-08-2005, 05:33 PM   #6
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440

Rep: Reputation: 52
Ahh ok. Thanks a lot! I get it.

-twantrd
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to find duplicate strings in vertical column of strings markhod Programming 7 11-02-2005 04:04 AM
splitting strings basher400 Linux - Newbie 6 04-12-2005 02:24 AM
Splitting a File Slayer097 Linux - Newbie 4 02-22-2005 07:15 PM
C: pointers, strings, splitting headache notsoevil Programming 5 06-19-2003 04:04 PM
Splitting my 6o Madmanator Linux - Newbie 1 01-03-2002 11:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:03 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration