Solved

awk search and replace

Posted on 2006-06-27
6
873 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
Independent Software Vendors: 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

Industry Leaders: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Shell script awk command 2 122
what is this linux system is? 16 123
BASH script to modify crontab? 3 114
looking for a CENTOS ISO to download with x window installed 2 93
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 demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Suggested Courses

739 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