Skip to main content
Post Undeleted by AdminBee
Post Deleted by AdminBee
Add explanation
Source Link
AdminBee
  • 23.6k
  • 25
  • 55
  • 77

You could use awk for that purpose:

awk 'index($0,"vm-1178")>0 {in_pat=1; print; next} \
in_pat == 1 && $0 ~ /^-->/ {print; next} \
{in_pat=0}' logfile.txt

This will look for lines containingIt contains three rules:

  • The first rule will look for lines containing the pattern and print these, as well as setting an internal flag in_pat to 1.
  • The second rule states that all subsequent lines starting with --> will be printed as well.
  • The third rule is used to reset that flag upon the very first line not containing the pattern or not starting with a -->, so that nothing is printed until the pattern is found again.

Note that in the pattern and print these, as well as setting an internal flag in_pattern to 1. All subsequent lines starting with --> will be printed as well. The very first line not containingrule, the pattern or not starting with a -->index will reset the flag, so that nothingfunction is printed until the patternused rather than a RegExp match. This is found againso that you can also look for patterns containing characters that have a special meaning in regular expressions.

You could use awk for that purpose:

awk 'index($0,"vm-1178")>0 {in_pat=1; print; next} \
in_pat == 1 && $0 ~ /^-->/ {print; next} \
{in_pat=0}' logfile.txt

This will look for lines containing the pattern and print these, as well as setting an internal flag in_pattern to 1. All subsequent lines starting with --> will be printed as well. The very first line not containing the pattern or not starting with a --> will reset the flag, so that nothing is printed until the pattern is found again.

You could use awk for that purpose:

awk 'index($0,"vm-1178")>0 {in_pat=1; print; next} \
in_pat == 1 && $0 ~ /^-->/ {print; next} \
{in_pat=0}' logfile.txt

It contains three rules:

  • The first rule will look for lines containing the pattern and print these, as well as setting an internal flag in_pat to 1.
  • The second rule states that all subsequent lines starting with --> will be printed as well.
  • The third rule is used to reset that flag upon the very first line not containing the pattern or not starting with a -->, so that nothing is printed until the pattern is found again.

Note that in the first rule, the index function is used rather than a RegExp match. This is so that you can also look for patterns containing characters that have a special meaning in regular expressions.

Source Link
AdminBee
  • 23.6k
  • 25
  • 55
  • 77

You could use awk for that purpose:

awk 'index($0,"vm-1178")>0 {in_pat=1; print; next} \
in_pat == 1 && $0 ~ /^-->/ {print; next} \
{in_pat=0}' logfile.txt

This will look for lines containing the pattern and print these, as well as setting an internal flag in_pattern to 1. All subsequent lines starting with --> will be printed as well. The very first line not containing the pattern or not starting with a --> will reset the flag, so that nothing is printed until the pattern is found again.