niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » shell
Better Method than this for string substitution
Post new topic   Reply to topic Page 1 of 1 [3 Posts] View previous topic :: View next topic
Author Message
Chris F.A. Johnson
*nix forums Guru


Joined: 20 Feb 2005
Posts: 2268

PostPosted: Sat Feb 05, 2005 2:09 pm    Post subject: Re: Better Method than this for string substitution Reply with quote

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

PostPosted: Sat Feb 05, 2005 7:53 am    Post subject: Re: Better Method than this for string substitution Reply with quote

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

PostPosted: Sat Feb 05, 2005 3:57 am    Post subject: Better Method than this for string substitution Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [3 Posts] View previous topic :: View next topic
The time now is Thu Jan 08, 2009 11:41 pm | All times are GMT
navigation Forum index » Programming » shell
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts Best method to search kristy420 Oracle 0 Tue Nov 18, 2008 6:34 pm
No new posts FAQ 4.32 How do I strip blank space from the beginning/en... PerlFAQ Server Perl 0 Fri Jul 21, 2006 1:03 pm
No new posts FAQ 4.34 How do I extract selected columns from a string? PerlFAQ Server Perl 0 Fri Jul 21, 2006 7:03 am
No new posts print all permutations of string anurag C 10 Thu Jul 20, 2006 5:57 pm
No new posts access method "gin" does not exist Kevin Murphy PostgreSQL 0 Thu Jul 20, 2006 4:25 pm

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
[ Time: 0.1462s ][ Queries: 20 (0.0753s) ][ GZIP on - Debug on ]