?
Solved

awk search and replace

Posted on 2006-06-27
6
Medium Priority
?
875 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 500 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 500 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
Stack Overflow Podcast - Developer Story

Welcome to the Stack Overflow podcast recorded Thursday July 20 at Stack Overflow Headquearters in NYC. Your hosts today are podcast regulars Jay Hanlon, David Fullerton, and Ilana Yitzhaki, plus the quite irregular Matt Sherman (Stack Overflow Engineering Manager extraordinaire)

 
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

Automating Terraform w Jenkins & AWS CodeCommit

How to configure Jenkins and CodeCommit to allow users to easily create and destroy infrastructure using Terraform code.

Question has a verified solution.

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

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…
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

752 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