LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-15-2015, 11:49 AM   #1
RandomTroll
Senior Member
 
Registered: Mar 2010
Distribution: Slackware
Posts: 1,971

Rep: Reputation: 271Reputation: 271Reputation: 271
sed: how to replace only the first instance of a line


I want to use sed to replace the first instance of a line but not subsequent instances. My Linux books tell me that:

Code:
sed 's/apple/banana/1'
should do it. It replaces all instances instead. 'info sed' tells me that the number flag for the s command isn't specified in Posix, that GNU matches every instance beginning with the numberth. So I tried:

Code:
sed 's/apple/banana/1' file | sed 's/banana/apple/2'

but the latter instruction didn't change anything sed 's/apple/banana/2' changed no lines.

What's the right way?

The file contains 123 instances of apple.
 
Old 11-15-2015, 12:25 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
The number where you have it currently will at the line it is on and only replace the n'th item, so if you have a line:
Code:
apple pear apple
and use your first example you would get:
Code:
$ sed 's/apple/banana/1' fruit_file
banana pear apple
$ sed 's/apple/banana/2' fruit_file
apple pear banana
So the numbers relate to position on the line.

You want your script to stop after it makes the necessary change, so I would use:
Code:
sed '/apple/{s/apple/banana/1;q}' file
One of the gurus may have a way to shorten that, but it should work till then
 
1 members found this post helpful.
Old 11-15-2015, 01:07 PM   #3
RandomTroll
Senior Member
 
Registered: Mar 2010
Distribution: Slackware
Posts: 1,971

Original Poster
Rep: Reputation: 271Reputation: 271Reputation: 271
Quote:
Originally Posted by grail View Post
the numbers relate to position on the line.
Used sed for 20 years and didn't know this - never needed to. Thanks.
 
Old 11-15-2015, 01:32 PM   #4
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,805

Rep: Reputation: 1204Reputation: 1204Reputation: 1204Reputation: 1204Reputation: 1204Reputation: 1204Reputation: 1204Reputation: 1204Reputation: 1204
The q skips the remainder of the file, doesn't it.
The following prints the remainder
Code:
sed '1,/apple/ s/apple/banana/' file
 
Old 11-15-2015, 07:40 PM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,131

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
The quit is (very) useful on large files - especially if the target is near the beginning of the file. Update in-place (-i) avoids having to read the entire file.
Sometimes applicable, sometimes not.
 
Old 11-16-2015, 03:22 AM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,805

Rep: Reputation: 1204Reputation: 1204Reputation: 1204Reputation: 1204Reputation: 1204Reputation: 1204Reputation: 1204Reputation: 1204Reputation: 1204
? The q is efficient because it skips the remainder. But the OP says there are 123 instances, so I assume the following 122 should be kept.
? The -i option leads to short shell code. But still the entire file is to be read (unless a q is met).
 
Old 11-16-2015, 04:07 AM   #7
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,131

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
Sorry - I meant -i as well as quit. Very handy.
 
  


Reply



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] sed, grep,gawk :: Finding First Instance Of String Before A Given Line Number cin_ Programming 13 05-28-2014 07:04 AM
[SOLVED] SED replace line after match r00t Linux - Newbie 12 05-31-2012 07:17 AM
[SOLVED] Replace a line using sed finekevin Linux - Newbie 6 02-23-2012 01:14 PM
[SOLVED] sed to replace first and second instance mhouston100 Linux - Newbie 6 07-18-2011 02:00 AM
SED - replace line after substing rany Linux - Newbie 2 07-02-2009 01:13 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:34 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