How to strip output of grep?

1.5k views Asked by At

I'm writing a script which utilises the salt-cli (SaltStack) as well as generic command line arguments to produce a simple HTML-based table outlining all of our servers' Hardware specs and software versions.

It's as simple as it sounds, however my only challenge is stripping my commands outputs so that they can be presented nicely in a table (e.g. one word/number rather than the whole output).

My latest challenge that I'm yet to strip effectively is the output of a simple 'df -Ph. So far I have got it down to this:

'df -Ph /'

My output:

 Filesystem                        Size  Used Avail Use% Mounted on
/dev/mapper/vhfhuffu               50G  8.0G   39G  18% /

I would like it to only show the available for each server, I can't find a reliable way to do this for ever Unix server.

4

There are 4 answers

0
jto On BEST ANSWER

I ended up going with this due to my need to run this command from a script via Salt which requires '' around the cmd.run

df -Ph / | grep / | awk '"'"'{ print $4}'"'"''

Ugly but it's the only thing that works in such a niche case

1
Serge Ballesta On

tail and cut should be enough here:

 df -Ph / | tail +2 | cut -f4 -w

should give you the file system size of the root file system.

8
David C. Rankin On

Another simple way is to combine tail -n 1 to get the last line and pass it through grep '{print $4}'. This will extract the39G` value that it sounds like you want, e.g.

$ df -Ph / | tail -n 1 | awk '{print $4}'
39G

If it is some other number you are trying to sum, just let me know.

0
RomanPerekhrest On

You are able to extract the needed column value using --output option:

df -h --output=avail | tail -n +2