LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-23-2022, 12:47 PM   #1
ddenial
Member
 
Registered: Dec 2016
Distribution: CentOS, Fedora, Ubuntu
Posts: 359

Rep: Reputation: 56
sed: insert a line after matching pattern in two lines. How?


Hello everyone.

Suppose I have a file with the following content:

Code:
$ cat thefile 
one
two
three
four
five
I want to search for the pattern that is spread across two lines and append a line after it. For example, I want to search for the patterns 'two' and 'three,' then append the line 'The new line' after that.

The output should be like this:
Code:
$ cat thefile 
one
two
three
The new line
four
five
I tried this command, and it is not working.
Code:
$ sed '/two\nthree/a\The new line/' thefile
I appreciate any solution.

Thanks
 
Old 10-23-2022, 01:47 PM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,357
Blog Entries: 3

Rep: Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767
There are probably several ways to do it, even with sed.

Code:
sed -e '/^two/ { n; /^three/ s/$/\nThe new line/ }' thefile
 
1 members found this post helpful.
Old 10-23-2022, 01:48 PM   #3
lvm_
Member
 
Registered: Jul 2020
Posts: 983

Rep: Reputation: 348Reputation: 348Reputation: 348Reputation: 348
Normally sed empties pattern space with each new line, but 'N' command appends next line into the pattern space, if you are determined to do it with sed you'll have to use it. But not me, I would've used awk here.

Code:
gawk '{print} /^one$/{one=1;next} /^two$/{if(one)print "the new line"} {one=0}'
 
1 members found this post helpful.
Old 10-23-2022, 02:37 PM   #4
ddenial
Member
 
Registered: Dec 2016
Distribution: CentOS, Fedora, Ubuntu
Posts: 359

Original Poster
Rep: Reputation: 56
Thank you
 
Old 10-23-2022, 07:53 PM   #5
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,832

Rep: Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219
Regarding sed:
Normally there is one line in the input buffer, and \n in a RE search does not work.
Only the N command appends another line to the input buffer, with an embedded \n that then can be used in a RE search.

A \n in a substitution string is specific to GNU sed.
A Unix/Posix sed needs the a(ppend) command and multi-line:
Code:
sed '
  /two/ {
    n
    /three/ a\
The new line
  }
' thefile
Code:
sed '
  /two/!b
  n
  /three/ a\
The new line
' thefile
 
1 members found this post helpful.
  


Reply



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] sed stop insert the line after the first occurrence of the pattern mackowiakp Linux - Newbie 12 12-27-2021 01:45 PM
SED one-liner for remove or comment out a line matching a certain pattern after the first line match Honest Abe Linux - Newbie 8 10-04-2018 05:36 AM
sed delete lines with pattern to pattern (exluding the second) Jykke Linux - Software 10 07-23-2018 02:43 AM
Scripting- sed command - replacing strings excluding the lines matching a pattern(s) msriram Linux - General 1 11-05-2012 09:29 AM
AWK/SED Multiple pattern matching over multiple lines issue GigerMalmensteen Programming 15 12-03-2006 05:08 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:21 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration