Solved

Linux replace entire line in every file

Posted on 2013-12-14
3
417 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 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
shell script 2 52
Programming Codes 2 45
Hive vs Impla in Hadoop 1 80
UM7 Stop Sending packets with GPS Sensor 1 21
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …

726 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