Subhadeep Ghosh
Subhadeep Ghosh

Reputation: 3

Get a specific line from an output using SED in linux

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.

Upvotes: 0

Views: 336

Answers (3)

William Pursell
William Pursell

Reputation: 212248

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

Upvotes: 1

NeronLeVelu
NeronLeVelu

Reputation: 10039

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)

Upvotes: 0

Avinash Raj
Avinash Raj

Reputation: 174706

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

Upvotes: 0

Related Questions