rveri
January 22, 2013, 2:06pm
1
awk , sed Experts,
I want to remove first and last line after pattern match "vg" :
I am trying : # sed '1d;$d' works fine , but where the last line is not having vg entry it is deleting one line of data.
So it should check for the pattern vg if present , then it should delete the line , else do nothing.
file1 :
/dev/vgP2XT
/dev/disk/disk4415
/dev/disk/disk4335
/dev/disk/disk4214
/dev/disk/disk4413
/dev/disk/disk4423
/dev/disk/disk41313
/dev/disk/disk21063
/dev/disk/disk20702
/dev/disk/disk21423
/dev/disk/disk21122
/dev/vgQ4XT
I have tried: the above sed on the file2 , but cutting one data line, that I don't want in the output :
file2 :
/dev/vgP2XT
/dev/disk/disk4415
/dev/disk/disk4335
/dev/disk/disk4214
/dev/disk/disk4413
/dev/disk/disk4423
/dev/disk/disk41313
/dev/disk/disk21063
/dev/disk/disk20702
/dev/disk/disk21423
/dev/disk/disk21122
Tried:
sed '1d;$d' , but it is deleting last line "disk21122"
How to tell sed to check for the pattern, and then delete, if possible.
So I want the output should be:
/dev/disk/disk4415
/dev/disk/disk4335
/dev/disk/disk4214
/dev/disk/disk4413
/dev/disk/disk4423
/dev/disk/disk41313
/dev/disk/disk21063
/dev/disk/disk20702
/dev/disk/disk21423
/dev/disk/disk21122
Thanks,
Try:
grep '/dev/d'
grep -v '/dev/vg'
--
or, if it needs to be only on the 1st and the last line:
sed '1{\|/dev/vg|d;}; ${//d;}' infile
1 Like
rveri
January 22, 2013, 5:50pm
3
Hi Scrutinizer ,
Thanks, but the sed did not work well,
please advise, how not to print, first and last line if pattern matches to the first and last line.
# cat infile
/dev/vgP2XT
/dev/disk/disk4415
/dev/disk/disk4335
/dev/disk/disk4214
/dev/disk/disk4413
/dev/disk/disk4423
/dev/disk/disk41313
/dev/disk/disk21063
/dev/disk/disk20702
/dev/disk/disk21423
/dev/disk/disk21122
/dev/vgQ4XT
# sed '1{\|/dev/vg|d;}; ${//d;}' infile
/dev/disk/disk4415
/dev/disk/disk4335
/dev/disk/disk4214
/dev/disk/disk4413
/dev/disk/disk4423
/dev/disk/disk41313
/dev/disk/disk21063
/dev/disk/disk20702
/dev/disk/disk21423
/dev/disk/disk21122
/dev/vgQ4XT
#
It did not removed the last "vg" pattern matched entry. /dev/vgQ4XT
Try this instead:
sed '\|/dev/vg|{1d; $d;}' infile
1 Like
rveri
January 24, 2013, 3:13pm
5
Thanks Scrutinizer, this works great .
sed '\|/dev/vg|{1d; $d;}' infile
I am not able to understand why "| | " used in between ,
Could please explain , Thanks again.
Hi revri, \| .. |
is the same as / .. /
, the difference is that the /
characters in /dev/vg
would need to be escaped:
sed '/\/dev\/vg/{1d; $d;}' infile
You can use most characters as long as the first one is escaped:
sed '\:/dev/vg:{1d; $d;}' infile