1

As I go through multiples files, I want grep to print the filename of the first match.

For example:

grep -nHC 2 "show reload" wgltva-811sono1-dc*

Right now, I am searching for a string in multiple files, but I get the filename everytime.

wgltva-811sono1-dc0-sw.show.tech.txt-1249-abc
wgltva-811sono1-dc0:------------- show reload 
wgltva-811sono1-dc0-sw.show.tech.txt-1251-abc
--
--
wgltva-811sono1-dc0-sw.show.tech.txt-1333-abc
wgltva-811sono1-dc0-sw.show.tech.txt:1334:------------- show reload cause history -------------
wgltva-811sono1-dc0-sw.show.tech.txt-1335-abc
--
--
wgltva-811sono1-dc1-sw.show.tech.txt-1249-abc
wgltva-811sono1-dc1-sw.show.tech.txt:1250:------------- show reload cause full -------------
wgltva-811sono1-dc1-sw.show.tech.txt-1251-abc
--
--
wgltva-811sono1-dc1-sw.show.tech.txt-1331-abc
wgltva-811sono1-dc1-sw.show.tech.txt:1332:------------- show reload cause history -------------
wgltva-811sono1-dc1-sw.show.tech.txt-1333-abc

What I would like is the following: Display the filename once and display one line before and one line after the match.

**wgltva-811sono1-dc0-sw.show.tech.txt-1249-**
abc
------------- show reload cause full -------------
abc
--
--
**wgltva-811sono1-dc0-sw.show.tech.txt-1333-**
abc
------------- show reload cause history -------------
abc
--
--
**wgltva-811sono1-dc1-sw.show.tech.txt-1249-**
abc
------------- show reload cause full -------------
abc
--
--
**wgltva-811sono1-dc1-sw.show.tech.txt-1331-**
abc
------------- show reload cause history -------------
abc

I would like to know some suggestion on how to do this. I would prefer not to do any linux scripting, as I am a beginner. However, if that is the only solution, I am all for it.

Angel
  • 11

0 Answers0