LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-14-2015, 07:34 PM   #1
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 634

Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
Grep match percentage


I need to match a percentage from 0% to 100%

My current code will match from 10-99% but not 0-9% or 100%

Code:
grep [0-9][0-9]%
I'm trying to match a ping packet loss output.

Code:
ping www.domain.tld
PING domain.tld (64.xx.xxx.xx) 56(84) bytes of data.
64 bytes from domain.tld (64.xx.xxx.xx): icmp_req=1 ttl=52 time=45.6 ms 
64 bytes from domain.tld (64.xx.xxx.xx): icmp_req=2 ttl=52 time=47.5 ms 
64 bytes from domain.tld (64.xx.xxx.xx): icmp_req=4 ttl=52 time=45.0 ms 
64 bytes from domain.tld (64.xx.xxx.xx): icmp_req=6 ttl=52 time=46.0 ms 
^C64 bytes from domain.tld (64.xx.xxx.xx): icmp_req=7 ttl=52 time=47.2 ms 

--- domain.tld ping statistics ---
7 packets transmitted, 5 received, 28% packet loss, time 14375ms
rtt min/avg/max/mdev = 45.030/46.323/47.595/0.989 ms
example code in use:

Code:
ping -qc 10 domain.tld | grep -o '[0-9][0-9]%'
 
Old 12-14-2015, 07:54 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,119

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
You need extended regex - "grep -E". It allows you to define a varying range - {1,3}.

Hmm - just grep will do it too if you escape the curly brackets.

Last edited by syg00; 12-14-2015 at 07:56 PM. Reason: More testing
 
1 members found this post helpful.
Old 12-14-2015, 08:13 PM   #3
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 634

Original Poster
Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
After a little more testing this works

Code:
grep -E '[0-9]|[0-9][0-9]|100'\
Had to use extended but that's ok. Escaping brackets didn't work for me using normal grep?

Code:
grep -E '[0-9]|[0-9][0-9]|100'\%
Matches 0-9%

Code:
grep -E '[0-9]|[0-9][0-9]|100'\%
Matches 10-99%
Code:
grep -E '[0-9]|[0-9][0-9]|100'\%
Matches 100%

Edit:
The above didn't match my original output (ping output) and matches a lot of extra stuff. This worked better with ping

Code:
grep -oE '[0-9]\%|[0-9][0-9]\%|100\%'

Last edited by Sefyir; 12-14-2015 at 08:29 PM.
 
Old 12-14-2015, 08:41 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,119

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Code:
grep -Eo "[[:digit:]]{1,3}%"
grep -o "[0-9]\{1,3\}%"

Last edited by syg00; 12-14-2015 at 08:43 PM. Reason: Bloody %% issues with the board
 
Old 12-14-2015, 09:07 PM   #5
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 634

Original Poster
Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
Quote:
Originally Posted by syg00 View Post
Code:
grep -Eo "[[:digit:]]{1,3}%"
grep -o "[0-9]\{1,3\}%"
That works - but I don't understand the role of {1,3}?
 
Old 12-14-2015, 09:16 PM   #6
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by Sefyir View Post
That works - but I don't understand the role of {1,3}?
It's just the count of digits - between one and three.
Technically this is not quite correct by the way - it would match 321% for example. Practically you are unlikely to encounter it though.
 
Old 12-14-2015, 09:17 PM   #7
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,119

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Read the section on regex in the grep manpage.
 
Old 12-15-2015, 12:17 AM   #8
JJJCR
Senior Member
 
Registered: Apr 2010
Posts: 2,148

Rep: Reputation: 448Reputation: 448Reputation: 448Reputation: 448Reputation: 448
Arrow

Quote:
Originally Posted by Sefyir View Post
That works - but I don't understand the role of {1,3}?
Curly Brackets
Curly brackets { } are general repetition quantifiers. They specify a minimum and maximum number of permitted matches.

{match the string if at least n times, match the string if not more than n times}

For example: a{2,4} matches aa, aaa, or aaaa, but not a or aaaaa

{n} - exactly n times

from this link: https://sc1.checkpoint.com/documents...uide/12885.htm
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] echo 8888|grep 8+ does not match. stf92 Linux - Newbie 3 07-30-2015 03:54 PM
use grep to match multiple lines noony123 Linux - Newbie 2 02-03-2011 04:47 AM
grep/sed/awk - find match, then match on next line gctaylor1 Programming 3 07-11-2007 08:55 AM
grep output on stdout and grep output to file don't match xnomad Linux - General 3 01-13-2007 04:56 AM
how to grep only one string pr match gummimann Linux - General 3 11-06-2003 09:40 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:36 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration