grep
will work fine for your purposes. You are just having a challenge with the syntax. Primarily, it is easier to use the pattern \s*
to match zero or more spaces between fields. You are using .*
which (since regular expressions are greedy) will match every character to the end of the line. Also, character classes mean characters contained within. I.e. to match 1, 2, or 3, use [123]
. With those changes, the following accomplishes what your intent appears to be:
echo "$ExifListAll" | grep -E "2014-07-21\s*12:54:3[34]\s*On\s*[123]"
output:
$ cat grepdat.dat | grep -E "2014-07-21\s*12:54:3[34]\s*On\s*[123]"
DSCF3567.JPG 2014-07-21 12:54:33 On 2
DSCF3567.RAF 2014-07-21 12:54:33 On 2
DSCF3568.JPG 2014-07-21 12:54:33 On 3
DSCF3568.RAF 2014-07-21 12:54:33 On 3
Is this not the output you were expecting? 12:54:34 had Off
& a 0
which I interpreted from your question as not wanted. If you want the states On/Off regardless, and included the
0` corresponding to 12:54:34 Off 0, then use:
echo "$ExifListAll" | grep -E "2014-07-21\s*12:54:3[34]\s*(On|Off)\s*[0123]"
output:
$ cat grepdat.dat | grep -E "2014-07-21\s*12:54:3[34]\s*(On|Off)\s*[0123]"
DSCF3567.JPG 2014-07-21 12:54:33 On 2
DSCF3567.RAF 2014-07-21 12:54:33 On 2
DSCF3568.JPG 2014-07-21 12:54:33 On 3
DSCF3568.RAF 2014-07-21 12:54:33 On 3
DSCF3569.JPG 2014-07-21 12:54:34 Off 0
per comment that lines 1-6 are desired:
cat grepdat.dat | grep -E "2014-07-21\s*12:54:3[234]\s*On\s*[123]"
output
$ cat grepdat.dat | grep -E "2014-07-21\s*12:54:3[234]\s*On\s*[123]"
DSCF3566.JPG 2014-07-21 12:54:32 On 1
DSCF3566.RAF 2014-07-21 12:54:32 On 1
DSCF3567.JPG 2014-07-21 12:54:33 On 2
DSCF3567.RAF 2014-07-21 12:54:33 On 2
DSCF3568.JPG 2014-07-21 12:54:33 On 3
DSCF3568.RAF 2014-07-21 12:54:33 On 3