Solved

awk search and replace

Posted on 2006-06-27
6
874 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
[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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Stressed Out?

Watch some penguins on the livecam!

Question has a verified solution.

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

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…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

724 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