Advertisement

Using SED with a BASH variable

Started by April 14, 2008 12:31 AM
3 comments, last by Gamer Gamester 16 years, 7 months ago
I'm writing a bash script. I have a variable which is something like:
FILE=My_File.out
What I want to do is create a new variable that removes the trailing ".out" portion of the variable. I would usually use sed for something like this. However, sed wants a file passed to it, not a line. Is there a way to pass just a line to sed (without creating an intermediary file)? Or is there an even simpler way to do this in Bash alone (I'm sure there must be, but I'm somewhat new to Bash scripting and it doesn't jump out at me)? Here's the type of thing I've been doing:
TITLE=`sed -e 's/^\(.*\)\.out/1/' < ${FILE}`
But this looks to pass the contents of the file "My_File.out" to sed, rather than the text "My_File.out" itself.
It's actually very simple, though easy to forget about. Simply echo the variable and pipe the output to sed. $(do something) is just the same as `do something`, I just think it's easier to read.
TITLE=$(echo "$FILE" | sed -e 's/^\(.*\)\.out/\1/')
Advertisement
echo $FILE | sed -e 's/^\(.*\)\.out/\1/'


Don't forget your backslash.
We''re sorry, but you don''t have the clearance to read this post. Please exit your browser at this time. (Code 23)
... you don't need sed. Use built-in parameter expansion:

TITLE = ${FILE%.out}
Thanks for all your replies. I will use ToohrVyk's suggestion because it is simplest, but I'm sure I will make use of an echo to sed pipe at some point as well.

This topic is closed to new replies.

Advertisement