I want to extract strings other than alphabetic-numeric-numeric configurations.

Asked 1 years ago, Updated 1 years ago, 70 views

when you have the following text:
A-7-2
D-1-5
D-1-5
A-1
D-1-5, A-2-5
G-5-21, B-3, A-2-5

I want to make sure that only alphabetic-numeric-numeric characters are extracted (A-1 and B-3 here).
What regular expressions should I use?Thank you for your cooperation.

regular-expression

2022-09-29 22:37

1 Answers

1) Comma (, ) delimited data is not considered

As Nekketsuuuu pointed out, simple string extraction like the following

cat text file|egrep-v'^[A-Z]-[0-9]-[0-9]$'

The fifth line of D-1-5, A-2-5 is also extracted.

2) If all items in the comma (, ) delimited line meet the conditions, you do not want to extract them.

cat text file|gawk'
{
    pattern="^[A-Z]-[0-9]-[0-9]$"
    split($0,a,",")
    hit = 0;
    for (i in a) {
        if(match(a[i], pattern)){
            hit = 1
        }
    }
    if(!hit){
        print$0
    }
}
'

The execution environment is as follows:
Ubuntu 18.04.3 LTS WSUnder WSL
gawk GNU Awk 4.1.4
egrep grep(GNU grep) 3.1
bash GNU bash, version 4.4.20(1)-release(x86_64-pc-linux-gnu)


2022-09-29 22:37

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.