LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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, 11:04 AM   #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, 11:40 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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 11:46 AM. Reason: as stated, plus fixed mistake
 
1 members found this post helpful.
Old 04-08-2011, 11:41 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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, 12: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 12:25 PM.
 
  


Reply

Tags
sed



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 10:55 AM
[SOLVED] Insert line of text prior to pattern only once grail Programming 5 06-29-2010 11:10 AM
Sed to read the file and replace and insert the pattern saurabhchokshi Programming 2 06-12-2009 01:15 PM
replace a text pattern with the reverse of another text pattern lothario Linux - Software 5 07-25-2008 02:43 PM
Need text pattern compare script kscott121 Linux - Software 4 05-10-2004 01:13 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:52 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
Open Source Consulting | Domain Registration