-2

I use grep (egrep) to search text a log. I have multiple txt files to search in the same folder. By default, grep output file name (full path), with math result.

The directory path is long so it looks ugly.

However, if I use -h option, it only give me match result but not file name, which is not good either, because sometimes I need to know which file it is from.

for example, in folder bashsearch I have file1, file2, file3...

The default output is like this:

/mnt/c/Users/chili/Desktop/.../bashsearch/file1:match result 1

/mnt/c/Users/chili/Desktop/.../bashsearch/file2:match result 2

this looks ugly.

If I use -h, the output is like this:

match result 1

match result 2

this is not good either.

What I want is like this:

file1:match result 1

file2:match result 2

How to achieve this please? Any help is highly appreciated.

HatLess
  • 10,622
  • 5
  • 14
  • 32
Lenny
  • 29
  • 1
  • 9
  • 2
    use the `-l` flag. check this : https://stackoverflow.com/questions/6637882/how-can-i-use-grep-to-show-just-filenames-on-linux – debugger Sep 18 '22 at 06:17
  • `grep` simply restates the file name you passed on the command line. If you don't want absolute paths in the output, don't pass absolute file names as input. See also [Difference between `./` and `~/`](https://stackoverflow.com/a/55342466/874188) – tripleee Sep 19 '22 at 05:35

1 Answers1

0

You can reformat output by using pipe to another command. For example

| grep -o "[^/]*:.*"

will remove the path part.

Yoichi Nakayama
  • 704
  • 6
  • 9