Welcome to the Linux Foundation Forum!

I want to append a line after the line containing the last occurence of a string in a file.

Basically, I want to append a line after the line containing the last
occurence of a specific string by without affecting the number of
newline characters.

So, its not just about appending here. I want to
delete all the lines from bottom to the line containing the last
occurence of the word, and then append the line and finally #EOF.

Initially:

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

EOF

Desired output:

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

EOF

Comments

  • ...And what have you done to achieve this?...

  • I want to append after deleting everything after the line containing the last occurence of the string,
    So I tried the following but it removes all the lines and appends only the line that I want to insert.

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

  • Have you tried asking your question here: https://stackoverflow.com/

  • Yeah i did ask there too. Tried out their suggestions, but didn't workout. Im trying on my own few more times, then I will revert
    Thanks

Categories

Upcoming Training