Lazyworm
Lazyworm

Reputation: 113

How to use sed command to read to return error if one of the line has unmatch pattern?

I tried to look for any un-match on each line using sed comment. If any line not matching the pattern, then return 1, if all lines success. It looks for the pattern of -. I have the sed command as follow:

sed -n -E '/([a-zA-Z ]+-[0-9]+)/ p'

success case:

u-3 abaklsd
a-2 jkds

fail case:

u-3 abaklsd
a-2 jkds
khs jkd

Upvotes: 0

Views: 42

Answers (1)

tripleee
tripleee

Reputation: 189936

sed does not have a facility for this. You can probably refactor your script to Perl with a small effort.

perl -ne 'if (/[a-zA-Z ]+-\d+/) { print } else { $rc=1; }
    exit $rc if (eof)'

Or Awk:

awk '{ if (/[a-zA-Z +-[0-9]+/) print; else rc=1 }
    END { exit rc }'

The parentheses are superfluous so I took them out. Perhaps you want leading ^ and trailing $ anchors on the regex, though.

Upvotes: 1

Related Questions