Reputation: 683
I want to write a script in bash that handles a file with lines that has 5 columns and i expect the delimiters to be one single space (" ").
everything other than that should lead to an error, even more than 1 space.
for instance, that line is legal: first last 1234124 complex no
while this
first last 1234124 complex no
or
first_last 1234124 complex no
should lead to an error.
I've tried some commands but everything i tried handled one space and more than one - the same way.
Waiting to be shared by your knowledge. Thanks.
Upvotes: 3
Views: 337
Reputation: 785541
You can use grep -qP
with this regex:
grep -qP '^(\S+\s){4}\S+$'
Testing:
> s='first last 1234124 complex no'
> grep -qP '^(\S+\s){4}\S+$' <<< "$s" && echo "valid" || echo "invalid"
valid
> s='first last 1234124 complex no'
> grep -qP '^(\S+\s){4}\S+$' <<< "$s" && echo "valid" || echo "invalid"
invalid
Upvotes: 1