Tell me more ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

As we all know, sed is greatly efficient to find and replace string, for example find 'a' and replace it to 'b': sed 's/a/b/g'.

Is it possible to do this with other command or shell script instead of sed?

share|improve this question
 
Actually, that is just regular expression based substitution. Any tool or language capable to handle such thing will be able to do the same, but with various syntax: $var=~s/a/b/g, gsub(/a/,"b",var), var.gsub(/a/,'b'), var.replace(/a/g,'b'), preg_replace("/a/","b",$var), regsub -all a b $var. Beside that, many tools and languages can also do plain text string replacement. So your question is somehow broad. –  manatwork 2 days ago
1  
Why? What problem are you trying to solve? –  Gilles 2 days ago
 
A cropped linux systems for TV have not sed command. So I has to use other command or script to instead of sed 's/a/b/g'. –  binghenzq 2 days ago

4 Answers

The classic alternative for single letter substitutions is the tr command which should be available on just about any system:

$ echo "foobar" | tr a b   
foobbr

tr is better than sed for this actually since using sed (let alone perl or awk) for single letter substitutions is like using a sledge hammer to kill a fly.

grep is not designed for this, it does not modify its input, it only searches through it.

Alternatively, you can use the shell's substitution capabilities:

$ foo="foobar"
$ echo ${foo//a/b}
foobbr

We could give you more specific answers if you explained exactly what problem you are trying to solve.

share|improve this answer
1  
As the question was s/a/b/g, better use ${foo//a/b}. –  manatwork 2 days ago
 
@manatwork good point, thanks, answer edited. –  terdon yesterday

Yes there are a variety of ways to do this. You can use awk, perl, or bash to do these activities as well. In general though sed is probably the most apropos tool for doing these types of tasks.

Examples

Say I have this sample data, in a file data.txt:

foo bar 12,300.50
foo bar 2,300.50
abc xyz 1,22,300.50

awk

$ awk '{gsub("foo", "foofoofoo", $0); print}' data.txt 
foofoofoo bar 12,300.50
foofoofoo bar 2,300.50
abc xyz 1,22,300.50

Perl

$ perl -pe "s/foo/foofoofoo/g" data.txt 
foofoofoo bar 12,300.50
foofoofoo bar 2,300.50
abc xyz 1,22,300.50
share|improve this answer
 
Thanks for you answer and i will try it next workday.But i am not sure if it work because some command maybe not exist in a cropped linux systems of TV,such as 'sed'.So i still want to use command 'find','grep' and so on to solve it. –  binghenzq 2 days ago

If you're working with a file rather than a stream, you could use the standard text editor, ed:

printf '%s\n' ',s/a/b/g' w q | ed file.txt

This should be available on any *nix. The comma in ',s/a/b/g' tells ed to work on every line (you can also use %, which will be more familiar if you're used to vim), and the rest of it is a standard search and replace. w tells it to write (save) the file, q tells it to exit.

Note that, unlike sed's -i option (and similar options in other tools), this actually does edit the file in-place rather than cheating with temporary files.

I don't think it's possible to get this working with streams, but then I don't really know much about ed and I wouldn't be surprised if it actually does have that capability (the unix philosophy being what it is).

share|improve this answer

Using the ancestor command :

Just printing on STDOUT :

ed -s file <<!
s/a/b/g
p
q
!

replacing in-place :

ed -s file <<!
s/a/b/g
w
q
!
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.