grep1-3 Exercise 3: Checking a Basic-to-Extended Translation ============================================================= cat > lines.txt <<'EOF' cat concatenate The cat sat on the mat cot cut cat c.t color colour a+b=c aaa abab abab price: $19.99 call 555-0142 now 2026-10-08 ERROR disk full 2026-10-08 WARN low memory the the end [note] keep this path C:\temp\file hello world indented line file.txt and fileXtxt end. EOF check() { if diff <(grep -o "$1" lines.txt) <(grep -oE "$2" lines.txt) > /dev/null; then echo "same: basic '$1' extended '$2'" else echo "DIFFERENT: basic '$1' extended '$2'" fi } # four correct translations check 'colou\?r' 'colou?r' check 'ERROR\|WARN' 'ERROR|WARN' check '\(ab\)\{2\}' '(ab){2}' check '[0-9]\{3\}-[0-9]\{4\}' '[0-9]{3}-[0-9]{4}' # two that look right and are not check 'a+b' 'a+b' check 'file.txt' 'file\.txt' Output: same: basic 'colou\?r' extended 'colou?r' same: basic 'ERROR\|WARN' extended 'ERROR|WARN' same: basic '\(ab\)\{2\}' extended '(ab){2}' same: basic '[0-9]\{3\}-[0-9]\{4\}' extended '[0-9]{3}-[0-9]{4}' DIFFERENT: basic 'a+b' extended 'a+b' DIFFERENT: basic 'file.txt' extended 'file\.txt' WHY THIS WORKS AS AN ANSWER --------------------------- The function runs the basic pattern without -E and the extended one with it, using -o so that the comparison is of what matched, not just which lines. diff <(...) <(...) compares the two outputs as if they were files (bash, not plain sh); the exit status of diff drives the if. The first four are genuine translations: every backslash operator in the basic form loses its backslash. The fifth is the trap from the chapter: the same text a+b is a literal plus in basic and 'one or more a' in extended, so the outputs differ (the basic one finds 'a+b', the extended one finds the 'ab' in 'abab'). The sixth is a different slip: 'file.txt' written the same way in both would be the same, but translating it as 'file\.txt' changes it, because the unescaped dot also matched 'fileXtxt'. A translation that is correct for the engine but changes your intent is a difference worth knowing about.