简体   繁体   中英

awk matching pattern without spaces

I need to match patterns in the first line of a document. The field separator is ":" and I want to get wrong message when the pattern is not followed. I need to distinguish between fields with and without spaces. I use something like this:

awk -F ":" '{if (($1 ~/^@[A-Za-z0-9]*/) && ($2 ~/[0-9]*/) && ($3 ~/[0-9]*/) && ($4 ~/[0-9]*/) && ($5 ~/[0-9]+/)){print "ok"} else { print "no"}}'  

I get "OK" if the

$1 

is

"AAABBBF43T5Y" 

and even if is

"AABBBF 43T5Y"

I need to get "no" when there is a space in the $1 or in the other field.

Any suggestions?

OK, it's a little confusing.

I have something like this :

@HWUSI-EAS100R:6:73:941:1973#0/1

And I want to check each field. The first one can contain any character, the second only numbers,etc. But blank spaces are not allowed, so it is not correct something like:

@HWUSI-E AS100R:6:73:9 41:1973#0/1

Thanks for your feedback and sorry for being so confusing.

Your example and wording is very confusing but it seems you want to check whether the first field contains a space. You could do something like:

$ cat file
AABBBF43T5Y:f2:f3
AABBBF 43T5Y:f2:f3

$ awk -F: '$1~/ /{print "FAIL LINE: ",NR;next}{print "PASS LINE:",NR}' file
PASS LINE: 1
FAIL LINE: 2

This can be written simpler if you just want to check a condition a print the output, if you want to anything else the previous block structure allows for easier modification:

$ awk -F: '{print $1~/ /?"FAIL":"PASS","LINE:",NR}' file
PASS LINE: 1
FAIL LINE: 2

You will get much better answers if you describe your problem clearly, post a representative input file and the expected output.

Edit:

As long as regex matches are anchored with ^ and $ and only contain the allowed characters then no spaces will be accpeted:

$ cat file
@HWUSI-EAS100R:6:73:941:1973#0/1
@HWUSI-E AS100R:6:73:9 41:1973#0/1

$ awk -F: '$1~/^@[a-zA-Z0-9-]*$/&&$2~/^[0-9]*$/{print "PASS",NR;next}{print "FAIL",NR}' file
PASS 1
FAIL 2

So $2~/^[0-9]*$/ is evaluated true only it the regex matches and the regex is:

^      # Match start of field
[0-9]* # Zero or more digits
$      # The end of the field

This will allow a blank field or a digit only field.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM