$ cat someStrings.txt
LOGICAL1
HUNGARY2
PENGUIN2
MOBILE
GUITAR1
MOUSE1
$
$ cat checkIF.txt
PENGUIN
MOBILE
$
I need to search for strings in someStrings.txt file that matches the patterns in checkIF.txt file.
The strings in someStrings.txt sometimes end with a number . But, during pattern match, this number should be ignored.
If there is at least 1 match, just print PENGUIN exists in someStrings.txt file .
Logic will look like
if
grep -c PENGUIN someStrings.txt > 0
then print 'PENGUIN exists in someStrings.txt file'
then proceed with pattern search in the file
fi
So, with the sample data given above, my expected output will be
PENGUIN exists in someStrings.txt file ( PENGUIN2 exists in someStrings.txt file is fine too ) MOBILE exists in someStrings.txt file
One last question : How can I grep only those lines that end with a number ?
ie. in the below file, all lines except the string MOBILE should be returned.
$ cat someStrings.txt
LOGICAL1
HUNGARY2
PENGUIN2
MOBILE
GUITAR1
MOUSE1
Note: Even though this will work with the sample at hand, since it is only the last digits that need to be ignored, an anchor ( $ )should be used. Also, to ignore any kind of spaces and make it more robust, $1 should be used rather than $0, so instead of
I would have thought (with an example including HUNGARY1 ), that there could also be entries like NEW ZEALAND7 . In which case, $0 would be better than $1 .
The samples provided all had single decimal digit numbers, but looking back at the original problem statement: "The strings in someStrings.txt sometimes end with a number ." (not end with digit), you may be correct in interpreting it as non-negative decimal number). Of course, the intent could even be that any string that would be treated as a number when scanned by strtod() should be considered a number. Without a clearer definition of terms by kraljic, we're just guessing at what regular expression is needed.