wannabe programmer
wannabe programmer

Reputation: 683

check for a specific delimiter per line - bash

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

Answers (2)

anubhava
anubhava

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

iruvar
iruvar

Reputation: 23374

If you use a regular expression to set the field separator in to a single space (and nothing else), you can use the below to report problematic lines

awk -F'[ ]' 'NF != 5' file.txt

Upvotes: 1

Related Questions