Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search a file in linux and jump to position of the first find

I need help with a linux command with which I can search for a string in a text file and go to the first position this string is found and being able to scroll down and up from there. I am looking for hours for such command. GREP will only find the occurrences, print them and exit the file. I need to be able to jump to that line and being able to scroll up and down in order to see the other lines around.

like image 849
Lu Popov Avatar asked Mar 21 '23 14:03

Lu Popov


1 Answers

You can do that with less or vim, for example:

less +/pattern file.txt
vim +/pattern file.txt

This will open file.txt for editing and jump to the first match of "pattern", if such exists. You can move up or down with the arrow keys or with j and k.

The pattern can be a basic regular expression (BRE) that less and vim understand. For example:

less +/^settings file.txt
vim +/^settings file.txt

this will jump to the first line that starts with the string "settings".

like image 75
janos Avatar answered Apr 09 '23 23:04

janos