Grep for String and open at the corresponding line
You can load your grep
output in Vim's quickfix list with:
$ vim -q <(grep -rn --include="*.cpp" mystring)
Go to the next occurence with :cn
and to the previous occurrence with :cp
.
See :help -q
and :help quickfix
.
grep for a string in a line if the previous line doesn't contain a specific string
grep
is really a line-oriented tool. It might be possible to achieve what you want with it, but it's easier to use Awk:
awk '
/xyz/ && !skip { print }
{ skip = /jkl/ }
' file
Read as: for every line, do
- if the current line matches
xyz
and we haven't just seenjkl
, print it; - set the variable
skip
to indicate whether we've just seenjkl
.
How can I format my grep output to show line numbers at the end of the line, and also the hit count?
-n
returns line number.
-i
is for ignore-case. Only to be used if case matching is not necessary
$ grep -in null myfile.txt
2:example two null,
4:example four null,
Combine with awk
to print out the line number after the match:
$ grep -in null myfile.txt | awk -F: '{print $2" - Line number : "$1}'
example two null, - Line number : 2
example four null, - Line number : 4
Use command substitution to print out the total null count:
$ echo "Total null count :" $(grep -ic null myfile.txt)
Total null count : 2
How to grep for lines which contain particular words in a log file?
If you store your patterns in a file, one per line, you can use grep -f file-with-patterns file-to-search.log
From the man page:
-f FILE, --file=FILE
Obtain patterns from FILE, one per line. The empty file
contains zero patterns, and therefore matches nothing. (-f is
specified by POSIX.)
Edit 2018:
Since I wrote this, I have become aware of the following interesting edge cases:
- You can read the list of patterns from pipe using
-f -
(if you don't need stdin, i.e. you specified files on grep's command line) or-f <()
(in any case) grep
's performance starts to fail badly if hundreds of patterns are passed. If your use case is that insane, consider generating and immediately executing ased
(or some other language) script, although this could potentially have problems with overlapping patterns.
How to grep multi line string with new line characters or tab characters or spaces
@TLP was pretty close:
perl -0777 -nE 'say for map {s/^\s+|\s+$//gr} /\bdummy\(\s*"(.+?)"/gs' test.txt
test1
test2
Using
-0777
to slurp the file in as a single string/\bdummy\(\s*"(.+?)"/gs
finds all the quoted string content after "dummy(" (with optional whitespace before the opening quote)- the
s
flag allows.
to match newlines. - any string containing escaped double quotes will break this regex
- the
map {s/^\s+|\s+$//gr}
trims leading/trailing whitespace from each string.
Get line number while using grep
grep -n SEARCHTERM file1 file2 ...
How do you grep a file and get the next 5 lines
You want:
grep -A 5 '19:55' file
From man grep
:
Context Line Control
-A NUM, --after-context=NUM
Print NUM lines of trailing context after matching lines.
Places a line containing a gup separator (described under --group-separator)
between contiguous groups of matches. With the -o or --only-matching
option, this has no effect and a warning is given.
-B NUM, --before-context=NUM
Print NUM lines of leading context before matching lines.
Places a line containing a group separator (described under --group-separator)
between contiguous groups of matches. With the -o or --only-matching
option, this has no effect and a warning is given.
-C NUM, -NUM, --context=NUM
Print NUM lines of output context. Places a line containing a group separator
(described under --group-separator) between contiguous groups of matches.
With the -o or --only-matching option, this has no effect and a warning
is given.
--group-separator=SEP
Use SEP as a group separator. By default SEP is double hyphen (--).
--no-group-separator
Use empty string as a group separator.
grep: show lines surrounding each match
For BSD or GNU grep
you can use -B num
to set how many lines before the match and -A num
for the number of lines after the match.
grep -B 3 -A 2 foo README.txt
If you want the same number of lines before and after you can use -C num
.
grep -C 3 foo README.txt
This will show 3 lines before and 3 lines after.
Related Topics
Allocating a Data Page in Linux with Nx Bit Turned Off
Dockerfile Cmd 'Command Not Found'
Using Gzip to Compress Files to Transfer with Aws Command
Shuffle Output of Find with Fixed Seed
Hash ("#") Symbol in /Etc/Environment Causes String to Be Split
How to Print the Nth (5Th) Line of Every File Preceded by the Filename Using Any Linux Tool
Qserialport Cannot Open Tty After Application Has Previously Been Run by 'Root'
Sed,Awk.Grep - Add a Line to the End of a Configuration Section If the Line Doesn't Already Exist
Shebang Not Working to Run Bash Scripts in Linux
G++ Always Fails with Undefined Reference to _Unwind_Getipinfo
Dotnetcore: Cross Platform Version of Getinvalidfilenamechars
How to Keep Program Running in Background in Ash Shell
Django on Apache Wtih Mod_Wsgi (Linux) - 403 Forbidden
Linux Equivalent of Windows Dll Forwarders or MACos Reexport_Library
Docker Overlay2: Error Walking File System: Oserror [Errno 40] Too Many Levels of Symbolic Links