LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 04-08-2011, 12:04 PM   #1
NetRock
Member
 
Registered: Mar 2010
Posts: 134

Rep: Reputation: 16
Question insert text before the First Pattern Found


Hi
i need to add this line
nameserver 208.67.122.221 from ISP before the first nameserver already exists in resolv.conf in all workstations i know
Code:
sed '/nameserver/ i\nameserver 208.67.122.221'
but that insert it on every line after nameserver not only before the first one
Code:
# blah blah
# blah blah
domain mydom.com
nameserver 111.114.110.21
nameserver 111.116.66.21
nameserver 111.117.120.21
.......... ..............
.........  ..............
# blah blah
Thank you for your help.
 
Old 04-08-2011, 12:40 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
Try this.
Code:
sed '0,/nameserver/ { /nameserver/i nameserver 208.67.122.221
}' file
First, match from start-of-file to nameserver, then match again inside that selection. Unfortunately though, the a,i,c commands terminate on newlines, so you have to physically break the line in order to finish up the expression.

Your shell allows a bit of a trick though that will allow you to do it on one line:
Code:
sed $'0,/nameserver/ { /nameserver/i nameserver 208.67.122.221 \n }' file
$'..' is an extended quoting system that expands backslashed characters like \n, similar to echo -e.

Edit: Here's another way to do it that doesn't require as much thinking:
Code:
sed -r "0,/nameserver/ s/(.*nameserver.*)/nameserver 208.67.122.221\n\1/" file

Last edited by David the H.; 04-08-2011 at 12:46 PM. Reason: as stated, plus fixed mistake
 
1 members found this post helpful.
Old 04-08-2011, 12:41 PM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,698

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
Not sure with sed. There is a 'q' for quitting but then I could only get to first nameserver and it stopped
Code:
awk '!a && /nameserver/{print "nameserver 208.67.122.221";a=1}1' file
 
1 members found this post helpful.
Old 04-08-2011, 01:22 PM   #4
NetRock
Member
 
Registered: Mar 2010
Posts: 134

Original Poster
Rep: Reputation: 16
Thanks for your replies.
All i need it just get to the first nameserver in the file and modify the file, That's ALL
so from:

Quote:
# blah blah
# blah blah
domain mydom.com
nameserver 111.114.110.21
nameserver 111.116.66.21
nameserver 111.117.120.21
.......... ..............
......... ..............
# blah blah
to this:

Quote:
# blah blah
# blah blah
domain mydom.com
nameserver 208.67.122.221
nameserver 111.114.110.21
nameserver 111.116.66.21
nameserver 111.117.120.21
.......... ..............
......... ..............
# blah blah

Last edited by NetRock; 04-08-2011 at 01:25 PM.
 
  


Reply

Tags
sed


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Insert a column of text to a text file docaia Linux - Newbie 5 06-02-2011 11:55 AM
[SOLVED] Insert line of text prior to pattern only once grail Programming 5 06-29-2010 12:10 PM
Sed to read the file and replace and insert the pattern saurabhchokshi Programming 2 06-12-2009 02:15 PM
replace a text pattern with the reverse of another text pattern lothario Linux - Software 5 07-25-2008 03:43 PM
Need text pattern compare script kscott121 Linux - Software 4 05-10-2004 02:13 PM


All times are GMT -5. The time now is 12:29 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration