Pankaj Singh Pankaj Singh - 2 years ago 88
Bash Question

Reading only two characters before a specific pattern in a linux file

I have a file like

Filesystem Size Used Avail Use% Mounted on
82G 7.7G 70G 10% /
hello 32G 922M 31G 3% /abc/asd
/abc/xyz 477M 118M 334M 27% /asd
/abc/xyz 50G 9.4G 38G 21% /ad
/abc/xyz 79G 27G 49G 36% /asd
/abc/xyz 30G 7.9G 21G 29% /sd
/abc/xyz 197G 2.4G 185G 2% /asd
500G 18G 483G 4% /asdas
1.9T 1.5T 405G 79% /media/Scratch

I want only the two characters before % i.e 10,3,27,21 and so on which i will compare one by one with a value 85 whether greater or less. But the first line use% should be skipped. Please suggest me how to get only those values in a variable one by one or in a file which i can compare with 85 using conditional statements.

i used grep -E -o ".{0,2}%" test.txt , but it is giving % with the values e.g 10% and also se% which i don't want.


Answer Source

You can use awk:

df -h | awk 'NR>1{print $5+0}'

Or with a file:

awk 'NR>1{print $5+0}' test.txt

Using gnu grep:

grep -oP '\d+(?=%)' test.txt
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download