Tags: append, file, instance, linux, matching, occurs, patternthat, programming, sed, unix
Append only first instance with sed?
On Programmer » Unix & Linux
8,237 words with 9 Comments; publish: Tue, 29 Apr 2008 18:37:00 GMT; (20062.99, « »)
I'm trying to do an append with sed. My problem is that the pattern
that I'm matching occurs more than once in the file. I only want to
append the first time the pattern is found. Here is what I have:
sed -e "1,/^Listen/a\\
Listen $WWWIP:443
" </usr/local/apache/conf/ssl.conf >/tmp/ssl.conf.$$
Any help is greatly appreciated.
http://unix-linux.itags.org/q_unix-linux-programming_76726.html
All Comments
Leave a comment...
- 9 Comments

- 2004-06-30, 10:24(-05), glitch.unix-linux.itags.org.mlh.net:
> I'm trying to do an append with sed. My problem is that the pattern
> that I'm matching occurs more than once in the file. I only want to
> append the first time the pattern is found. Here is what I have:
> sed -e "1,/^Listen/a\\
> Listen $WWWIP:443
> " </usr/local/apache/conf/ssl.conf >/tmp/ssl.conf.$$
[...]
Use awk instead:
awk '{print}
/^Listen/ && already == 0 {
print "Listen " WWWIP ":443"
already=1
}' WWWIP="$WWWIP" < old-file > new-file
Stephane
#1; Tue, 29 Apr 2008 18:38:00 GMT

- In article <slrnce5n30.180.stephane.chazelas.unix-linux.itags.org.spam.is.invalid>,
Stephane CHAZELAS <this.address.unix-linux.itags.org.is.invalid> wrote:
>2004-06-30, 10:24(-05), glitch.unix-linux.itags.org.mlh.net:
>[...]
>Use awk instead:
This is the best advice I've seen posted in comp.unix.shell in quite some
time. Most of the time, if you try to use sed to solve a problem, all
you're doing is demonstrating to the world that you are into pain.
#2; Tue, 29 Apr 2008 18:39:00 GMT

- On Wed, 30 Jun 2004 20:24:38 GMT, Kenny McCormack <gazelle.unix-linux.itags.org.yin.interaccess.com> wrote:[colo
r=darkred]
>
> In article <slrnce5n30.180.stephane.chazelas.unix-linux.itags.org.spam.is.invalid>,
> Stephane CHAZELAS <this.address.unix-linux.itags.org.is.invalid> wrote:
> This is the best advice I've seen posted in comp.unix.shell in quite some
> time. Most of the time, if you try to use sed to solve a problem, all
> you're doing is demonstrating to the world that you are into pain.
>[/color]
Says someone who doesn't know sed. For someone who didn't know awk,
using IT would be a pain.
DUH
In this case, the OP *obviously* doesn't know either one of them.
DOUBLE DUH
This is the second post of yours today I have read that was just bullshit,
and the other one was downright mean.
Back to ignoring your posts I go. That includes those with your other
aliases.
AC
#3; Tue, 29 Apr 2008 18:40:00 GMT

- On Wed, 30 Jun 2004 10:24:26 -0500, glitch.unix-linux.itags.org.mlh.net <glitch.unix-linux.itags.org.mlh.net> wrote:
>
> I'm trying to do an append with sed. My problem is that the pattern
> that I'm matching occurs more than once in the file. I only want to
> append the first time the pattern is found. Here is what I have:
> sed -e "1,/^Listen/a\\
> Listen $WWWIP:443
> " </usr/local/apache/conf/ssl.conf >/tmp/ssl.conf.$$
> Any help is greatly appreciated.
Use ed instead. It stops after the first success, by default, unlike sed.
ed -s inputfile <<XXX
/^Listen/a
Listen $WWWIP:443
" </usr/local/apache/conf/ssl.conf >/tmp/ssl.conf.$$
.
wq
XXX
Ed rewrites the original file, so be careful. Make a copy.
With ed and sed and the shell, you can do anything that awk can do.
When using the a,c,i commands with sed, it is usually better to
put them in a file (no surrounding quotes) and call it with
sed -f file inputfile > outputfile
Or a here document, as above, (<<<XXX...XXX) which is basically the
same thing.
AC
ed(1) Check out the original tutorials by Brian W.
Kernighan at the Ed Home Page http://tinyurl.com/2aa6g
#4; Tue, 29 Apr 2008 18:41:00 GMT

