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?

5 Answers 5

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

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

1
  • +1 for -P (posix) usage. Sep 27, 2013 at 8:23
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}'
4
  • 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. Sep 26, 2013 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. Sep 26, 2013 at 21:13
  • 1
    My bad: forgot to add space. Fixed.
    – jasonwryan
    Sep 26, 2013 at 21:23
  • Yeah, that's a nice one! Eliminates the need for sed. Sep 26, 2013 at 21:26
6
df | awk '/test/ {print $1 " " $5}'

will print

/test 10%
/test/drv0 20%
/test/drv1 15%
4
  • Many ways to remove the percent sign, including | tr -d '%' Sep 26, 2013 at 20:36
  • 1
    That assumes mount point paths don't contain blank characters. Sep 26, 2013 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. Sep 26, 2013 at 20:47
  • @glennjackman I know, but I intentionally left that part as an exercise for the reader :) Sep 26, 2013 at 21:59
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)}'
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
1
  • 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, 2013 at 15:55

You must log in to answer this question.

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