0

I'm trying to search within all settings.ini files and determine which of them contain a specific multiline string.

My directory structures look like this

/
|-- folder
|---- settings.ini
|-- folder
|---- settings.ini
|-- folder
|---- settings.ini
|-- folder
|---- settings.ini
.
.
.

The string I am trying to find looks like this

[NeatFeature]
Enabled=1

The files themselves will contain either Enabled=1 or Enabled=0

I'm able to get a single line search working like this

find . -maxdepth 2 -name 'settings.ini' -exec grep '\[TransactionalMessageSearch\]' {} +

The problem is I can't figure out how to also include the Enabled=1 portion. I can't just search for that on it's own as that patter occurs multiple times throughout the files under different headings.

Any ideas?

dscl
  • 1,616
  • 7
  • 28
  • 48

4 Answers4

1

Try adding -A to add some lines after the match maybe:

grep -A 2 xyz file

It depends how many lines there may be between the search string and the additional information.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
1

Try this:

find . -name 'settings.ini' | xagrs grep 'Enabled=1' 
COLINHY
  • 395
  • 1
  • 6
  • This wont work because there may be Enabled=1 elsewhere in the file, but not under the header important to me. – dscl Jul 26 '14 at 16:08
1

Perl Method

perl -0777 -ne 's/.*\[TransactionalMessageSearch]\nEnabled=(\d)//s;print $1' settings.ini

Original Answer

You can also do this with gawk and test more than one file per invocation, using find's + feature:

Save this as a file called FeatureTest

#!/bin/bash
awk -F'=' '/\[TransactionalMessageSearch\]/{armed=1} armed==1 && /Enabled/{print FILENAME":"$2;armed=0;nextfile}' "$@"

then do this to make it executable

chmod +x FeatureTest

then you can do:

find . -name 'settings.ini' -exec ./FeatureTest {} +

The nextfile function is part of GNU awk, and I don't know of a way to do that without, so you would have to settle for the slower version as follows if you do not have GNU awk.

#!/bin/bash
awk -F'=' '/\[TransactionalMessageSearch\]/{armed=1} armed==1 && /Enabled/{print FILENAME":"$2;exit}' "$1"

and use it with:

find . -name 'settings.ini' -exec ./FeatureTest {} \;

The awk script basically looks for the string [Transact...] and when it finds it, it arms itself ready for the next occurrence of the word Enabled. When it finds that, after being armed, it prints the second field and the filename and moves to the next file (or exits). The fields are separated by an = sign because of the -F at the start.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • So this works, but not in my environment as I don't have the ability to create the file on this server so need to be able to run it all as one command. – dscl Jul 26 '14 at 16:42
1

Thanks to a suggestion by Mark, I was able to then pipe the output and get grep through that to get to the result set I was looking for

find . -maxdepth 2 -name 'settings.ini' -exec grep -HA 1 '\[TransactionalMessageSearch\]' {} \; | grep 'Enabled=1'
dscl
  • 1,616
  • 7
  • 28
  • 48