First I'm not a star with shell-scripting, more used to programming in Python, but have to work with an existing Python script which calls Unix commands via subprocess.
In this script we use 2 find commands to check if 2 certain strings can be found in an xml file / file-name:
FIND_IN_FILE_CMD: find <directory> -name *.xml -exec grep -Hnl STRING1|STRING2 {}
FIND_IN_FILENAME_CMD: find <directory> ( -name *STRING1*xml -o -name *STRING2*xml )
The problem we saw is that STRING1 and STRING2 are not always written capitalized.
Now I can do something like STRING1|STRING2|String1|String2|string1|string2
and ( -name *STRING1*xml -o -name *STRING2*xml -o -name *String1*xml -o -name *String2*xml -o -name *string1*xml -o -name *string2*xml )
, but I was wondering if there was something more efficient to do this check in one go which basically matches all different writing styles.
Can anybody help me with that?
CodePudding user response:
You can make use of the -i
option with the grep
command to perform case-insensitive searches:
FIND_IN_FILE_CMD: find <directory> -name *.xml -exec grep -Hnil -i STRING1|STRING2 {}
FIND_IN_FILENAME_CMD: find <directory> ( -name *STRING1*xml -o -name *STRING2*xml -o -name *[Ss][Tt][Rr][Ii][Nn][Gg]1*xml -o -name *[Ss][Tt][Rr][Ii][Nn][Gg]2*xml -o -name *[sS][tT][rR][iI][nN][gG]1*xml -o *[sS][tT][rR][iI][nN][gG]2*xml )
This way, the grep
command will search for either STRING1
or STRING2
in a case-insensitive manner in the *.xml
files.
In the second command, you can make use of the -o
option to perform an "or" operation between multiple file name patterns, so that you don't need to repeat the whole command multiple times. Here, I have used square brackets with different cases of characters to match any of the variations of STRING1 and STRING2 in the file names.
CodePudding user response:
If you are going to continue using find
, just replace -name
with the case insensitive version -iname
.