Solved

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

Posted on 2014-01-07
5
296 Views
Last Modified: 2014-01-30
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.
0
Comment
Question by:onaled777
  • 2
  • 2
5 Comments
 
LVL 31

Accepted Solution

by:
farzanj earned 125 total points
ID: 39763041
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
0
 
LVL 13

Assisted Solution

by:dhoffman_98
dhoffman_98 earned 125 total points
ID: 39763077
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.
0
 
LVL 31

Expert Comment

by:farzanj
ID: 39763096
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.
0
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 250 total points
ID: 39763182
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.
0
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 250 total points
ID: 39763395
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!
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

In this tutorial I will explain how to make squid prevent malwares in five easy steps: Squid is a caching proxy for the Web supporting HTTP, HTTPS, FTP, and more. It reduces bandwidth and improves response times by caching and reusing frequently-…
How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now