1

I'm trying to get the fs that have high space consumed. I'm trying to do this using df command with -g flag (gigabyte view) and I can't obtain my expected value.

df -g | sed 's/%//g'| awk '+$5>=75 {print}' | head -10

With this command below I got an error:

df -g | sed 's/%//g'| awk '{print $5>=75}' | head -4

Error Message:

Syntax Error The source line is 1.
The error context is
               {print >>>  $5>= <<<
awk: 0602-502 The statement cannot be correctly parsed. The source line is 1.

Here is the output of df -g command on AIX:

/tmp>df -g | sed 's/%//g'| awk '$5 >= 75 { print }' | head -4
Filesystem    GB blocks      Free Used    Iused Iused Mounted on
/dev/hd4          10.50     10.05    5    16492     1 /
/dev/hd2          25.50     18.15   29    64361     2 /usr
/dev/hd9var        2.50      0.72   72     4521     3 /var

Complete view:

Filesystem    GB blocks      Free %Used    Iused %Iused Mounted on
/dev/hd4          10.50     10.05    5%    16492     1% /
/dev/hd2          25.50     18.15   29%    64357     2% /usr
/dev/hd9var        2.50      1.39   45%     4512     2% /var
/dev/hd3          21.50     20.24    6%     1495     1% /tmp

I tried on AIX 7.1 PowerPC_POWER8. I don't know what I'm doing wrong!

4
  • I have no idea if this is required on the AIX flavor of awk or now, but try adding spaces? Also, your two awk statements are different. $5 >= 75 {print} will print the input line if the fifth field is greater than or equal to 75; {print $5 >= 75} will print a zero or one based on the truthiness of the inequality.
    – DopeGhoti
    Aug 23, 2017 at 18:44
  • Yes, I know it. I want exactly the result of $5 >= 75 {print} but I cant see this result. I tried adding spaces and It does not work. Aug 23, 2017 at 18:50
  • You should probably add an example of the output of df -g to the text in the question. There's not many here with an AIX system at hand. Also, never say "it does not work" without also saying exactly in what way it doesn't work.
    – Kusalananda
    Aug 23, 2017 at 19:02
  • Oks, I will edit main post. Aug 23, 2017 at 19:03

3 Answers 3

3

Instead of using tr and awk twice, just use awk once:

df -g | awk '{ sub("%", "", $4); if ($4 >= 75) { print $4, $7 }}' | sort -n

This replaces the percent signs in field 4 with the empty string (stripping them off); if the remaining value is at least 75, print fields 4 and 7, then pipe them to sort.

An improvement, based on remembering one of Stéphane's answers:

df -g | awk '$4+0 >= 75 { print $4, $7 }' | sort -n

The "+0" addition forces a numerical comparison instead of a strictly string-based comparison.

If you want to keep the header, change the criteria to:

df -g | awk 'NR < 2 || $4+0 >= 75 { print $4, $7 }' | sort -n
1

Concentrating on the awk code:

awk '{print $5>=75}'

This should probably be

awk '$5 >= 75 { print }'

which will output the current input record if its fifth field is numerically greater than or equal to 75.

Judging from the output that you get, you probably want to check $4 rather than $5 though since I presume it's the fourth field that contains the percentage:

df -g | tr -d '%' | awk '$4 >= 75 { print }' | head -4

Looking at the error message: Using awk on OpenBSD (which seems to complain in exactly the same way as your AIX awk), the original code has a syntax error in that it's not possible to print $5 >= 75 (unless put inside parentheses in which case it will evaluate to 0 (false) or 1 (true)). mawk behaves the same.

GNU awk accepts the syntax in the original code, but it's obviously still not what you want.

7
  • BSD awk (as provided by McOS) is also perfectly happy with this syntax, but throwing it into a parenthetical is probably a good idea anyhow, to make it clear that you want to be doing what you're doing in five years when future-you is reading the awk script.
    – DopeGhoti
    Aug 23, 2017 at 18:56
  • 1
    @Kusulananda: I edited the main post and added the df -g command output. Aug 23, 2017 at 19:12
  • @vicdeveloper Updated. Since you didn't show the df -g output I'm only guessing that you actually want column 4 rather than 5. It's hard to tell with the % removed.
    – Kusalananda
    Aug 23, 2017 at 19:20
  • See the main post. I added the complete view. Aug 23, 2017 at 19:22
  • 1
    Hello, I find my expected result using this command: **df -g | tr -d '%' | awk '$4 >= 70 { print }' | awk '{print $4, $7}'| sort -n | uniq** Many thanks for all of your responses. Aug 23, 2017 at 19:38
0

This is the error ... 'Filesystem GB blocks Free Used Iused Iused Mounted on'

The solution is:

# df -g | grep Filesystem | sed 's/%//g'| awk '{print $5>=75}' | head -4

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .