gafm
gafm

Reputation: 351

Print the filename only when found

so I have a command that prints the host if found.

awk -v IGNORECASE=1 -v m="$host" '$2~m {p=1} NF>2 && $2!~m {p=0} p' ${TEMP}/*

and this is the result result of it

33 VMME87      VMware        51402EC000CF2732 3:0:2
                             51402EC000CF2730 3:0:1
                             51402EC000CF2730 2:0:1
                             51402EC000CF2732 2:0:2
83 VMME87          VMware        51402EC000CF2732 1:2:4
                                 51402EC000CF2730 1:2:3
                                 51402EC000CF2730 0:2:3
                                 51402EC000CF2732 0:2:4

However, is it possible to also print the name of the file to where it was found?

Probably something like this:

**Name of the file**
33 VMME87      VMware        51402EC000CF2732 3:0:2
                             51402EC000CF2730 3:0:1
                             51402EC000CF2730 2:0:1
                             51402EC000CF2732 2:0:2
echo (space)

**Name of the file**    
83 VMME87          VMware        51402EC000CF2732 1:2:4
                                 51402EC000CF2730 1:2:3
                                 51402EC000CF2730 0:2:3
                                 51402EC000CF2732 0:2:4

If i use a grep, it prints the name of the file but does not print all the last column.

Name of the file 33 VMME87      VMware        51402EC000CF2732 3:0:2

Upvotes: 1

Views: 47

Answers (1)

hek2mgl
hek2mgl

Reputation: 158100

The current filename is available in the FILENAME variable. Change this part

{p=1}

to

{p=1; print FILENAME}

If you want only the filename without the path, use:

{p=1;sub(/^.*\//,"",FILENAME);print FILENAME}

Upvotes: 2

Related Questions