LinuxQuestions.org
Help answer threads with 0 replies.
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: 477

Rep: Reputation: 214Reputation: 214Reputation: 214
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: 15,644

Rep: Reputation: 2091Reputation: 2091Reputation: 2091Reputation: 2091Reputation: 2091Reputation: 2091Reputation: 2091Reputation: 2091Reputation: 2091Reputation: 2091Reputation: 2091
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: 477

Original Poster
Rep: Reputation: 214Reputation: 214Reputation: 214
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: 15,644

Rep: Reputation: 2091Reputation: 2091Reputation: 2091Reputation: 2091Reputation: 2091Reputation: 2091Reputation: 2091Reputation: 2091Reputation: 2091Reputation: 2091Reputation: 2091
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: 477

Original Poster
Rep: Reputation: 214Reputation: 214Reputation: 214
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
Senior Member
 
Registered: Nov 2013
Location: Tokyo
Distribution: Redhat/Centos, Ubuntu, Raspbian, Fedora
Posts: 1,691

Rep: Reputation: 353Reputation: 353Reputation: 353Reputation: 353
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: 15,644

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

Rep: Reputation: 201Reputation: 201Reputation: 201
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


All times are GMT -5. The time now is 10:23 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration