Solved

awk search and replace

Posted on 2006-06-27
6
869 Views
Last Modified: 2010-05-18
Hi,

Can I use awk to change a file like the following?  I want to add the second line but am not getting correct results.  See below for my current script.

BEFORE:
domain foobar.com
nameserver 1.2.3.4
nameserver 2.3.4.5
nameserver 3.4.5.6

AFTER:
domain foobar.com
nameserver 5.5.5.5
nameserver 1.2.3.4
nameserver 2.3.4.5
nameserver 3.4.5.6

My current script _replaces_ line 2 instead of _inserting_ it.  Can anyone show me how to do it correctly?

awk 'NR==2{$1 = "nameserver"} NR==2{$2 = "5.5.5.5"}{print}' /etc/resolv.conf,backup
0
Comment
Question by:bryanlloydharris
  • 2
  • 2
  • 2
6 Comments
 
LVL 51

Assisted Solution

by:ahoffmann
ahoffmann earned 125 total points
ID: 16996245
awk '/^domain/{print; print "nameserver 5.5.5.5"}{print}' /etc/resolv.conf,backup
0
 
LVL 15

Accepted Solution

by:
bpmurray earned 125 total points
ID: 16996538
You're simply replacing the current values of line 2 in your code. What you really want to do is add an extra line:

awk 'NR==2{print "nameserver 5.5.5.5"}{print}' /etc/resolv.conf,backup
0
 
LVL 3

Author Comment

by:bryanlloydharris
ID: 16996755
Thanks to both for the tips.  It seems ahoffman that yours has a slightly strange added effect of adding domain.com twice.  But that's okay I can work with it.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 3

Author Comment

by:bryanlloydharris
ID: 16996783
Sorry ahoffman I meant it adds foobar.com twice.
0
 
LVL 15

Expert Comment

by:bpmurray
ID: 16997226
The fix for ahoffmann's script is to remove the first "print;" because the first line fits both the pattern and the section with no pattern. Actually, the section without a pattern should be first, or the extra nameserver line will be printed before the domain line.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 16998938
> It seems ahoffman that yours has a slightly strange added effect of adding domain.com twice.
sorry, printed a lazy solution just out of my finger tips, there should be a
 next;
at end of the first block

> The fix for ahoffmann's script is to remove the first "print;"
yes, no, perhaps
that's a dirty solution 'cause it breaks the order of the keywords
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

746 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

13 Experts available now in Live!

Get 1:1 Help Now