Solved

Linux replace entire line in every file

Posted on 2013-12-14
3
410 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
Comment Utility
sed -i.bak 's/.*; Serial/20131215 ; Serial/' domain*.db
0
 
LVL 31

Assisted Solution

by:farzanj
farzanj earned 150 total points
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

743 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

14 Experts available now in Live!

Get 1:1 Help Now