Linux Shell Command - Find And Replace Text Within Files
linux shell command - find and replace text within files
linux, shell, command, find, and, replace, text, within, files
Linux Shell Command - Find And Replace Text Within Files
Post Description: linux shell command - find and replace text within files
POST# 796
Posted On: Fri Feb 29, 2008 12:01 pm
webmaster
Topic: Linux Shell Command - Find And Replace Text Within Files
To Find and Replace text within file(s)

ok today i was asked to edit 900 files in a unix system. i was asked to change the ip addresses on all the dns records in bind.

so when i went to see how many domain names there were, there were like 980 pri. files.. ouch

how am i going to do this.. do i have to do this one by one? no, i thought, there's gotta be an easier way tot do this. well im luck because you can search for text and then replace whatever text you want in a file with some simple commands in linux.

so here is my situation. when i open a pri. file in /var/named/chroot/var/named/pri.mydomain.com it looks something like this:

pri.mydomain.com
$TTL        86400
@       IN      SOA     ns1.webune.com. admin.mydomain.net. (
                        2006101601       ; serial, todays date + todays serial #
                        28800              ; refresh, seconds
                        7200              ; retry, seconds
                        604800              ; expire, seconds
                        86400 )            ; minimum, seconds
;
                NS      ns1.webune.com.              ; Inet Address of name server 1
                NS      ns1.webune.com.              ; Inet Address of name server 2
;

www  MX      10 ns1.webune.com.

mydomain.net.      A        192.168.1.135
www       A       192.168.1.135

;;;; MAKE MANUAL ENTRIES BELOW THIS LINE! ;;;;


as you can see my pri files have 192.168.1.135 address, so i want to change it to 192.168.1.112

so i would use these commands for the shell to go look in all my pri file where it find 192.168.1.135 and replace it with 192.168.1.112

Command:
for fl in pri.*; do
mv $fl $fl.old
sed ’s/192.168.1.135/192.168.1.112/g’ $fl.old > $fl
done
This command will make backup copies of the old files

Command WITHOUT MAKING BACKUPS:
for fl in pri.*; do
mv $fl $fl.old
sed ’s/192.168.1.135/192.168.1.112/g’ $fl.old > $fl
rm -f $fl.old
done


so now when i view the file called pri.mydomain.com it looks like this:

pri.mydomain.com
$TTL        86400
@       IN      SOA     ns1.webune.com. admin.mydomain.net. (
                        2006101601       ; serial, todays date + todays serial #
                        28800              ; refresh, seconds
                        7200              ; retry, seconds
                        604800              ; expire, seconds
                        86400 )            ; minimum, seconds
;
                NS      ns1.webune.com.              ; Inet Address of name server 1
                NS      ns1.webune.com.              ; Inet Address of name server 2
;

www  MX      10 ns1.webune.com.

mydomain.net.      A        192.168.1.112
www       A       192.168.1.112

;;;; MAKE MANUAL ENTRIES BELOW THIS LINE! ;;;;


- that was easy
Share:
BBCODE:
HTML Code:


Tue May 20, 2008 4:51 pm
1
cyberline
Reply #2032
will this linux command work also to replace text in many files?

i have alot of files in my system that i want to update all at once at the same time, can anyone tell me if this will work?

thanks
Tue Jun 24, 2008 11:18 am
2
unix question
Reply #2084
me too, i need a command in which i can replace the ip address in all my .pri files in my named directory for all my websites,

you see i need to change the ip address

lets say in my primary zone files, i have the ip address of : 192.168.1.120

and i need to have a automatic way of the system to look for any instances where 192.168.1.120 and replace it with 192.168.1.113 which is my new ip address

so i want the gawk to go inside each files (which is like 800 files) and whenever it finds 192.168.1.120 i want it changed

how can i do that?
Wed Jun 25, 2008 8:15 pm
3
sajal
Reply #2085
Picture 25--linux-shell.jpg
yes,i like to find what is this comand you say. please email me the information

What do you think?

* name:  

* email:  

* Please enter comments:


Receive Replies on my Comments
(An email will be sent to you when someone replies to your comments)

Add image to comments
yes no             upload
     
  1. Linux Shell Command - Find And Replace Text Within Files
  2. Linux Command To Locate And Replace Text