Skip to main content
Clarify explanation
Source Link
Chris Davies
  • 128.1k
  • 16
  • 179
  • 324

A standard approach could be:

what_we_want='123456789'
context='firstname'
distance=10
grep -E -e "${context}" -C "${distance}" file_to_look_into | grep -E -e "${what_we_want}" -C "${distance}"

The first ensure that we only look $distance lines around $context matching lines, the 2nd then see if it find "$what_we_want" in those 2*$distance+1 lines. If you just want the matching line as a result, drop the -C "${distance}" from the 2nd grep.

The first grep ensures that we only look $distance lines around $context matching lines. The 2nd then sees if it find $what_we_want in those 2*$distance+1 lines.

If you just want the matching line as a result, drop the -C "${distance}" from the 2nd grep.

A standard approach could be:

what_we_want='123456789'
context='firstname'
distance=10
grep -E -e "${context}" -C "${distance}" file_to_look_into | grep -E -e "${what_we_want}" -C "${distance}"

The first ensure that we only look $distance lines around $context matching lines, the 2nd then see if it find "$what_we_want" in those 2*$distance+1 lines. If you just want the matching line as a result, drop the -C "${distance}" from the 2nd grep.

A standard approach could be:

what_we_want='123456789'
context='firstname'
distance=10
grep -E -e "${context}" -C "${distance}" file_to_look_into | grep -E -e "${what_we_want}" -C "${distance}"

The first grep ensures that we only look $distance lines around $context matching lines. The 2nd then sees if it find $what_we_want in those 2*$distance+1 lines.

If you just want the matching line as a result, drop the -C "${distance}" from the 2nd grep.

layout fixed
Source Link
Cyrus
  • 12.8k
  • 3
  • 32
  • 55

A standard approach could be:

what_we_want='123456789'
context='firstname'
distance=10
grep -E -e "${context}" -C "${distance}" file_to_look_into | grep -E -e "${what_we_want}" -C "${distance}"
# the first ensure that we only look $distance lines around $context matching lines, 
# the 2nd then see if it find "$what_we_want" in those 2*$distance+1 lines.
# if you just want the matching line as a result, drop the -C "${distance}" from the 2nd grep.

The first ensure that we only look $distance lines around $context matching lines, the 2nd then see if it find "$what_we_want" in those 2*$distance+1 lines. If you just want the matching line as a result, drop the -C "${distance}" from the 2nd grep.

A standard approach could be:

what_we_want='123456789'
context='firstname'
distance=10
grep -E -e "${context}" -C "${distance}" file_to_look_into | grep -E -e "${what_we_want}" -C "${distance}"
# the first ensure that we only look $distance lines around $context matching lines, 
# the 2nd then see if it find "$what_we_want" in those 2*$distance+1 lines.
# if you just want the matching line as a result, drop the -C "${distance}" from the 2nd grep.

A standard approach could be:

what_we_want='123456789'
context='firstname'
distance=10
grep -E -e "${context}" -C "${distance}" file_to_look_into | grep -E -e "${what_we_want}" -C "${distance}"

The first ensure that we only look $distance lines around $context matching lines, the 2nd then see if it find "$what_we_want" in those 2*$distance+1 lines. If you just want the matching line as a result, drop the -C "${distance}" from the 2nd grep.

added 6 characters in body
Source Link
Olivier Dulac
  • 6.6k
  • 1
  • 25
  • 42

A standard approach could be:

what_we_want='123456789'
context='firstname'
distance=10
grep -E -e "${context}" -C "${distance}" file_to_look_into | grep -E -e "${what_we_want}" -C "${distance}"
# the first ensure that we only look $distance lines around $context matching lines, 
# the 2nd then see if it find "$what_we_want" in those 2*$distance+1 lines.
# if you just want the matching line as a result, drop the -C "${distance}" from the 2nd grep.

A standard approach could be:

what_we_want='123456789'
context='firstname'
distance=10
grep -E "${context}" -C "${distance}" file_to_look_into | grep -E "${what_we_want}" -C "${distance}"
# the first ensure that we only look $distance lines around $context matching lines, 
# the 2nd then see if it find "$what_we_want" in those 2*$distance+1 lines.
# if you just want the matching line as a result, drop the -C "${distance}" from the 2nd grep.

A standard approach could be:

what_we_want='123456789'
context='firstname'
distance=10
grep -E -e "${context}" -C "${distance}" file_to_look_into | grep -E -e "${what_we_want}" -C "${distance}"
# the first ensure that we only look $distance lines around $context matching lines, 
# the 2nd then see if it find "$what_we_want" in those 2*$distance+1 lines.
# if you just want the matching line as a result, drop the -C "${distance}" from the 2nd grep.
Source Link
Olivier Dulac
  • 6.6k
  • 1
  • 25
  • 42
Loading