1

I'm trying to write a bash command that will sort all volumes by the amount of data they have used and tried using

df | awk '{print $1 | "sort -r -k3 -n"}' 

Output:

map devfs Filesystem /dev/disk1s5 /dev/disk1s2 /dev/disk1s1 

But this also shows the header called Filesystem. How do I remove that?

0

6 Answers 6

6

I normally use one of these options, if I have no reason to use awk:

df | sed 1d 

The 1d option to sed says delete the first line, then print everything else.

df | tail -n+2 

the -n+2 option to tail say start looking at line 2 and print everything until End-of-Input.

I suspect sed is faster than awk or tail, but I can't prove it.
EDIT If you want to use awk, this will print every line except the first:

df | awk '{if (FNR>1) print}' 

FNR is the File Record Number. It is the line number of the input. If it is greater than 1, print the input line.

Sign up to request clarification or add additional context in comments.

Comments

6

Use tail -n +2 to remove the first line in df (or any other command) output:

df | tail -n +2 | sort -r -k3 -n 

Explanation: tail -n +N outputs starting with line N.

In a more general case, in order to remove the first lines of any output, you can use the tail -n +N to start with line N:

command | tail -n +2 | other_command 

Comments

3

Skip the first line, like this:

df | awk 'NR>1 {print $1 | "sort -r -k3 -n"}' 

Comments

2

Count the lines from the output of df with wc and then substract one line to output a headerless df with tail ...

LINES=$(df|wc -l) LINES=$((${LINES}-1)) df | tail -n ${LINES} 

OK - I see oneliner - Here is mine ...

DF_HEADERLESS=$(LINES=$(df|wc -l); LINES=$((${LINES}-1));df | tail -n ${LINES}) 

And for formated output lets printf loop over it...

printf "%s\t%s\t%s\t%s\t%s\t%s\n" ${DF_HEADERLESS} | awk '{print $1 | "sort -r -k3 -n"}' 

Comments

1

This might help with GNU df and GNU sort:

df -P | awk 'NR>1{$1=$1; print}' | sort -r -k3 -n | awk '{print $1}' 

With GNU df and GNU awk:

df -P | awk 'NR>1{array[$3]=$1} END{PROCINFO["sorted_in"]="@ind_num_desc"; for(i in array){print array[i]}}' 

Documentation: 8.1.6 Using Predefined Array Scanning Orders with gawk

Comments

1

Removing something from a command output can be done very simply, using grep -v, so in your case:

df | grep -v "Filesystem" | ... 

(You can do your awk at the ...)

When you're not sure about caps, small caps, you might add -i:

df | grep -i -v "FiLeSyStEm" | ... 

(The switching caps/small caps are meant as a clarification joke :-) )

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.