How to Add Text After Last Pattern Match Using Ed

Append text after last occurance of matched line

sed is for simple substitutions on individual lines, that is all. For anything else you should be using awk:

$ awk 'NR==FNR{if (/#include/) nr=NR; next} {print; if(nr==FNR) print "\nusing namespace xyz;"}' file file
#include <string.h>
#include <stdio.h>

using namespace xyz;

// some functions
void blabla();

The above is using 2 passes - the first to find the line number where the last occurrence of #include appears in the file and storing it's line number in a variable named nr and then the 2nd to print "using ..." when that line number is hit on that 2nd pass. You can do it without specifying the file name twice by changing awk 'script' file file to awk 'BEGIN{ARGV[ARGC]=ARGV[1]; ARGC++} script' file if you prefer to duplicate the file name in the arguments list array.

Alternatively if the file's not huge you can read it all into memory and then do a substitution treating the whole file as a single string, e.g. with GNU awk for multi-char RS and gensub():

$ awk -vRS='^$' -voORS= '{print gensub(/(.*#include[^\n]+\n)/,"\\1\nusing namespace xyz;\n",1)}' file
#include <string.h>
#include <stdio.h>

using namespace xyz;

// some functions
void blabla();

With other awks you'd build up the string line by line into a variable then process that in the END section using match() and substr():

$ awk -v ORS= '{rec = rec $0 RS} END{ if (match(rec,/.*#include[^\n]+\n/)) rec = substr(rec,1,RSTART+RLENGTH-1) "\nusing namespace xyz;\n" substr(rec,RSTART+RLENGTH); print rec}' file
#include <string.h>
#include <stdio.h>

using namespace xyz;

// some functions
void blabla();

How to add to the end of lines containing a pattern with sed or awk?

This works for me

sed '/^all:/ s/$/ anotherthing/' file

The first part is a pattern to find and the second part is an ordinary sed's substitution using $ for the end of a line.

If you want to change the file during the process, use -i option

sed -i '/^all:/ s/$/ anotherthing/' file

Or you can redirect it to another file

sed '/^all:/ s/$/ anotherthing/' file > output

How to insert a line before the FIRST and LAST matching pattern using sed

Example how to do it in Perl

perl -nE'/Surname/&&($n++||say"Name")||($n=0);/Age/&&($g=1)||($g--&&say"Gender");print}{say"Gender"if$g'

And another way

perl -nE'/Surname/&&say("Name")..!/Surname/;/Age/&&($g=1)||($g--&&say"Gender");print}{say"Gender"if$g'

Insert line after match using sed

Try doing this using GNU sed:

sed '/CLIENTSCRIPT="foo"/a CLIENTSCRIPT2="hello"' file

if you want to substitute in-place, use

sed -i '/CLIENTSCRIPT="foo"/a CLIENTSCRIPT2="hello"' file

Output

CLIENTSCRIPT="foo"
CLIENTSCRIPT2="hello"
CLIENTFILE="bar"

Doc

  • see sed doc and search \a (append)

Using sed to insert text at end of line matching string

$ cat file
ITEMS="$ITEM1 $ITEM2 $ITEM3"
$ number=4
$ sed "/ITEMS/s/\"$/ \$ITEM$number&/" file
ITEMS="$ITEM1 $ITEM2 $ITEM3 $ITEM4"


Related Topics



Leave a reply



Submit