LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   [bash] cutting text with sed (https://www.linuxquestions.org/questions/programming-9/%5Bbash%5D-cutting-text-with-sed-288837/)

Erhnam 02-11-2005 04:07 AM

[bash] cutting text with sed
 
I'm looking for a way to cut of some text.
With the commando 'pwd' I list the current directory. (example: /home/cs/server05)

With these to lines:

SPATH="$(cd $(dirname $0);pwd)" ## /home/cs/server05
SNAME=`echo $SPATH | cut -f 4 -d/` ## server05

I'm able to read the full directory and cut of the last 8 values (static, always 8 characters).

The next problem is that I want to read what's between /home/ and the /server05. This value is dynamic by name and length. I tried using sed but the commando below gives me "cs/server05".

SERVER=`echo $SPATH | sed -e s/\.home\.\/''/`

What do I need to change to get 'cs' ?

Erhnam 02-11-2005 04:28 AM

Fixed it already!

SPATH="$(cd $(dirname $0);pwd)" ## /home/cs/server05
SVALUE=`echo $SPATH | cut -d/ -f3`

echo $SVALUE

slakmagik 02-11-2005 04:29 AM

cut -d/ -f3

Or is it more complex than that and you really need sed?

keefaz 02-11-2005 04:31 AM

First you can also get SNAME with:
SNAME=`basename $SPATH`

for SERVER, you could try:
SERVER=`echo $SPATH | sed -r 's#(.*)/(.*)/(.*)#\2#'`

[edit]
Ok, it seems you find a better way :)


All times are GMT -5. The time now is 06:50 AM.