Grep characters before and after match?

前端 未结 7 1497
长情又很酷
长情又很酷 2020-12-02 04:19

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 di

相关标签:
7条回答
  • 2020-12-02 04:44

    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 patttern.
        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.

    0 讨论(0)
  • 2020-12-02 04:47

    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.

    0 讨论(0)
  • 2020-12-02 04:48

    You could use

    awk '/test_pattern/ {
        match($0, /test_pattern/); print substr($0, RSTART - 10, RLENGTH + 20);
    }' file
    
    0 讨论(0)
  • 2020-12-02 04:49

    3 characters before and 4 characters after

    $> echo "some123_string_and_another" | grep -o -P '.{0,3}string.{0,4}'
    23_string_and
    
    0 讨论(0)
  • 2020-12-02 04:55

    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

    0 讨论(0)
  • 2020-12-02 05:08
    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.

    0 讨论(0)
提交回复
热议问题