How to add text after last pattern match using ed
问题 This is how I am adding text before first pattern, I want to add after last pattern FILE_NAME="folder/myfile.c++" STR_TO_ADD="string that i want to add" PATTERN="banana" ed $FILE_NAME 2>NULL 1>NULL<<EOF /^$PATTERN -1 a $STR_TO_ADD . wq EOF file banana apple banana one two three Expected output banana apple banana string that i want to add one two three 回答1: Go to last line of file and search pattern in backward direction. FILE_NAME="folder/myfile.c++" STR_TO_ADD="string that i want to add"