sed headaches: inserting lines upon singular matches in file (NOT per line)

After over eight hours of searching, I am throwing in the towel and creating a new question for this one. The operation is simple, but I am having the hardest time getting this to work correctly, having gone through seemingly every other solution on SO. I need two things:

1.) To insert a line before a line in which the FIRST MATCH of “PBS” in the entire file occurs. It should only happen once in the entire file. For some reason, every solution I’ve tried has ended up duplicating the insert for every occurrence in the file; I suspect, because sed is following on a per-line basis.

So this needs to happen. Original file:

stuff here
stuff here
PBS -N
PBS -V
stuff here

becomes:

stuff here
stuff here
inserted line
PBS -N
PBS -V
stuff here

2.) To append a line after a line in which the LAST MATCH of “PBS” in the entire file occurs. Same as before: it should only happen once in the entire file.

So this needs to happen:

stuff here
stuff here
PBS -N
PBS -V
stuff here

becomes:

stuff here
stuff here
PBS -N
PBS -V
inserted line
stuff here

All of the solutions I’m seeing online (and I have about twenty tabs open at this point) state that this should be relatively easy. I have no shame in declaring that sed is taking a toll on my self-esteem at this point… Thanks to anyone who can help


Download sed.headaches.inserting.lines.upon.singular.matches.in.file.not.per.line.zip
Direct Link


Download


Download sed.headaches.inserting.lines.upon.singular.matches.in.file.not.per.line.zip
Mediafire


Download


Download sed.headaches.inserting.lines.upon.singular.matches.in.file.not.per.line.zip
Kingfiles


Download

Both comments and pings are currently closed.

Comments are closed.