filmov
tv
Unix & Linux: How to find and replace string without use command Sed? (5 Solutions!!)

Показать описание
Unix & Linux: How to find and replace string without use command Sed?
The Question: 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?
This is for a cropped linux systems for TV that does not have the sed command.
So I have to use other commands or scripts instead of sed 's/a/b/g'. -
Solutions: Please watch the whole video to see all solutions, in order of how many people found them helpful
== This solution helped 12 people ==
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 ****
foo bar 12,300.50
foo bar 2,300.50
abc xyz 1,22,300.50
awk
foofoofoo bar 12,300.50
foofoofoo bar 2,300.50
abc xyz 1,22,300.50
Perl
foofoofoo bar 12,300.50
foofoofoo bar 2,300.50
abc xyz 1,22,300.50
**** Inline editing ****
The above examples can directly modify the files too. The Perl example is
trivial. Simply add the -i switch.
For awk it's a little less direct but just as effective:
This method creates a sub-shell with the braces '{ ... }` where the file is
redirected into it via this:
Once the file has been redirected into the sub-shell, it's deleted and then awk
is run against the contents of the file that was read into the sub-shells
STDIN. This content is then processed by awk and written back out to the same
file name that we just deleted, effectively replacing it.
== This solution helped 14 people ==
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.
$ 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.
== This solution helped 3 people ==
If you're working with a file rather than a stream, you could use the standard
text editor, ed:
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).
The Question: 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?
This is for a cropped linux systems for TV that does not have the sed command.
So I have to use other commands or scripts instead of sed 's/a/b/g'. -
Solutions: Please watch the whole video to see all solutions, in order of how many people found them helpful
== This solution helped 12 people ==
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 ****
foo bar 12,300.50
foo bar 2,300.50
abc xyz 1,22,300.50
awk
foofoofoo bar 12,300.50
foofoofoo bar 2,300.50
abc xyz 1,22,300.50
Perl
foofoofoo bar 12,300.50
foofoofoo bar 2,300.50
abc xyz 1,22,300.50
**** Inline editing ****
The above examples can directly modify the files too. The Perl example is
trivial. Simply add the -i switch.
For awk it's a little less direct but just as effective:
This method creates a sub-shell with the braces '{ ... }` where the file is
redirected into it via this:
Once the file has been redirected into the sub-shell, it's deleted and then awk
is run against the contents of the file that was read into the sub-shells
STDIN. This content is then processed by awk and written back out to the same
file name that we just deleted, effectively replacing it.
== This solution helped 14 people ==
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.
$ 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.
== This solution helped 3 people ==
If you're working with a file rather than a stream, you could use the standard
text editor, ed:
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).