Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.debian.user > #243593

Re: Replace line in file based on pattern

From Greg Wooledge <greg@wooledge.org>
Newsgroups linux.debian.user
Subject Re: Replace line in file based on pattern
Date 2022-01-03 00:50 +0100
Message-ID <DBiLU-Z6-3@gated-at.bofh.it> (permalink)
References <DBi9c-Mv-5@gated-at.bofh.it> <DBisy-SG-3@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


On Sun, Jan 02, 2022 at 06:20:15PM -0500, The Wanderer wrote:
> If you mean that you want to only replace the first matching line in the
> file, but leave any subsequent matching lines alone... I've never
> actually had occasion to do that, but a bit of Googling (for 'sed first
> match only') found me
> 
> https://stackoverflow.com/questions/148451/how-to-use-sed-to-replace-only-the-first-occurrence-in-a-file
> 
> which looks like at least the seed of a way to do it; see also below.

Why does everyone think sed is an appropriate tool for this?  It's
some sort of malevolent meme, I swear.

unicorn:~$ printf %s\\n foo foo foo | awk '!done && /foo/{$0="bar"; done=1} 1'
bar
foo
foo

Of course, given the ORIGINAL question, the best tool is neither of these.
It's something like ansible or puppet.  Or if you insist on jury-rigging
crap together with stone knives and bearskins, ssh and patch.

See also <http://backreference.org/2011/01/29/in-place-editing-of-files/>.

Back to linux.debian.user | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Replace line in file based on pattern "Paul M. Foster" <paulf@quillandmouse.com> - 2022-01-03 00:10 +0100
  Re: Replace line in file based on pattern Andy Smith <andy@strugglers.net> - 2022-01-03 00:20 +0100
    Re: Replace line in file based on pattern "Paul M. Foster" <paulf@quillandmouse.com> - 2022-01-03 03:00 +0100
  Re: Replace line in file based on pattern The Wanderer <wanderer@fastmail.fm> - 2022-01-03 00:30 +0100
    Re: Replace line in file based on pattern Greg Wooledge <greg@wooledge.org> - 2022-01-03 00:50 +0100
      Re: Replace line in file based on pattern The Wanderer <wanderer@fastmail.fm> - 2022-01-03 02:20 +0100
        Re: Replace line in file based on pattern Greg Wooledge <greg@wooledge.org> - 2022-01-03 02:50 +0100
          Re: Replace line in file based on pattern "Paul M. Foster" <paulf@quillandmouse.com> - 2022-01-03 03:30 +0100
            Re: Replace line in file based on pattern Andrei POPESCU <andreimpopescu@gmail.com> - 2022-01-15 13:40 +0100
      Re: Replace line in file based on pattern "Paul M. Foster" <paulf@quillandmouse.com> - 2022-01-03 04:00 +0100
        Re: Replace line in file based on pattern Greg Wooledge <greg@wooledge.org> - 2022-01-03 05:10 +0100
          Re: Replace line in file based on pattern "Paul M. Foster" <paulf@quillandmouse.com> - 2022-01-03 14:40 +0100
            Re: Replace line in file based on pattern David Wright <deblis@lionunicorn.co.uk> - 2022-01-04 20:50 +0100
    Re: Replace line in file based on pattern "Paul M. Foster" <paulf@quillandmouse.com> - 2022-01-03 03:00 +0100
      Re: Replace line in file based on pattern john doe <johndoe65534@mail.com> - 2022-01-03 07:50 +0100

csiph-web