?
Solved

Linux replace entire line in every file

Posted on 2013-12-14
3
Medium Priority
?
419 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 84

Accepted Solution

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

Assisted Solution

by:farzanj
farzanj earned 600 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
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.
Suggested Courses

762 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