LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 09-07-2006, 05:20 AM   #1
EXnihilo
LQ Newbie
 
Registered: Sep 2006
Posts: 10

Rep: Reputation: 0
Question how to change a word in a text file


Hi,

I have a text file where there is a word i wont to change to another word

eksample i wont to change the word "hello" to "hi"

i think i can remember that there is a command line ...something i can use?????
 
Old 09-07-2006, 05:49 AM   #2
zulfilee
Member
 
Registered: Apr 2004
Location: India
Distribution: Redhat,Fedora
Posts: 430

Rep: Reputation: 39
Welcome to LQ ...

That was a very simple question you asked ,but has got a hell lot of answers.

You can give a bit more detail as to what text editor you are using and whether you want to open a file and replace it or replace it as such.

A few good commands to replace a pattern in a file without even opening is

Assume your file is file1

sed "s/hello/hi/" file1 > file2

[or] if you dont want to redirect to a file and want to replace in the same file

perl -pi -e "s/hello/hi/" file1


...
 
Old 09-07-2006, 05:54 AM   #3
fatra2
Member
 
Registered: Feb 2004
Location: Switzerland
Distribution: Fedora - RedHat - Mandrake - OpenSuse
Posts: 216

Rep: Reputation: 35
It really depends on the text editor that you are using. They vary so much from one to another.

You will have to give more details about the editor itself and if possible the version of it (which can make a difference somtimes).

Cheers
 
Old 09-07-2006, 08:17 AM   #4
EXnihilo
LQ Newbie
 
Registered: Sep 2006
Posts: 10

Original Poster
Rep: Reputation: 0
Hi

Iam using Kate as my text editor. My text is actual a php file where i wont to change a ipaddress to a variable

eksample

$arr = snmpwalk("10.10.1.164", "secret", "1.3.6.1.4.1.1824.1.3.1.1");

actual a php file where i wont to change a ip address to a variable

example

$arr = snmpwalk("10.10.1.164", "secret", "1.3.6.1.4.1.1824.1.3.1.1");


i have a lot of different snmpwalks to the difference snmp devises and i wont to change them easy with a variable.

I can not get this to work

sed "s/hello/hi/" file1 > file2

perl -pi -e "s/hello/hi/" file1
 
Old 09-08-2006, 06:01 AM   #5
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
Quote:
Originally Posted by EXnihilo
I can not get this to work

sed "s/hello/hi/" file1 > file2

perl -pi -e "s/hello/hi/" file1
So what is the error for each of the commands?.
 
Old 09-11-2006, 02:41 AM   #6
EXnihilo
LQ Newbie
 
Registered: Sep 2006
Posts: 10

