Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 322
  • Last Modified:

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.
0
onaled777
Asked:
onaled777
  • 2
  • 2
4 Solutions
 
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
0
 
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.
0
 
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.
0
 
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.
0
 
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!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now