Solved

awk search and replace

Posted on 2006-06-27
6
872 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

830 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