PDA

View Full Version : Do you know this command in Bash?



tseliot
January 30th, 2006, 11:05 AM
Hi, I'm a newbie in bash (and in programming in general) and I would like to know how I can replace some words with other ones in a text file without opening it with an editor.

For example I would like to change every "ciao" word in "text.txt" with an "hello" word and then either save the modified file or redirect the output to another file.

Is there a command (in bash) which does this?

Thanks in advance.

Alberto

alamba
January 30th, 2006, 11:11 AM
Yes there is, I believe you can do this using pipes. Its pretty trivial. Unfortunately I'm not good at this. Try google for bash pipes if u're in a hurry or I'm sure someone would be able to give you the exact command here.

Akshay

toojays
January 30th, 2006, 11:13 AM
There is no command built into bash to do this. However, the sed program was designed to do what you want.

To redirect the output to another file, you would run "sed -e 's/ciao/hello/g' input.txt >output.txt"

To save the modified file, you would use a special GNU extension: "sed -ie 's/ciao/hello/g' input.txt"

You can read the complete sed documentation online (http://www.gnu.org/software/sed/manual/sed.html), or in Emacs' info mode, or by running "info sed".

tseliot
January 30th, 2006, 11:35 AM
There is no command built into bash to do this. However, the sed program was designed to do what you want.
...And the books I bought don't mention how to do this.


To redirect the output to another file, you would run "sed -e 's/ciao/hello/g' input.txt >output.txt"

To save the modified file, you would use a special GNU extension: "sed -ie 's/ciao/hello/g' input.txt"

You can read the complete sed documentation online (http://www.gnu.org/software/sed/manual/sed.html), or in Emacs' info mode, or by running "info sed".
Thanks I'll try it

EDIT: it WORKS GREAT! Thanks again now I can develop my little project!

jerome bettis
January 30th, 2006, 09:32 PM
you can also use awk for this type of stuff. i prefer awk over sed cause it has C like syntax and sed is kinda bizarre. awk is pretty sweet check it out

cat input.txt | awk ' { gsub("oldtext", "nextext"); print $_ } ' > output.txt

slavik
January 31st, 2006, 05:52 AM
or you can write perl :)

perl is sed, awk, shell, C all put in one. :)

it can also do db and stuff ... and a simple perl web server is like 10 lines of code ... take THAT apache and smoke it. :P

sas
February 2nd, 2006, 08:11 PM
In perl it'd be:
perl -pi -e 's/ciao/hello/g' text.txt

Lord Illidan
February 2nd, 2006, 08:21 PM
...And the books I bought don't mention how to do this.


Thanks I'll try it

EDIT: it WORKS GREAT! Thanks again now I can develop my little project!

What books have you got then? Am wondering what to buy myself..

kvorion
February 2nd, 2006, 08:24 PM
For example I would like to change every "ciao" word in "text.txt" with an "hello" word and then either save the modified file or redirect the output to another file.

Alberto

I think

perl -e `s/ciao/hello/g` > text.txt should work.

We are using Perl regular expressions here.

perl -e is simply to run the command on ur shell.
s/foo/bar/g;
replaces any occurrence of the exact character sequence foo in the "current string" by the character sequence bar; for example, foolish bigfoot would become barlish bigbart

tseliot
February 4th, 2006, 09:25 PM
Thanks for the answers!

4 Lord Illidan: I'm using this book (I bought it on Amazon) "A Practical Guide to Linux Commands, Editors, and Shell Programming" - Mark Sobell

The other book of mine is: "Running Linux" Oreilly

BTW I haven't read them all.

professor_chaos
February 5th, 2006, 01:49 AM
for bash scripting i find this a useful resource.
http://www.tldp.org/LDP/abs/html/