9

I want to search in a long sentence (more than 1024 letters).

I have one text file (test.txt) which has one long sentence, like this:

afdafglwqgkjrldjl;ewqje;'k;g;je;;;fdsgalsdkf;akslg;safdas.....dasfsd

Now I want to check which line contains the word saf. This command just shows the whole sentence:

less test.txt | grep saf

Is it possible to get a part of the sentence or should I use a command other than grep?

Worthwelle
  • 4,648
whitebear
  • 705
  • 4
  • 13
  • 28

2 Answers2

6

Not exactly what you were looking for: show the matching lines and highlight the occurences in those lines:

grep --color 'saf' test.txt

Options for searching saf and displaying up to 15 characters before and after the occurences found using:

  • the standard regex syntax, first mentioned by @kamil-maciorowski in his comment on the question:

    grep -o '.\{0,15\}saf.\{0,15\}' test.txt | grep saf --color
    
  • Perl-compatible regex syntax with the -P option, if available:

    grep -o -P '.{0,15}saf.{0,15}' test.txt | grep --color saf
    
  • extended regex syntax with the -E option, if your grep has no -P option (like e.g. on macOS):

    grep -o -E '.{0,15}saf.{0,15}' test.txt | grep --color saf
    
t0r0X
  • 209
0

bgrep if lines don't necessarily fit into memory

I keep coming back to this random repo from time to time: https://github.com/tmbinc/bgrep Install:

curl -L 'https://github.com/tmbinc/bgrep/raw/master/bgrep.c' | gcc -O2 -x c -o $HOME/.local/bin/bgrep -

Use:

bgrep `printf %s saf | od -t x1 -An -v | tr -d '\n '` myfile.bin

Sample output:

myfile.bin: c80000003
\x02abc
myfile.bin: c80000007
dabc

I have tested it on files that don't fit into memory, and it worked just fine.

I've given further details at: https://unix.stackexchange.com/questions/223078/best-way-to-grep-a-big-binary-file/758528#758528