Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.
 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?

share|improve this question

5 Answers 5

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

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

share|improve this answer
    
+1 for -P (posix) usage. –  Olivier Dulac Sep 27 '13 at 8:23

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}'
share|improve this answer
    
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. –  val0x00ff 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. –  val0x00ff 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. –  val0x00ff Sep 26 '13 at 21:26
df | awk '/test/ {print $1 " " $5}'

will print

/test 10%
/test/drv0 20%
/test/drv1 15%
share|improve this answer
    
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)}'
share|improve this answer

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
share|improve this answer
    
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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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