简体   繁体   中英

Getting even number using grep in bash [on hold]

I have a range of numbers example 1 to 10 or 1 to 100. I used awk command and perl command to implement that. I want to use grep to get all even numbers and show it into terminal all even numbers.

I have a file named File.txt and contains the range of numbers: 1 2 3 4 5 6 7 8 9 0.

Using this code awk '. ($0%2)' File.txt awk '. ($0%2)' File.txt the output is 2 4 6 8 0.

Also with this code perl -ne 'print if($.%2 == 0)' File.txt the output is 2 4 6 8 0.

How can I use grep with the same output as perl and awk?

Given that even numbers are sequence of digits NOT ending with 02468 (and odd numbers ends with 13579), you can use:

   # Even
grep '^[0-9]*[02468]$'
   #  Odd
grep '^[0-9]*[13579]$'

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