Vijay
Vijay

Reputation: 123

Linux command to Print filename and lines containing string

I have large number of .cfg files in a specific directory.

The files content looks like /usr/prod/File1.cfg:

#Below are the configuration details
$DB_Connection=ABC_DATABASE
$Table_Name=XYZ
$DESCRIPTION=Used to connect to organizations critical business data

/usr/prod/File2.cfg:

#Below are the configuration details
$DB_Connection=SAMPLE_DATABASE
$Table_Name=PQR
$DESCRIPTION=Used to connect to Customers critical business data

/usr/prod/File3.cfg:

/usr/prod/File4.cfg:

Note: there are some empty files and some files doesn't have specific string pattern at all

I would like to get all the database Connections i.e. lines containing "$DB_Connection="

Expected output: Filename:lineNumber:Entire line containing string

/usr/prod/File1.cfg:2:$DB_Connection=ABC_DATABASE
/usr/prod/File2.cfg:2:$DB_Connection=SAMPLE_DATABASE
....

I tried this command from other blogs but it doesn't work or gives empty dataset

find /usr/prod/*.cfg -type f -print0 | xargs -0 grep -Hn -A0 "$DB_Connection=" > /home/userId/outputconnections.txt

Upvotes: 0

Views: 123

Answers (1)

tink
tink

Reputation: 15204

You're overthinking this:

grep -n '$DB_Connection' /usr/prod/*.cfg
File1.cfg:2:$DB_Connection=SAMPLE_DATABASE
File3.cfg:2:$DB_Connection=ABC_DATABASE

Upvotes: 1

Related Questions