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


Groups > linux.debian.user > #243591

Re: Replace line in file based on pattern

From The Wanderer <wanderer@fastmail.fm>
Newsgroups linux.debian.user
Subject Re: Replace line in file based on pattern
Date 2022-01-03 00:30 +0100
Message-ID <DBisy-SG-3@gated-at.bofh.it> (permalink)
References <DBi9c-Mv-5@gated-at.bofh.it>
Organization This space intentionally left blank.

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

On 2022-01-02 at 17:52, Paul M. Foster wrote:

> Folks:
> 
> In a script, I'd like to search for a pattern in a file, and replace
> that line entirely with a new line, once (not globally). I've tried

What do you mean by "globally"?

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.

> sed -i s/search/new_line/
> 
> but this only replaces the string itself. I want the script to find the 
> line my search term is on, and replace the whole line with my 
> replacement line.
> 
> Anyone know how to do this?

Without re-testing at the moment, I'd say from past experience that you
want:

sed -i 's/^.*search.*$/new_line/'

This matches the beginning of the line, followed by zero or more
characters, followed by the search string, followed by zero or more
characters, followed by the end of the line.

Whether or not the quotes are necessary depends on what your 'search'
and 'new_line' strings consist of, but I habitually include them just to
be on the safe side.

There may well be ways to do this more readily using other tools, but I
don't know them off the top of my head, and that should be the way to do
it using sed.


Combining this with the suggestion from the link above would produce
something like:

sed -i '0,/^.*search.*$/{s/^.*search.*$/new_line/}'

I have *not* tested that, since I don't have a suitable test input file
ready to hand and don't feel like throwing one together right at the
moment, but it looks like it should produce the desired result.

-- 
   The Wanderer

The reasonable man adapts himself to the world; the unreasonable one
persists in trying to adapt the world to himself. Therefore all
progress depends on the unreasonable man.         -- George Bernard Shaw

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