• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 423
  • Last Modified:

Linux replace entire line in every file

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
bdhtechnology
Asked:
bdhtechnology
2 Solutions
 
ozoCommented:
sed -i.bak 's/.*; Serial/20131215 ; Serial/' domain*.db
0
 
farzanjCommented:
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
 
bdhtechnologyAuthor Commented:
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

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now