LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 05-25-2007, 06:45 PM   #1
Xeratul
Senior Member
 
Registered: Jun 2006
Location: Debian Land
Posts: 1,389

Rep: Reputation: 88
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 )
 
Old 05-25-2007, 06:51 PM   #2
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655
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.
 
Old 05-25-2007, 06:56 PM   #3
Xeratul
Senior Member
 
Registered: Jun 2006
Location: Debian Land
Posts: 1,389

Original Poster
Rep: Reputation: 88
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 )
 
Old 05-25-2007, 08:39 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
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()
 
Old 05-26-2007, 12:13 AM   #5
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 719

Rep: Reputation: 72
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to replace a string in a text file jpan Linux - General 3 10-14-2012 07:17 PM
Problem using sed to replace string in file umk Debian 12 02-01-2012 09:39 AM
BASH: Replace string in file with another eur0dad Programming 5 07-27-2006 05:29 PM
Perl : Troubles to replace a string. philipina Programming 4 07-26-2004 06:09 AM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 02:04 AM


All times are GMT -5. The time now is 11:50 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration