Grep characters before and after match?
Asked Answered
S

9

220

Using this:

grep -A1 -B1 "test_pattern" file

will produce one line before and after the matched pattern in the file. Is there a way to display not lines but a specified number of characters?

The lines in my file are pretty big so I am not interested in printing the entire line but rather only observe the match in context. Any suggestions on how to do this?

Stlouis answered 12/11, 2011 at 1:5 Comment(1)
W
284

3 characters before and 4 characters after

$> echo "some123_string_and_another" | grep -o -P '.{0,3}string.{0,4}'
23_string_and
Writhe answered 12/11, 2011 at 1:19 Comment(9)
A good answer for small amounts of data, but it starts getting slow when you are matching >100 characters - e.g. in my giant xml file, I want {1,200} before and after, and it is too slow to use.Screamer
The awk version by @amit_g is much faster.Rustproof
Not available on Mac OSX, so really this is not a widely available solution. The -E version (listed below) is a better solution. What is -P? Read on ... -P, --perl-regexp Interpret PATTERN as a Perl regular expression (PCRE, see below). This is highly experimental and grep -P may warn of unimplemented features.Hereunto
Inexplicably, for me, this prints a certain number of lines of beautiful output, then says "Aborted", every time the same number of lines, which depends on what I'm searching for, but is never the full number of matches, by far. bash 4.1.2(1) and grep 2.6.3, CentOS 6.5.Bensen
The -E version below does not have this trouble, for some reason. Also, if I search for something that doesn't exist, I get only the Aborted line.Bensen
On OSX install via: brew install homebrew/dupes/grep and run it as ggrep.Bailsman
As implied by @Screamer this will be performance-wise impossible to use for huge files with moderately wide surroundings desired for the match target.Butyraceous
not working for me bash-5.1$ echo "some123_string_and_another" | grep -o -P '.{0,3}string.{0,4}' grep: unrecognized option: PHygrometric
Thank you for this! I wrote a quick script "grep_4CharsSurroundingResults.sh" so I never forget again :) (Sorry for formatting) #!/bin/bash grep -o -P '.{0,3}'$1'.{0,4}' $2 '''Ciprian
E
175
grep -E -o ".{0,5}test_pattern.{0,5}" test.txt 

This will match up to 5 characters before and after your pattern. The -o switch tells grep to only show the match and -E to use an extended regular expression. Make sure to put the quotes around your expression, else it might be interpreted by the shell.

Escent answered 12/11, 2011 at 1:26 Comment(2)
Good answer, interesting that it's capped at 2^8-1 for length in the {} so {0,255} works {0,256} gives grep: invalid repetition count(s)Kirkcudbright
This seems to get considerably less performant as I increase the number of matching chars (5 -> 25 ->50), any idea why?Flue
V
50

You could use

awk '/test_pattern/ {
    match($0, /test_pattern/); print substr($0, RSTART - 10, RLENGTH + 20);
}' file
Vertebral answered 12/11, 2011 at 1:17 Comment(5)
Works nicely even with somewhat bigger files alsoGallard
how can you use this to find multiple matches per line?Bonbon
What's the significance of the first number in the curly-bracketed pairs? Like the 0s in "grep -E -o ".{0,5}test_pattern.{0,5}" test.txt "?Schultz
It's really faster but not as accurate as @ekse's answer.Risarise
It's not at all accurate for large files. For a 5.5GB file that I'm sure has millions of matches, this command returned one result.Seeseebeck
T
35

You mean, like this:

grep -o '.\{0,20\}test_pattern.\{0,20\}' file

?

That will print up to twenty characters on either side of test_pattern. The \{0,20\} notation is like *, but specifies zero to twenty repetitions instead of zero or more.The -o says to show only the match itself, rather than the entire line.

Torture answered 12/11, 2011 at 1:20 Comment(2)
This command is not working for me: grep: Invalid content of \{\}Freberg
@AlexanderPravdin I think he's assuming that grep is is BRE(so, no -E no -P). echo zzzabczzzz | grep -o '.\{0,20\}abc.\{0,20\}' If it's ERE then the syntax is easier echo zzzabczzzz | grep -o -E '.{0,20}abc.{0,20}' likewise if it's PCRE then it's syntax as with ERE. echo zzzabczzzz | grep -o -P '.{0,20}abc.{0,20}' You can also do echo zzzabczzzz | grep -o -P '.abc..' adding or removing as many dots as you wantBesse
C
3

