Example of string

blah:blah:example - 1234
blah1:blah2:example2 - 3213

What I want to check exists

- 1234

but numeric can be different

I know I can use

awk -F: '$3 ~ / - /' 

to check if " - " exists, but how can I apply numeric possibilities?

2 Answers 2


So you basically want to check if after ' - ' there's some numbers?
If you want to do this you can write
awk -F: '$3 ~ / - [0-9]+/'

More advanced solution

If you have an array of possible values for your last number (es. 1, 2, 3, 4, 5) you could write something like this.

values=(1 2 3 4 5)
awk -v vals="$(echo ${values[@]})" \
'{if(match(vals,$3" ")){print $0}}' input_file.txt

This will print only those entries of input_file.txt having the last number in the values array you specified.


The following awk command will display all lines from file that has a - in its penultimate whitespace-delimited column, and a positive integer in its last whitespace-delimited column:

awk '$(NF-1) == "-" && $NF ~ /^[0-9]+$/' file

The following grep command would do the same thing:

grep -E -e '-[[:space:]]+[0-9]+$' file

The -E is needed to understand the + modifier in the pattern, and -e is needed to stop grep from interpreting the - in the pattern as a command line option (-e could also be changed replaced by --, which signals the end of any command line options).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.