|
|
|
|
|
|
| Author |
Message |
Chris F.A. Johnson *nix forums Guru
Joined: 20 Feb 2005
Posts: 2268
|
Posted: Sat Feb 05, 2005 2:09 pm Post subject:
Re: Better Method than this for string substitution
|
|
|
On Sat, 05 Feb 2005 at 04:57 GMT, Blitz Krieg wrote:
| Quote: | How can I change the following code to get it to one line?
BUFFER=`cat "$1"`
|
Since you are using bash, you don't need cat:
BUFFER=`< "$1"`
However, note that both methods will delete trailing newlines. You
can prevent that with:
BUFFER=`< "$1"; printf '.'`
BUFFER=${BUFFER%.}
| Quote: | echo "${BUFFER//$2/$3}" > "$4"
I want to kill the need to use 'BUFFER' string var
|
If you want to use bash's parameter expansion to perform the
search and replace, then you are going to have to have the string
in a variable. How else could you use it?
| Quote: | btw, $1 is a input file name, $2 is the text to find in file, $3 is the
replace text, $4 is the output file name.
Why am I doing this versus using SED or something else? no reason the
following works to...
sed "s/$2/$3/g" < "$1" > "$4"
but since I am in a bash script, using sed just seemed to heavy
|
I wouldn't be surprised if the sed version is faster on a large
file.
--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
===================================================================
My code (if any) in this post is copyright 2005, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License |
|
| Back to top |
|
 |
Erkan Yanar *nix forums beginner
Joined: 27 May 2005
Posts: 6
|
Posted: Sat Feb 05, 2005 7:53 am Post subject:
Re: Better Method than this for string substitution
|
|
|
Blitz Krieg?
Dont you think this name is to stupid(offensive)?!
fup2p
--
über den grenzen muß die freiheit wohl wolkenlos sein |
|
| Back to top |
|
 |
Blitz Krieg *nix forums beginner
Joined: 05 Feb 2005
Posts: 1
|
Posted: Sat Feb 05, 2005 3:57 am Post subject:
Better Method than this for string substitution
|
|
|
How can I change the following code to get it to one line?
BUFFER=`cat "$1"`
echo "${BUFFER//$2/$3}" > "$4"
I want to kill the need to use 'BUFFER' string var
btw, $1 is a input file name, $2 is the text to find in file, $3 is the
replace text, $4 is the output file name.
Why am I doing this versus using SED or something else? no reason the
following works to...
sed "s/$2/$3/g" < "$1" > "$4"
but since I am in a bash script, using sed just seemed to heavy |
|
| Back to top |
|
 |
Google
|
|
| Back to top |
|
 |
|
|
The time now is Thu Jan 08, 2009 11:41 pm | All times are GMT
|
|
Wikipedia | Loans | Debt Consolidation | Online Loans | Debt Consolidation
|
|
Copyright © 2004-2005 DeniX Solutions SRL
|
|
|
|
Other DeniX Solutions sites:
Unix/Linux blog |
electronics forum |
medicine forum |
science forum |
|
|
Privacy Policy
|
Powered by phpBB © 2001, 2005 phpBB Group
|
|