Solved

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

Posted on 2014-01-07
5
304 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Network Interface Card (NIC) bonding, also known as link aggregation, NIC teaming and trunking, is an important concept to understand and implement in any environment where high availability is of concern. Using this feature, a server administrator …
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
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.

679 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