Solved

Linux replace entire line in every file

Posted on 2013-12-14
3
412 Views
Last Modified: 2013-12-15
I am trying to find a way to replace the entire line that matches a given string in a lot of files.  The line will have a random value that I need to replace but will end with the string '; Serial'.  The replacement will be the same for all files.

What I am trying to do is replace the zone serial number in the bind zone files.  I am migrating to a new host IP so have updated all the IP addresses.  Now I want to update the serial so I don't have to edit by hand hundreds of files.

Here is a sampling of the values:
#grep -i -e '; Serial' *

domain1.db:                    2008092501      ; Serial
domain2.db:                  2013040901      ; Serial
domain3.db:                       2012051901      ; Serial
domain4.db:                    2012123101      ; Serial
domain5.db:                     2012072601      ; Serial

Open in new window


And I want to replace them all with:
20131215 ; Serial

I am sure there is a way to do this with sed or similar script but I have not been able to find a way thus far.
0
Comment
Question by:bdhtechnology
3 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 350 total points
ID: 39719081
sed -i.bak 's/.*; Serial/20131215 ; Serial/' domain*.db
0
 
LVL 31

Assisted Solution

by:farzanj
farzanj earned 150 total points
ID: 39719083
files=$(grep -RP ';\s*Serial)

for f in $files
do
     perl -pe "s/[0-9]{8}\s*;Serial/20131215;Serial/" "$f"
done

This is  just to show you without actually modifying it.  If you are happy with it, you can use -i option in the Perl command

perl -i -pe ....
0
 
LVL 1

Author Comment

by:bdhtechnology
ID: 39720194
The sed oneliner worked perfectly, thank you.  I should have known this was more of a regex question.

I didn't test the perl option, though it looks like it would work.

Thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now