LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 09-04-2003, 12:25 PM   #1
philco
LQ Newbie
 
Registered: Sep 2003
Posts: 3

Rep: Reputation: 0
parsing a variable using linux shell


Hi,

Is there a way to parse a variable using shell scripting.
For example i have a simple variable like

FILE_DIR="/tmp/"

I want to basically cut the slashes from this variable and
put into a new variable so that it looks like

OUTPUT="tmp"

thanks,
Phil
 
Old 09-04-2003, 12:52 PM   #2
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
Welcome to LQ.

Try this:
OUTPUT=`echo $FILE_DIR | sed {'s/\///g'}`

sed is a search and replace tool that you can use to trip characters.
 
Old 09-04-2003, 01:04 PM   #3
philco
LQ Newbie
 
Registered: Sep 2003
Posts: 3

Original Poster
Rep: Reputation: 0
thanks for your response but I`m getting this error:

sed: -e expression #1, char 9: Unknown option to 's'

I see what you are doing, it looks like you are replacing the occurence of / with an empty character. Wonder what`s wrong.

Phil
 
Old 09-04-2003, 01:49 PM   #4
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
That's odd - what version of sed do you have:
sed -V

I'm using:
GNU sed version 4.0.5
 
Old 09-04-2003, 02:00 PM   #5
philco
LQ Newbie
 
Registered: Sep 2003
Posts: 3

Original Poster
Rep: Reputation: 0
GNU sed version 3.02

older version but still you'd think something like that would work.
Anyway I`ll get the llatest version and see what that does
 
Old 09-04-2003, 04:09 PM   #6
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
There are lots of different ways to parse something. Is there more to what you're trying to accomplish than the example you gave? Another possibility is:

OUTPUT=`echo $FILE_DIR | cut -f 2 -d '/'`
 
Old 09-04-2003, 06:52 PM   #7
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
You could try
OUTPUT="${FILE_DIR//\//}"

However if you start with something like "/usr/local/doc/" this will give "usrlocaldoc", so you need to first strip off the trailing '/' with
"${FILE_DIR%\/}", and then strip off the leading "/*/" patterns. ie - "/usr/local/":
"${FILE_DIR##\/*\/}"

Putting it all together:
TEMP="${FILE_DIR%\/}"
OUTPUT="${TEMP##\/*\/}"
 
  


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
Shell variable in Linux is reset after while loop CBenner Programming 7 05-19-2006 09:46 AM
(shell script) string parsing kuru Programming 4 09-12-2005 07:59 PM
Script File: Parsing command sent to shell cheema Programming 1 07-01-2005 12:54 PM
File Parsing using a Shell Script yasir15 Programming 5 08-22-2003 12:17 PM
Need suggestions for parsing a shell variable Pauly Linux - General 1 01-15-2003 07:32 PM

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

All times are GMT -5. The time now is 12:01 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