LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how to replace string by other one in a file & without perl ? (https://www.linuxquestions.org/questions/programming-9/how-to-replace-string-by-other-one-in-a-file-and-without-perl-556717/)

Xeratul 05-25-2007 05:45 PM

how to replace string by other one in a file & without perl ?
 
Let's take an example:
In file1.txt, for the replaceement of all occurences of "the little bird" by "the little turtle that "

Please in C, gambas, bash, sh, ... (but not perl and please not with stuffs like ninja \//\\\\\\//////\\\\\\ )


Thank you !

==
(Sorry, but google cannot say which is the best way & easiest )

jschiwal 05-25-2007 05:51 PM

This is something that you do in sed or awk.
If each instance in contained on the same line, and the replacement won't make the line too long, then a single sed command can do it:
sed 's/the little bird/the little turtle that/g' file1.txt.

However, if the phrase may be split on two lines, you need to add more rules which build up two lines in a buffer and ignore \n characters.

The first edition of the O'Reily "Sed & Awk" book may be available on the web.

Xeratul 05-25-2007 05:56 PM

Quote:

Originally Posted by jschiwal
This is something that you do in sed or awk.
If each instance in contained on the same line, and the replacement won't make the line too long, then a single sed command can do it:
sed 's/the little bird/the little turtle that/g' file1.txt.

However, if the phrase may be split on two lines, you need to add more rules which build up two lines in a buffer and ignore \n characters.

The first edition of the O'Reily "Sed & Awk" book may be available on the web.

thank you very much ! Nothing than the best reply :-) ! Our bash

Then, now, hence, that s the beginning for me working with Awk.I liked yoru help ! I have a book with Awk and will study it.


==
(When I was googling I was really scared by these \//\\\\\\//////\\\\\\ and my trials didnt worked all the time )

ghostdog74 05-25-2007 07:39 PM

Quote:

Originally Posted by Xeratul
Let's take an example:
In file1.txt, for the replaceement of all occurences of "the little bird" by "the little turtle that "

Please in C, gambas, bash, sh, ... (but not perl and please not with stuffs like ninja \//\\\\\\//////\\\\\\ )


Thank you !

==
(Sorry, but google cannot say which is the best way & easiest )

in awk:
Code:

#!/bin/sh
awk '{ gsub(/the little bird/,"the little turtle");print}' file

Python, no ninjas:
Code:

#!/usr/bin/python
for line in open("file"):
    line=line.replace("the little bird","the little turtle")
    print line.strip()


makyo 05-25-2007 11:13 PM

Hi.

If you find yourself doing this kind of operation often, you could write your own command to do it simply.
Quote:

Early versions of Unix also had a gres command (perform a substitution on all matching lines) but it was obsoleted by sed and abandoned. -- http://www.perlmonks.org/?node_id=501419
It's not difficult to do this once or twice with a sed or awk command, but for continual use it is convenient to do it from the command line. I wrote and improved the one I use in your proscribed language :) but clearly you could use anything that is appropriate for the task to achieve this:
Code:

gres "the little bird" "the little turtle" filename
... cheers, makyo


All times are GMT -5. The time now is 01:56 PM.