0

I'm doing:

$> df -kh | sed -n '/\/data\/WAS\/7.0\/wasap03/p'    
/dev/xxxxx/xxxxx          5.0G  490M  4.2G  11% /data/WAS/7.0/wasap03    
/dev/xxxxx/xxxxxxx  3.5G   72M  3.3G  3% /data/WAS/7.0/wasap03/xxxx/xxxx    
/dev/xxxxxx/xxxxxxxxxx  3.5G   72M  3.3G 3% /data/WAS/7.0/wasap03/xxxx/xxxx/xxxx    

$> df -kh | sed -n '/\/data\/WAS\/7.0\/wasap03/p' | awk {'print $5, $6'}    
11% /data/WAS/7.0/wasap03    
3% /data/WAS/7.0/wasap03/xxxxx/xxxxx    
3% /data/WAS/7.0/wasap03/xxxxx/xxxx/xxxxx

My question is I only need the whole line containing only /data/WAS/7.0/wasap03 i.e the output should be only -> 11% /data/WAS/7.0/wasap03, nothing more than this. Please do not say use head -1, I need this only through sed.

Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
  • @fedorqui I'm sure that this kind of question has been asked before but I don't think that the duplicate you have proposed is correct. This question isn't about getting the nth line, it's about matching an exact pattern. – Tom Fenech Feb 13 '15 at 12:10
  • @TomFenech yep I guess you are right. Since the OP stated the `head -1`, I could imagine it was clear to him what line he was looking for. I am reopening it, then I won't be able to cast any close / duplicate vote, so please go ahead if you find an apropiate one. – fedorqui Feb 13 '15 at 12:12
  • Also, if you are looking for the `df` info of a specific filesystem, why don't you just do `df -kh /data/WAS/7.0/wasap03`? And then use `awk` to print the desired fields. – fedorqui Feb 13 '15 at 12:15
  • 1
    possible duplicate of [Sed match exact](http://stackoverflow.com/questions/16196185/sed-match-exact) – Tom Fenech Feb 13 '15 at 12:16

3 Answers3

1

If you want to match the end of line:

df -kh | sed -n '\@/data/WAS/7.0/wasap03$@p'

With awk (so you can avoid piping to awk to get fields 5 and 6:

df -kh | awk 'match($0,"/data/WAS/7.0/wasap03$"){print $5, $6}'
William Pursell
  • 204,365
  • 48
  • 270
  • 300
0

You could use the below sed command to print only the first match,

$ df -kh | sed -n '0,/\/data\/WAS\/7.0\/wasap03/p'
/dev/xxxxx/xxxxx          5.0G  490M  4.2G  11% /data/WAS/7.0/wasap03

Through awk,

$ df -kh | awk '/\/data\/WAS\/7.0\/wasap03/{print;exit}' 
/dev/xxxxx/xxxxx          5.0G  490M  4.2G  11% /data/WAS/7.0/wasap03
$ df -kh | awk '/\/data\/WAS\/7.0\/wasap03/{print $5,$6;exit}'
11% /data/WAS/7.0/wasap03
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
0
df -kh | sed -n '/\/data\/WAS\/7.0\/wasap03/ {s/.*[[:space:]]\([0-9]*%\)/\1/p;q;}'
11% /data/WAS/7.0/wasap03

i suggest to use awk instead because i assume % is alone in the line where awk work with field (same kind of probleme with space in path but you can assume not due to your filter)

NeronLeVelu
  • 9,908
  • 1
  • 23
  • 43