Original Poster
Rep: Reputation: 0
the sed command:
sed: -e expression nr. 1, sign 17: unterminated `s' command

the Perl command
Substitution replacement not terminated at -e line 1.
 
Old 09-11-2006, 02:48 AM   #7
zulfilee
Member
 
Registered: Apr 2004
Location: India
Distribution: Redhat,Fedora
Posts: 430

Rep: Reputation: 39
The ending '/' would have been omitted.That could be one reason why sed and perl failed.

Both the commands have the following syntax


sed 's/string/replacement/' filename

perl -pi -e "s/string/replacement/" filename


Where
string - is the string which you want to replace
replacement - is the string which will be replaced in place of string


Note:
If you have any '/' in your search string , escape it with a '\' character.


One easy example you can try is this

sed 's/bash/BASH/' /etc/passwd

This will convert all the bash instances in /etc/passwd into BASH.

Dont try the same with perl command as it will actually overwrite the file.

Zulfi.A
 
Old 09-11-2006, 02:52 AM   #8
EXnihilo
LQ Newbie
 
Registered: Sep 2006
Posts: 10

Original Poster
Rep: Reputation: 0
I dont know if it is importent but my os is Kubuntu, and i run it in a vmware.
 
Old 09-11-2006, 02:54 AM   #9
zulfilee
Member
 
Registered: Apr 2004
Location: India
Distribution: Redhat,Fedora
Posts: 430

Rep: Reputation: 39
Not an issue.

Sed and perl have the same syntax on any platform.
 
Old 09-11-2006, 02:55 AM   #10
EXnihilo
LQ Newbie
 
Registered: Sep 2006
Posts: 10

Original Poster
Rep: Reputation: 0
yes, that work fine.

mic@mic-desktop:~$ sed 's/bash/BASH/' /etc/passwd
root:x:0:0:root:/root:/bin/BASH
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/bin/sh
man:x:6:12:man:/var/cache/man:/bin/sh
lp:x:7:7:lp:/var/spool/lpd:/bin/sh
mail:x:8:8:mail:/var/mail:/bin/sh
news:x:9:9:news:/var/spool/news:/bin/sh
uucp:x:10:10:uucp:/var/spool/uucp:/bin/sh
proxy:x:13:13roxy:/bin:/bin/sh
www-data:x:33:33:www-data:/var/www:/bin/sh
backup:x:34:34:backup:/var/backups:/bin/sh
list:x:38:38:Mailing List Manager:/var/list:/bin/sh
irc:x:39:39:ircd:/var/run/ircd:/bin/sh
gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/bin/sh
nobody:x:65534:65534:nobody:/nonexistent:/bin/sh
dhcp:x:101:101::/nonexistent:/bin/false
syslog:x:102:102::/home/syslog:/bin/false
klog:x:103:103::/home/klog:/bin/false
cupsys:x:100:106::/home/cupsys:/bin/false
messagebus:x:104:107::/var/run/dbus:/bin/false
haldaemon:x:108:108:Hardware abstraction layer,,,:/var/run/hal:/bin/false
hplip:x:105:7:HPLIP system user,,,:/var/run/hplip:/bin/false
mic:x:1000:0:Michael C:/home/mic:/bin/BASH
mysql:x:106:113:MySQL Server,,,:/var/lib/mysql:/bin/false
snmp:x:107:65534::/var/lib/snmp:/bin/false
ftp:x:109:65534::/home/ftp:/bin/false
 
Old 09-11-2006, 03:01 AM   #11
zulfilee
Member
 
Registered: Apr 2004
Location: India
Distribution: Redhat,Fedora
Posts: 430

Rep: Reputation: 39
Yeah the same thing can be applied on your file also.
Ex: You want to change the MIB entries
10.1.2.3
10.5.6.5
to a variablename MYDEVICE

Just use
sed 's/10.[0-9].[0-9].[0-9]/MYDEVICE/' filename

Thats one way to do it.
 
Old 09-11-2006, 08:07 AM   #12
EXnihilo
LQ Newbie
 
Registered: Sep 2006
Posts: 10

Original Poster
Rep: Reputation: 0
i have maked a simple file with this:
FILENAME test8

hi hi hi hi hi
new new new new new
hello hello hello hello hello

i then run the sed command with g flag

mic@mic-desktop:~$ sed 's/hi/hello/g' /home/mic/test8
hello hello hello hello hello
new new new new new
hello hello hello hello hellomic@mic-desktop:~$

and this work to
mic@mic-desktop:~$ sed 's/10.10.1.164/MyVar/g' /home/mic/database.php > database8

thank you all for youre help
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to change some text of a certain line of a text file with bash and *nix scripting alred Programming 6 07-10-2006 11:55 AM
How to find and change a specific text in a text file by using shell script Bassam Programming 1 07-18-2005 07:15 PM
Change Text-File through bash script arkus Programming 2 12-17-2004 08:48 PM
How to read and change the contents of a text file? Bassam Linux - General 11 02-26-2004 04:08 AM
How 2 find a duplicate word in a text file cowardnewbie Programming 1 09-16-2001 11:57 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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

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
Open Source Consulting | Domain Registration