LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 12-29-2009, 08:54 PM   #1
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Angry ?? sed: how to use "1 s/" with -i to replace pattern only once?


I think I shouldn't need to be asking this, but I've been frigging around with this and reading sed tutorials for some time now, and I just can't get this to work.

In a text file, I have a bunch of identical lines, like this:


blah..

#STUB
#STUB
#STUB
#STUB
#STUB

more blah..

.. and I want to use sed -i to replace only the FIRST of those #STUB things, with a replacement.
It works in the console, without -i like so:

Code:
bash-3.1# echo "#RULESTUB
#RULESTUB
#RULESTUB
" | sed  "1 s/^#RULESTUB$/replacement/"

outputs:

replacement <--one replacement
#RULESTUB
#RULESTUB
but doing this from within a script, like so:
Code:
sed -i "1 s/^#STUB$/replacement/" $file
does not work. It doesn't replace anything. And if I remove the "1 " it replaces ALL the #STUB lines

What am I missing?

NOTE: if it matters, the sed operation is double quoted because in the real-world situation, 'replacement' is a $variable.

Sasha
 
Old 12-29-2009, 09:05 PM   #2
GooseYArd
Member
 
Registered: Jul 2009
Location: Reston, VA
Distribution: Slackware, Ubuntu, RHEL
Posts: 183

Rep: Reputation: 46
^ and $ are the beginning and end of line anchors. Your search pattern will only match a line that contains exactly "#STUB\n"
 
Old 12-29-2009, 09:06 PM   #3
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Original Poster
Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
That's *exactly* what the line(s) contains..
 
Old 12-29-2009, 09:10 PM   #4
GooseYArd
Member
 
Registered: Jul 2009
Location: Reston, VA
Distribution: Slackware, Ubuntu, RHEL
Posts: 183

Rep: Reputation: 46
in that case, double check your line endings and shell quoting (in case something is eating your end-of-line $), since I just pasted your example into a file here and it works alright.
 
Old 12-29-2009, 09:12 PM   #5
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Original Poster
Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Oh!! You may have just got it -- update in a moment...

Nope.. I had thought maybe the $ was being interpreted literally, but it isn't..

FWIW, the replacement operation works fine without the "1 " but it replaces ALL the instances of #STUB, instead of quitting after one replacement. So the regex isn't the problem; in fact, I have an earlier line(s) in the script which use a similar regex, and it works fine (but that earlier line is SUPPOSED to replace ALL instances).

Last edited by GrapefruiTgirl; 12-29-2009 at 09:15 PM.
 
Old 12-29-2009, 09:20 PM   #6
GooseYArd
Member
 
Registered: Jul 2009
Location: Reston, VA
Distribution: Slackware, Ubuntu, RHEL
Posts: 183

Rep: Reputation: 46
do you mean the first instance in a line, or the first instance in the file?
 
Old 12-29-2009, 09:22 PM   #7
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Original Poster
Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
First instance in the file. I think I may have identified the problem though.. It may be PEBKAC

Back in a moment.
 
Old 12-29-2009, 09:24 PM   #8
GooseYArd
Member
 
Registered: Jul 2009
Location: Reston, VA
Distribution: Slackware, Ubuntu, RHEL
Posts: 183

Rep: Reputation: 46
ah. make sure your quotes are escaped. I haven't got any idea about replace once per file.
 
Old 12-29-2009, 09:28 PM   #9
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Original Poster
Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
I'm relieved to NOT have to admit it was PEBKAC

But it isn't a quote-escape issue; I use double quotes often for stuff like this, when either the PATTERN or REPLACEMENT is a variable. But still, in this case, I'm stumped. It won't work...

Still trying though!

Thanks for the feedback so far.

Sasha
 
Old 12-29-2009, 09:38 PM   #10
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,355

Rep: Reputation: 1042Reputation: 1042Reputation: 1042Reputation: 1042Reputation: 1042Reputation: 1042Reputation: 1042Reputation: 1042
That "1" is not a match count, it is an address (line number). What's the first line of the file ?.
 
Old 12-29-2009, 09:41 PM   #11
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Original Poster
Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
@ syg00 -- Aha! So I *AM* doing this wrong after all.

You ask what's the first line of the file? You mean the line's contents? At this moment, the first line reads:

# # THE REAL ONE!
 
Old 12-29-2009, 10:03 PM   #12
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,696
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
awk
Code:
# more file
#STUB
#STUB
#STUB
#STUB
#STUB

$ awk '{s=(m>=1)?"#STUB":"new";}{m=m+sub("#STUB",s)}1' file
new
#STUB
#STUB
#STUB
#STUB

$ awk '/#STUB/ && !m{sub("#STUB","new");m+=1}m>=1' file
new 
#STUB
#STUB
#STUB
#STUB
 
Old 12-30-2009, 12:31 AM   #13
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,355

Rep: Reputation: 1042Reputation: 1042Reputation: 1042Reputation: 1042Reputation: 1042Reputation: 1042Reputation: 1042Reputation: 1042
Conceptually I prefer the latter, however in the case of the target line not being the first line, data will be lost. Slight modification perhaps ?
Code:
awk '/#STUB/ && !m{sub("#STUB","new");m+=1}1' file
 
Old 12-30-2009, 12:57 AM   #14
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,696
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by syg00 View Post
Conceptually I prefer the latter, however in the case of the target line not being the first line, data will be lost.
please give examples, so that i can do the modifications, since the solutions are all done with OP's sample.
 
Old 12-30-2009, 01:10 AM   #15
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,355

Rep: Reputation: 1042Reputation: 1042Reputation: 1042Reputation: 1042Reputation: 1042Reputation: 1042Reputation: 1042Reputation: 1042
As per post #7 from the OP:
Code:
# # THE REAL ONE!
#SLUB
#SLUB
#SLUB
...
Nothing to suggest there isn't more data lines before the required test data.
 
  


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] Need sed help: s/ command won't replace two occurrences of pattern on same line GrapefruiTgirl Programming 7 12-16-2009 02:08 AM
replace a pattern with a line using sed/awk lokeshn05 Linux - Newbie 3 05-06-2009 03:01 PM
How can sed (or any other such utility) replace `x\"y' with `x\\\"y'? lumix Linux - Newbie 1 05-13-2008 08:05 PM
replacement with sed: replace pattern with multiple lines Hcman Programming 5 11-18-2004 07:40 AM
can I replace text with the result of "wc" using sed? BrianK Linux - General 1 04-21-2004 01:15 PM


All times are GMT -5. The time now is 08:54 PM.

Main Menu
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