Reputation: 1403
Below is my input file,
input.txt
17.0
17.12
17.123
17.1234
17.120
17.1230
17.12340
17.00
17.000
17.0000
And i want the out put as below
output.txt
17.120
17.1230
17.12340
17.000
17.0000
i.e. i need the values with more than 2 decimal points with trailing zero. i Have used the below awk
but the output file contains 17.00 also. How to ignore this.
awk '/^[0-9]+[.][0-9][1-9]*0/' input.txt
Output of awk
17.120
17.1230
17.12340
17.00
17.000
17.0000
Upvotes: 0
Views: 869
Reputation: 45556
I'm not exactly sure why you want to do this using awk
, grep
suffices. Also, if you know that your input only contains valid numbers matching anything in front of the dot is quite unnecessary.
$ grep '[.]...*0$' input.txt
17.120
17.1230
17.12340
17.000
17.0000
$ grep -P '[.][0-9]{2,}0$' input.txt
17.120
17.1230
17.12340
17.000
17.0000
To fix your pattern just add one more [0-9]
class; matching "at least two" means matching exactly two ([0-9][0-9]
) plus zero or more [0-9]*
):
$ awk '/[.][0-9][0-9][0-9]*0$/' input.txt
17.120
17.1230
17.12340
17.000
17.0000
or use +
:
$ awk '/[.][0-9][0-9]+0$/' input.txt
17.120
17.1230
17.12340
17.000
17.0000
Upvotes: 3
Reputation: 289535
Count the number of fields after the .
:
$ awk -F. '/0$/ && (split($2,a,"")>2)' file
17.120
17.1230
17.12340
17.000
17.0000
0
.split()
. If there are more than 2, the check is True.Upvotes: 1
Reputation: 174696
Through sed,
$ sed -n '/^[0-9][0-9]\.[0-9][0-9]\+0$/p' file
17.120
17.1230
17.12340
17.000
17.0000
Through awk,
$ awk '/^[0-9][0-9]\.[0-9][0-9]+0$/' file
17.120
17.1230
17.12340
17.000
17.0000
Upvotes: 1