Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

789 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