- In article <aTFEc.452$R36.60.unix-linux.itags.org.newsread2.news.pas.earthlink.net>,
Alan Connor <xxxx.unix-linux.itags.org.yyy.zzz> wrote:
>On Wed, 30 Jun 2004 20:24:38 GMT, Kenny McCormack
><gazelle.unix-linux.itags.org.yin.interaccess.com> wrote:
>Says someone who doesn't know sed. For someone who didn't know awk,
>using IT would be a pain.
>DUH
>In this case, the OP *obviously* doesn't know either one of them.
>DOUBLE DUH
So where was your post showing how to do it with sed?
>This is the second post of yours today I have read that was just bullshit,
>and the other one was downright mean.
PKB
>Back to ignoring your posts I go. That includes those with your other
>aliases.
Welcome to the club Kenny!
Chuck Demas
Eat Healthy | _ _ | Nothing would be done at all,
Stay Fit | .unix-linux.itags.org. .unix-linux.itags.org. | If a man waited to do it so well,
Die Anyway | v | That no one could find fault with it.
demas.unix-linux.itags.org.theworld.com | \___/ | http://world.std.com/~cpd
#5; Tue, 29 Apr 2008 18:42:00 GMT

- In article <aTFEc.452$R36.60.unix-linux.itags.org.newsread2.news.pas.earthlink.net>,
Alan Connor <xxxx.unix-linux.itags.org.yyy.zzz> wrote:
>On Wed, 30 Jun 2004 20:24:38 GMT, Kenny McCormack <gazelle.unix-linux.itags.org.yin.interaccess.
com> wrote:
>Says someone who doesn't know sed. For someone who didn't know awk,
>using IT would be a pain.
>DUH
>In this case, the OP *obviously* doesn't know either one of them.
Thanks for making my point. The fact is that a lot of newbies don't really
know either, but get involved in using sed, because it is decepively simple
to use (for very simple tasks).
I.e., the only excuse for using sed for complex tasks is because one
already knows it well (in which case, there is no hope for you...)
#6; Tue, 29 Apr 2008 18:43:00 GMT

- In article <cbvcrn$6ve$1.unix-linux.itags.org.pcls3.std.com>,
Charles Demas <demas.unix-linux.itags.org.TheWorld.com> wrote:
...
>
>Welcome to the club Kenny!
'Tis an honor to be here.
#7; Tue, 29 Apr 2008 18:44:00 GMT

- On Wed, 30 Jun 2004 21:01:58 GMT, Alan Connor <zzzzzz.unix-linux.itags.org.xxx.yyy> wrote:
>
Because I grew sick of Charle's Dumas' abusive posts a long time ago,
I no longer read them.
Here's the post by "Kenny McCormack" that I was referring to in my
response to him:
<quote>
From: gazelle.unix-linux.itags.org.yin.interaccess.com (Kenny McCormack)
Newsgroups: comp.unix.shell
Subject: Re: stdout , stderr
Reply-To: gazelle.unix-linux.itags.org.interaccess.com
In article <57e25f29.0406301029.18360864.unix-linux.itags.org.posting.google.com>,
michael <mxa.unix-linux.itags.org.yahoo.com> wrote:
>Hi,
>I am using hpux and /proc/self and lsof are not available on hpux.
>I am willing to write a C or Java program to retrieve the stdout and stderr
>file name.
Generally, when people give answers that start with "on some systems, ...",
the implications are that:
1) There's no portable way to do it.
2) It probably doesn't work on your system unless your system is
Linux or, possibly, Solaris.
That said, it is doable under HPUX, but it is complicated, has nothing to
do with shell programming, and I'm not going to tell you how to do it.
</quote>
Almost always bullshit from him, teasing, but no answers. Just like in
his post to the OP on this thread. Check him out.
Once again, Charles Dumas comes to the support of trolls in order to post
nasty messages to me that I don't read.
Maybe he ought to change his name to "Dumbass"?
AC
#8; Tue, 29 Apr 2008 18:45:00 GMT

- Alan Connor <zzzzzz.unix-linux.itags.org.xxx.yyy> writes:
> Says someone who doesn't know sed. For someone who didn't know awk,
> using IT would be a pain.
Yeah.
awk '{print $4}'
is really complicated. Much too complicated for beginers to understand.
Stick with sed. Any language that has more manual pages (e.g. AT&T/Sun
manual page) covering error messages than covering syntax is my kind
of language.
Sending unsolicited commercial e-mail to this account incurs a fee of
$500 per message, and acknowledges the legality of this contract.
#9; Tue, 29 Apr 2008 18:46:00 GMT