Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > linux.debian.user > #243590
| From | Andy Smith <andy@strugglers.net> |
|---|---|
| Newsgroups | linux.debian.user |
| Subject | Re: Replace line in file based on pattern |
| Date | 2022-01-03 00:20 +0100 |
| Message-ID | <DBiiS-PG-13@gated-at.bofh.it> (permalink) |
| References | <DBi9c-Mv-5@gated-at.bofh.it> |
| Organization | linux.* mail to news gateway |
Hello, On Sun, Jan 02, 2022 at 05:52:36PM -0500, Paul M. Foster wrote: > 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 > > 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. Match the whole line, not just the string. sed -i 's/^.*search.*$/new line/' filename The ^ and $ anchor at start and end respectively and the .* matches everything. You can instead use sed's 'c' modifier to tell it to replace the whole line: sed -i '/search/new line/c' filename but I personally prefer to give exact matches. Cheers, Andy -- https://bitfolk.com/ -- No-nonsense VPS hosting
Back to linux.debian.user | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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