Linux Command to copy text from the last occurrence of a particular text.

Hello

Can any one share with me a command to use while in the vi editor that does the following:
Given the text 'startup trace' copy from the last occurrence of that string to the end of the file.

Thank you for any assistance you can provide.
onaled777Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

farzanjCommented:
In vi, if you want to get to the last occurance of this string, you have to find it with command

?startup trace

You can now highlight it by pressing v first and then space bar until it is completely highlighted. Once highlighted, press y

If you do not have visual mode, you can press instead
2yw


Then keep in command mode and press G$  (G is upper case) and the press p.


So basically

Esc
?startup trace
Enter key
now cursor should be before startup trace
2yw
G$p

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dhoffman_98Commented:
Start from the end of the file by typing a capital 'G' which will take you to the last line.

Then type a question mark followed by your search text and press enter.
?startup trace

Then if you want to select from that line until the end of the file you can type the following:

:.,$y

That's colon, dot, comma, dollar sign, y.

The colon starts a command.
The dot selects the current line.
The comma shows you are selecting a range of lines.
The dollar sign sets the end of the range as the last line of the file.
The y 'yanks' the set of those lines... essentially the same as a COPY.

Then you move to where you want to be, and hit the 'p' key to paste what you copied.

Of course, this is assuming you are inside the same file.
farzanjCommented:
In my solution above, I am assuming you just opened the file so you are at the beginning of the file.  If you are not, you can press
gg

To go to the beginning of the file.  You can also press G to go to the end of the file.  That would work too.
woolmilkporcCommented:
All in one:

<ESC>:$;?startup trace?;.t$<ENTER>

means:

hit the <ESC> key to make sure you're in command mode, then type : to go to the command line, then type $;?startup trace?;.t$ and hit the <ENTER> key.

It doesn't matter where in the file the cursor is positioned.

You can also map the whole thing to a particular key (sequence), let's say <Ctrl><A>:

:map ^A :$;?startup trace?;.t$^M

Create ^A by pressing <Ctrl><V> then <Ctrl><A>
and create ^M by pressing <Ctrl><V> then <ENTER>

Now the sequence of commands will be executed every time you press <Ctrl><A>.

You can add the above to the .exrc file in your home directory to make the mapping permanent.
woolmilkporcCommented:
Or do you mean "copy everything from the last occurrence of the pattern up to the end of the file to a new file"?

This can be done with "tac" and "sed":

tac inputfile | sed '/startup trace/q' | tac > newfile

With vi:

<ESC>:$;?startup trace?;.,$w newfile

Explanation: See above!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.