Hi,
I need to grep lines that contain three words from files in the same directory
And next to the greped line I need to have the file that the line came from.
Example:
The directory is:
/var/logs
The files in the directory are:
XSLog2012.05.14-15.40.48.txt
XSLog2012.05.14-15.49.01.txt
XSLog2012.05.14-15.57.11.txt
XSLog2012.05.14-16.05.07.txt
XSLog2012.05.14-16.11.41.txt
XSLog2012.05.14-16.18.26.txt
XSLog2012.05.14-16.25.16.txt
XSLog2012.05.14-16.32.00.txt
XSLog2012.05.14-16.38.43.txt
XSLog2012.05.14-16.45.41.txt
XSLog2012.05.14-16.52.50.txt
XSLog2012.05.14-17.00.03.txt
XSLog2012.05.14-17.06.38.txt
XSLog2012.05.14-17.14.00.txt
XSLog2012.05.14-17.20.41.txt
XSLog2012.05.14-17.27.35.txt
XSLog2012.05.14-17.34.57.txt
XSLog2012.05.14-17.42.23.txt
XSLog2012.05.14-17.50.01.txt
XSLog2012.05.14-17.57.01.txt
XSLog2012.05.14-18.04.16.txt
XSLog2012.05.14-18.12.12.txt
XSLog2012.05.14-18.19.54.txt
XSLog2012.05.14-18.26.49.txt

The words that I need to be greped in one line are:

YYYY
2@0011234abc12
EEE

The output should look like this:

XSLog2012.05.13-21.54.41.txt:YYYY 792 bbln/2@0011234abc12.abc.abc.abc.aa XXXX 1.0 EEE 1.0
XSLog2012.05.13-22.14.28.txt:YYYY 797 bbln/2@0011234abc12.abc.abc.abc.aa XXXX 1.0 EEE 1.0
XSLog2012.05.13-22.39.20.txt:YYYY 802 bbln/2@0011234abc12.abc.abc.abc.aa XXXX 1.0 EEE 1.0
XSLog2012.05.13-23.08.40.txt:YYYY 807 bbln/2@0011234abc12.abc.abc.abc.aa XXXX 1.0 EEE 1.0
XSLog2012.05.13-23.29.07.txt:YYYY 812 bbln/2@0011234abc12.abc.abc.abc.aa XXXX 1.0 EEE 1.0
XSLog2012.05.13-23.54.00.txt:YYYY 817 bbln/2@0011234abc12.abc.abc.abc.aa XXXX 1.0 EEE 1.0
XSLog2012.05.14-00.29.26.txt:YYYY 822 bbln/2@0011234abc12.abc.abc.abc.aa XXXX 1.0 EEE 1.0
XSLog2012.05.14-01.22.47.txt:YYYY 827 bbln/2@0011234abc12.abc.abc.abc.aa XXXX 1.0 EEE 1.0

Thanks in advence.

Dani AI

Generated

A compact, reliable approach is to search each file for the three required substrings (literal or regex) and print the filename plus the matching line. The easiest portable method uses awk with fixed-substring checks so punctuation in tokens (dots, @, etc.) does not need escaping.

awk 'index($0,"word1") && index($0,"word2") && index($0,"word3") { print FILENAME ":" $0 }' /var/logs/*.txt

index() tests fixed substrings, so it matches the tokens anywhere on the same line in any order. FILENAME is the current filename, so output is filename:line. This is fast and avoids regex escaping issues.

A simple grep-based alternative chains fixed-string greps and preserves filenames:

grep -F -H 'word1' /var/logs/*.txt | grep -F 'word2' | grep -F 'word3'

Use -F for literal (no regex), -H to force filename prefixes. Add -i to grep commands for case-insensitive matching. For environments where GNU grep supports PCRE, a single-command positive-lookahead option is also possible, but it is less portable than the awk or -F pipeline.

Common pitfalls and checks: confirm all three tokens appear on the same line (not split across lines), ensure the shell glob actually matches the intended files, and verify file permissions. When tokens include regex metacharacters, prefer -F or awk index() to avoid accidental pattern interpretation. As noted, consulting the local grep/awk man pages helps with available flags; thread author later marked the issue solved after applying a matching approach like these.

Recommended Answers

All 2 Replies

Try reading the man page for grep/egrep: man grep

Solved,
Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.