0

I can use below command to find specific text in a file

grep -w text-to-find filename

can anyone help me with a command wherein I can move my cursor to a specific position using cat filename.

Like I have opened a file using cat command and I want to move my cursor to some text which can be placed anywhere in the file.

I like to move my cursor to that position and scroll upwards from that position in the file.

  • 4
    What is the bigger picture? Do you want to edit the file? to select and copy-paste from that area of the output? cat will dump the content, and after that, moving just the cursor of the terminal will not be very useful, and you will have maybe unwanted behaviour after typing any more characters into that terminal. So I think it is helpful to add what do you want to do by this, what is the next move. – thanasisp Dec 23 '20 at 16:51
  • 3
    I have a feeling you want something like less -p text-to-find filename – jesse_b Dec 23 '20 at 16:56
  • its working same like grep. I like to move my cursor to that position and scroll upwards from that position in the file.. This file is so huge so I would like to go back to some position and need to scroll upwards from there – vikrant rana Dec 23 '20 at 17:13

1 Answers1

2

While the cat command by itself can't quite get you what you need, there are multiple possibilities.

First, assuming the file isn't too large to fit in memory:

  • As jesse_b mentioned in the comment, less -p text-to-find filename will work.
  • Personally, I would recommend you add tmux to your normal workflow, as it allows you to enter a "scrollback" mode (called "copy mode") where you can search backwards. Default keybinding Ctrl+[ to enter copy mode, then Ctrl+r to search backward. This assumes that the text is in the default history-limit of 2000 lines or you have extended it in .tmux.conf.
  • Use a terminal with a searchable scrollback buffer (e.g. see this answer)

But assuming that the file is simply huge and you need to stop listing it where the text is found and scroll up from there:

  • grep --before-context 50 -w text-to-find filename will show the 50 lines before text-to-find, as well as the text-to-find line (--before-context can be abbreviated -B). Note that it will result in multiple results if text-to-find isn't unique.
  • sed '/text-to-search/q' FILE (loads a line, prints it, until it finds the line with the text you are looking for then quits printing).
  • awk can do it as well.

See this answer for more details and alternatives.

NotTheDr01ds
  • 3,547