-1

Basically, I want to insert a line of text after the line containing the last occurence of a specific string, without affecting the rest of the file.

I imagine I should proceed like this:

  1. delete all lines after the last occurrence of the specified word
  2. append the desired string to the bottom of the file
  3. append back the previously deleted part, right after the newly inserted line

In my case, after the line containing the last occurrence of the specific word, there isn't any more text: just a certain number of empty lines and the special string #EOF on the very last line. It is important that this section is preserved, with exactly the same number of empty lines, and the #EOF tag intact.

This is a sample input file:

First line
Second line
Third line 1
Third line 2
Third line N



#EOF

For the sake of this example, I want to append a line with the text Fourth line right after the line containing the latest occurrence of the word Third. The result should be as follows:

First line
Second line
Third line 1
Third line 2
Third line N
Fourth line



#EOF

You can see that the number of newline characters after Fourth line stays the same as before.

Which command is the best one in this case? I tried the following but all the lines in the file were deleted and only Fourth line was appended.

sed -e :a -e '/^\n*$/{$d;N;};/\n$/ba' file
echo -e "\nFourth line\n\n\n#EOF" >> file

I am very new to shell scripting, therefore any help would be greatly appreciated.

  • I will edit the question. Third line is the last line containing the word "Third" – ShellyBelly Sep 4 '18 at 14:32
  • You don't specify three things: 1) Do you want to insert a line immediately after the last occurrence of the specific word or after the line containing the last occurrence of the specific word. 2) Is the rest of the file, after the line containing the last occurrence of the specific word, comprised exclusively of newlines and a final #EOF string? 3) Do you want to just insert one line or also perform further operations, like deleting some other lines? – simlev Sep 4 '18 at 15:01
  • 1
    1) I want to insert a line after the line containing the last occurence of the word 2) Rest of the file comprises only of newlines and #EOF 3) I just want to insert one line – ShellyBelly Sep 4 '18 at 15:03
  • You really need to clarify what you want here - your text says "delete all the blank lines after the last Third line including EOF" but your example shows replacement of only the first blank line – steeldriver Sep 4 '18 at 15:19
  • If you see clearly, I have kept the blank lines constant after adding "Fourth line". So its not replacement, its appending after the line containing the last occurence of a word without changing the number of newline characters – ShellyBelly Sep 4 '18 at 15:34
1

Question:

As I understand it, you are asking to insert a line after the last line where a certain word appears. What comes next in the file is of no importance, as long as it is preserved. Let's say the word is Third and the line to be inserted is Fourth line. Perl can be of help in this instance:

perl -0777 -pe 's/(.*Third[^\n]*)/$1\nFourth line/s' input.txt
  • I reframed my question with more specifics. I want to add a line after the last occurence of a string then add the same number of newlines as before, then add #EOF indicating end of file – ShellyBelly Sep 4 '18 at 14:39
  • @AkshayNandi What you say now is perfectly copmpatible with my understanding of the original question. The proposed solution stays the same. – simlev Sep 4 '18 at 14:55
  • Thanks I will try the solution and let you know. I am very new to shell scripting so need help – ShellyBelly Sep 4 '18 at 14:57
  • Hi simlev, does MacOS recognize perl command? I have tried but no positive results. Any advice? – ShellyBelly Sep 7 '18 at 14:09
  • @AkshayNandi I don't know much about MacOs, but found these instructions online. It states here that Perl comes preinstalled on Mac OS X. Please note this site is called AskUbuntu, and it is assumed all questions are related to Ubuntu or its derivatives. Perl comes preinstalled on most Ubuntu version or can be easily added with apt-get install perl. – simlev Sep 7 '18 at 14:19
0

You can simply insert lines on specific position like this:

sed '4iFourth Line' file

Or use perl's multiline matching capabilities if you don't know which line you need to insert.

perl -0777 -pe 's/(\n{2,}#EOF)/\nFourth Line$1/g' file

Explanation:

  • -0777 - slurp whole file (read as one line)
  • (\n{2,}#EOF) - match and capture 2+ consecutive \n followed by #EOF
  • \nFourth Line$1 - replace the match with \nFourth Line followed by the pattern match ($1).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.