LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 02-11-2005, 04:07 AM   #1
Erhnam
Member
 
Registered: Jan 2005
Posts: 54

Rep: Reputation: 15
[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' ?
 
Old 02-11-2005, 04:28 AM   #2
Erhnam
Member
 
Registered: Jan 2005
Posts: 54

Original Poster
Rep: Reputation: 15
Fixed it already!

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

echo $SVALUE
 
Old 02-11-2005, 04:29 AM   #3
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
cut -d/ -f3

Or is it more complex than that and you really need sed?
 
Old 02-11-2005, 04:31 AM   #4
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
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
 
  


Reply



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
Using sed to replace a lot of text Wynd Programming 9 10-07-2005 03:47 AM
Sed Help Needed..Cutting out peice of file farmerjoe Programming 2 02-25-2005 06:05 PM
Cutting in Bash??? mattyk1026 Programming 4 06-09-2004 01:23 PM
cutting down contents of text file kam_kenneth Linux - Newbie 2 05-22-2004 01:16 AM
bash - sed/tr??? pk21 Programming 2 09-05-2003 07:56 AM

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

All times are GMT -5. The time now is 02:21 AM.

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