LinuxQuestions.org
Help answer threads with 0 replies.
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-30-2009, 01:50 AM   #16
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241

sorry, so what should be the output of your example? i don't understand post #7, all i did understand is OP want to change only the first instance of "STUB". pls correct me again if i am wrong.
Code:
$ awk '/#STUB/ && !m{sub("#STUB","new");m+=1}1' file
# # THE REAL ONE!
new
#STUB
#STUB
 
Old 12-30-2009, 02:53 AM   #17
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,286

Rep: Reputation: 1030Reputation: 1030Reputation: 1030Reputation: 1030Reputation: 1030Reputation: 1030Reputation: 1030Reputation: 1030
True - but that is my adjustment to your code, not your original offering ...
 
Old 12-30-2009, 06:14 AM   #18
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
to change only the first match, you have to add the quit command like

Code:
sed -i '/pattern/{ s/.../.../; q; }' file
 
Old 12-30-2009, 06:27 AM   #19
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,286

Rep: Reputation: 1030Reputation: 1030Reputation: 1030Reputation: 1030Reputation: 1030Reputation: 1030Reputation: 1030Reputation: 1030
Hmmm - did you test that ?.
I'm a big fan of sed, but use the right tool for the job. Personally I'd probably use perl, but I'm happy to continue to get my meagre awk knowledge improved by ghostdog74 and a couple of others on this list.
 
Old 12-30-2009, 10:07 AM   #20
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Original Poster
Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
OK, first, thanks for the continued input everyone! I took a leave last night when Star Wars came on the TV at midnight..

Personally, I feel that sed *is* the right tool for this job, but I just haven't got it quite right yet and, of course, maybe AWK is better for this, and I just don't know it yet. Given what sed can do, I just don't understand why it won't do this properly. So, I have not *yet* tried the AWK ideas here, because I want to use sed and/or rule it out as an option.

For the record/clarification, here's a better representation of what the target file looks like:

Code:
data...

data...
...
data...

...
...
data...
#PATTERN
#PATTERN
#PATTERN
data...
data...
...
#EOF
Konsolebox has got me the closest using sed:

Code:
sed -i "/#PATTERN$/ {s/^\#PATTERN$/$replacement/; q;}" $file
trouble with that is, it truncates the file after matching the pattern and doing the operation, leaving the edited line as the LAST line in the target file. No good.

At least I have learned one way to use the 'q' option. Maybe moving the 'q' around will help.. Still at it.

Sasha

Last edited by GrapefruiTgirl; 12-30-2009 at 10:13 AM.
 
Old 12-30-2009, 11:37 AM   #21
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 70
Quote:
Originally Posted by GrapefruiTgirl View Post
Personally, I feel that sed *is* the right tool for this job,
I disagree.
Code:
sed -e "/^#PATTERN$/{x;s/././;x;t;s/^#PATTERN$/$replacement/;h}" data
Much less elegant than awk.
 
Old 12-30-2009, 12:31 PM   #22
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
How do I match only the first occurrence of a pattern?

I too think sed is the better tool, as it's a just a simple line match and substitution. You just need to know how to do it right.

Code:
sed '0,/^#STUB$/ s//#FOO/'
 
1 members found this post helpful.
Old 12-30-2009, 12:58 PM   #23
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Original Poster
Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
@ David.The.H -- Thank you,

You know, I have that very link open already (and a few others too), yet for some reason, missed that part (or subconsciously neglected to pay attention to it :/).

I will be trying it as soon as I finish working on this OTHER part of the same program. Hopefully it will work, as it is simpler than awk too.

Meanwhile, I had come up with another solution, which I may now (probably will) toss in favor of this method.
What I came up with, was to simply have only ONE of the pattern line in the target file; so the sed operation would find that line, and replace it with the new data PLUS a newline PLUS a new pattern line, so that future operations would work.
Like so:

replace:
#STUB

with:
New data blah blah blah\n#STUB

It works great; only questionable thing with it is that, if for some reason Joe User decides to create multiple copies of the #STUB target line in his copy of the target file, then I'm half-way back to the start of this all; multiple replacements will get made.

So, by switching to the 0, method you gave, I will be assured the replacement will only happen once, regardless.

I'll update again later on this.

Thanks again to everyone who helped out here,

Sasha
 
Old 12-30-2009, 04:54 PM   #24
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,286

Rep: Reputation: 1030Reputation: 1030Reputation: 1030Reputation: 1030Reputation: 1030Reputation: 1030Reputation: 1030Reputation: 1030
The things you learn - didn't know about that: thank you @David the H. GNU sed has many niceties that aren't available everywhere - I have a *very* non-GNU sed requirement sometimes. Hence my desire for other solutions rather than resorting to figuring out ugly hacks as per @osor.
 
Old 12-31-2009, 02:22 AM   #25
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by syg00 View Post
Hmmm - did you test that ?.
I haven't. Sorry. Too bad it didn't work. It's just that I sort of remember using it multiple times. Maybe it works on one way streams and not with the '-i' option. I might have also added some alternations like '!' and 'b'.

Quote:
Originally Posted by syg00 View Post
I'm a big fan of sed, but use the right tool for the job. Personally I'd probably use perl, but I'm happy to continue to get my meagre awk knowledge improved by ghostdog74 and a couple of others on this list.
Well everyone's got their ways. On my side, I also thought that it was just simple and that sed should really be the only right tool for the job as based on the original requirements. I think other tools will just appear as heavier. Just my taste though. I have the tendency to be critical with balancing speed, readability, etc. I'd prefer to use awk if it's just got a bit more complicated. For example, input text may also contain characters that can give conflict with sed's syntax.
 
Old 12-31-2009, 02:33 AM   #26
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Original Poster
Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
In this case, for the record, speed is a factor in considering which tool I use.
I have replaced dozens of awk operations in this program with `sed` or `tr` equivalents, because my testing showed that sed is usually faster than awk when doing relatively simple text substitutions, and tr is probably faster than both, but the difference was immeasurable in my testing.

I still have not yet tried the 0, method with sed in this particular scenario; other work has been keeping me busy. But I will update when I do try it.

Sasha
 
Old 12-31-2009, 02:52 AM   #27
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,286

Rep: Reputation: 1030Reputation: 1030Reputation: 1030Reputation: 1030Reputation: 1030Reputation: 1030Reputation: 1030Reputation: 1030
FWIW, my testing on (really) big files showed perl faster than sed - most surprised was I.
Maybe if you can consolidate a lot of those calls into one (perl ???) you'll save even more in the "setup/teardown" overheads of lots of small calls.
 
Old 01-01-2010, 05:39 AM   #28
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by syg00 View Post
FWIW, my testing on (really) big files showed perl faster than sed - most surprised was I.
Maybe if you can consolidate a lot of those calls into one (perl ???) you'll save even more in the "setup/teardown" overheads of lots of small calls.
Truly reasonable if you're going to use sed multiple times inside a script. I wonder though what's quicker by average, perl or awk...? I think awk's pretty quick with text processing even with hashes (similarly associative arrays in bash). How 'bout in perl?... Maybe it depends on the implementation but awk already does the compilation of the code before executing.
 
  


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 04:05 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