I'll never easily remember these cryptic command modifiers so I took the top answer and turned it into a function in my ~/.bashrc file:

cgrep() {
    # For files that are arrays 10's of thousands of characters print.
    # Use cpgrep to print 30 characters before and after search pattern.
    if [ $# -eq 2 ] ; then
        # Format was 'cgrep "search string" /path/to/filename'
        grep -o -P ".{0,30}$1.{0,30}" "$2"
    else
        # Format was 'cat /path/to/filename | cgrep "search string"
        grep -o -P ".{0,30}$1.{0,30}"
    fi
} # cgrep()

Here's what it looks like in action:

$ ll /tmp/rick/scp.Mf7UdS/Mf7UdS.Source

-rw-r--r-- 1 rick rick 25780 Jul  3 19:05 /tmp/rick/scp.Mf7UdS/Mf7UdS.Source

$ cat /tmp/rick/scp.Mf7UdS/Mf7UdS.Source | cgrep "Link to iconic"

1:43:30.3540244000 /mnt/e/bin/Link to iconic S -rwxrwxrwx 777 rick 1000 ri

$ cgrep "Link to iconic" /tmp/rick/scp.Mf7UdS/Mf7UdS.Source

1:43:30.3540244000 /mnt/e/bin/Link to iconic S -rwxrwxrwx 777 rick 1000 ri

The file in question is one continuous 25K line and it is hopeless to find what you are looking for using regular grep.

Notice the two different ways you can call cgrep that parallels grep method.

There is a "niftier" way of creating the function where "$2" is only passed when set which would save 4 lines of code. I don't have it handy though. Something like ${parm2} $parm2. If I find it I'll revise the function and this answer.

Ceria answered 4/7, 2020 at 2:21 Comment(0)
B
1

I personally do something similar to the posted answers.. but since the dot key, like any keyboard key, can be tapped or held down.. and I often don't need a lot of context(if I needed more I might do the lines like grep -C but often like you I don't want lines before and after), so I find it much quicker for entering the command, to just tap the dot key for how many dots / how many characters, if it's a few then tapping the key, or hold it down for more.

e.g. echo zzzabczzzz | grep -o '.abc..'

Will have the abc pattern with one dot before and two after. ( in regex language, Dot matches any character). Others used dot too but with curly braces to specify repetition.

If I wanted to be strict re between (0 or x) characters and exactly y characters, then i'd use the curlies.. and -P, as others have done.

There is a setting re whether dot matches new line but you can look into that if it's a concern/interest.

Besse answered 22/8, 2022 at 13:48 Comment(0)
J
1

If using ripgreg this is how you would do it:

grep -E -o ".{0,5}test_pattern.{0,5}" test.txt 
Jed answered 30/9, 2022 at 10:15 Comment(1)
You meant ripgrep, I suppose. I'd like to know, how is it different from grep? Your answer seems to be the exact same answer as ekse, except for the ripgrep specification.Fokine
S
0

With gawk , you can use match function:

    x="hey there how are you"
    echo "$x" |awk --re-interval '{match($0,/(.{4})how(.{4})/,a);print a[1],a[2]}'
    ere   are

If you are ok with perl, more flexible solution : Following will print three characters before the pattern followed by actual pattern and then 5 character after the pattern.

echo hey there how are you |perl -lne 'print "$1$2$3" if /(.{3})(there)(.{5})/'
ey there how

This can also be applied to words instead of just characters.Following will print one word before the actual matching string.

echo hey there how are you |perl -lne 'print $1 if /(\w+) there/'
hey

Following will print one word after the pattern:

echo hey there how are you |perl -lne 'print $2 if /(\w+) there (\w+)/'
how

Following will print one word before the pattern , then the actual word and then one word after the pattern:

echo hey there how are you |perl -lne 'print "$1$2$3" if /(\w+)( there )(\w+)/'
hey there how
Shawnee answered 14/3, 2017 at 11:25 Comment(0)
O
-1

You can use regexp grep for finding + second grep for highlight

echo "some123_string_and_another" | grep -o -P '.{0,3}string.{0,4}' | grep string

23_string_and

enter image description here

Oneal answered 29/7, 2019 at 10:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.