6
 df -h | grep test | sed -e 's/*%.^ //g;s/.*[ ^I]//'

Output:

/test
/test/drv0
/test/drv1
 df -h  | grep test  | cut -d '%' -f1 | sed -e 's/*%.^ //g;s/.*[ ^I]//'

Output:

10
20
15

I want to know how can I join those outputs like this:

/test 10
/test/drv0 20
/test/drv1 15

Can someone help me?

jasonwryan
  • 71,734
  • 34
  • 193
  • 226
sharkguto
  • 63
  • 5

5 Answers5

10
df -P | sed -n '/test/s/.*[[:blank:]]\(.*\)%[[:blank:]]*\(.*\)/\2 \1/p'

(that assumes the mount point paths don't contain % or newline characters)

Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
8

I think something as follow. I don't understand the whole question though

df -h | awk '/test/{print $1" "$5}' | sed -e ':a;N;$!ba;s/\n/ /g' -e 's/%//g'

Since the question has changed, here is the updated answer

df -h | awk '/test/{print $1, +$5}'
jasonwryan
  • 71,734
  • 34
  • 193
  • 226
Valentin Bajrami
  • 9,244
  • 3
  • 25
  • 38
6
df | awk '/test/ {print $1 " " $5}'

will print

/test 10%
/test/drv0 20%
/test/drv1 15%
Martin von Wittich
  • 13,857
  • 6
  • 51
  • 74
4

This is not a good solution for your particular situation (it works, but is needlessly complex), you should use the answers already provided. I just wanted to mention another tool that is very useful when you want to join the output of multiple programs, paste:

DESCRIPTION
   Write  lines  consisting  of  the sequentially corresponding lines from
   each FILE, separated by TABs, to standard output.   With  no  FILE,  or
   when FILE is -, read standard input.

Combined with bash process substitution, you can combine the output of your two commands like this:

$ paste  <(df -h | grep test | sed -e 's/*%.^ //g;s/.*[ ^I]//') \
       <(df -h  | grep test  | cut -d '%' -f1 | sed -e 's/*%.^ //g;s/.*[ ^I]//')
 /test 10
 /test/drv0 20
 /test/drv1 15

Or, to use a simpler example:

$ paste <(echo -e "a\nb\nc") <(echo -e "1\n2\n3")
a   1
b   2
c   3
terdon
  • 234,489
  • 66
  • 447
  • 667
  • I have never seen (or thought of) having multiple input redirections in a case like this. No idea it would have even worked. Thank you for pointing it out. – kurtm Oct 06 '13 at 15:55
4

I would use int() function in awk that will remove % and important is -P flag to df because if partition in LVM then also it's print properly.

df -hP | awk '/test/{print $1,int($5)}'
Rahul Patil
  • 24,281
  • 25
  • 80
  • 96