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


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



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

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

Can someone help me?

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

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


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}'
  • I have no idea what you mean about +$5. Do you mean the awk part?. Can you provide an example. Feel free to edit the answer. – Valentin Bajrami Sep 26 '13 at 21:01
  • @asonwryan This will trim the space between test/ and the percentage. So I don't think that's what the questioner want. – Valentin Bajrami Sep 26 '13 at 21:13
  • 1
    My bad: forgot to add space. Fixed. – jasonwryan Sep 26 '13 at 21:23
  • Yeah, that's a nice one! Eliminates the need for sed. – Valentin Bajrami Sep 26 '13 at 21:26
df | awk '/test/ {print $1 " " $5}'

will print

/test 10%
/test/drv0 20%
/test/drv1 15%
  • Many ways to remove the percent sign, including | tr -d '%' – glenn jackman Sep 26 '13 at 20:36
  • 1
    That assumes mount point paths don't contain blank characters. – Stéphane Chazelas Sep 26 '13 at 20:36
  • @StephaneChazelas yes, it does. But I'd argue that that's pretty uncommon and not in the scope of such a simple shell solution. – Martin von Wittich Sep 26 '13 at 20:47
  • @glennjackman I know, but I intentionally left that part as an exercise for the reader :) – Martin von Wittich Sep 26 '13 at 21:59

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)}'

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:

   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
  • 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 6 '13 at 15:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.