Prev: Possible to define a variable for only certain directories?
Next: starfield in Bash for GNU/Linux
From: Rakesh Sharma on 19 Nov 2009 01:42 On Nov 19, 4:14 am, "#! /shell/nerd" <vikas...(a)gmail.com> wrote: > HI, > > sed '/AAA/,/BBB/p' <file> gives me > > AAA > some text > some text > BBB > > Now I want is the out put without AAA or BBB. How is this possible > with a single sed command? I tried following but didn't work - > sed '/AAA/,/BBB/{ 1d; $d; p; }' <file> On some seds (e.g., GNU sed): sed -e '/AAA/,/BBB/!d;//d' yourfile But you can always do this too: sed -e '/AAA/,/BBB/!d;/AAA/d;/BBB/d' yourfile sed -ne '/AAA/,/BBB/{;/AAA/!{;/BBB/!p;};}' yourfile
From: Ed Morton on 19 Nov 2009 04:00 Rakesh Sharma wrote: > On Nov 19, 4:14 am, "#! /shell/nerd" <vikas...(a)gmail.com> wrote: >> HI, >> >> sed '/AAA/,/BBB/p' <file> gives me >> >> AAA >> some text >> some text >> BBB >> >> Now I want is the out put without AAA or BBB. How is this possible >> with a single sed command? I tried following but didn't work - >> sed '/AAA/,/BBB/{ 1d; $d; p; }' <file> > > On some seds (e.g., GNU sed): > sed -e '/AAA/,/BBB/!d;//d' yourfile > > But you can always do this too: > sed -e '/AAA/,/BBB/!d;/AAA/d;/BBB/d' yourfile > > sed -ne '/AAA/,/BBB/{;/AAA/!{;/BBB/!p;};}' yourfile > The GNU one seems to work (though I don't understand why, but then I've only been using sed for 25 years!), but the other 2 delete lines containing the start pattern if they appear within the selected region: $ cat file1 a AAA b AAA c BBB d $ sed -e '/AAA/,/BBB/!d;//d' file1 b AAA c $ sed -e '/AAA/,/BBB/!d;/AAA/d;/BBB/d' file1 b c $ sed -ne '/AAA/,/BBB/{;/AAA/!{;/BBB/!p;};}' file1 b c Regards, Ed.
From: pk on 19 Nov 2009 04:20 Ed Morton wrote: >>> Now I want is the out put without AAA or BBB. How is this possible >>> with a single sed command? I tried following but didn't work - >>> sed '/AAA/,/BBB/{ 1d; $d; p; }' <file> >> >> Just exclude the endpoints: >> >> sed -n '/AAA/,/BBB/{ >> /AAA/b >> /BBB/b >> p >> }' > > That would delete other lines within the selected range that > also just happened to match the start pattern: Well, I based my answer on the output he showed. But what you say is true. It can still be done with sed, but it's probably not worth the effort since cleaner solutions have been posted already. Thanks for catching that.
From: Ed Morton on 19 Nov 2009 05:18 pk wrote: > Ed Morton wrote: > >>>> Now I want is the out put without AAA or BBB. How is this possible >>>> with a single sed command? I tried following but didn't work - >>>> sed '/AAA/,/BBB/{ 1d; $d; p; }' <file> >>> Just exclude the endpoints: >>> >>> sed -n '/AAA/,/BBB/{ >>> /AAA/b >>> /BBB/b >>> p >>> }' >> That would delete other lines within the selected range that >> also just happened to match the start pattern: > > Well, I based my answer on the output he showed. Yeah, and that's always more challenging when the OP doesn't post any input! Ed. > But what you say is true. It can still be done with sed, but it's probably > not worth the effort since cleaner solutions have been posted already. > Thanks for catching that. >
From: Rakesh Sharma on 19 Nov 2009 13:56 On Nov 19, 2:00 pm, Ed Morton <mortons...(a)gmail.com> wrote: > Rakesh Sharma wrote: > > On Nov 19, 4:14 am, "#! /shell/nerd" <vikas...(a)gmail.com> wrote: > >> HI, > > >> sed '/AAA/,/BBB/p' <file> gives me > > >> AAA > >> some text > >> some text > >> BBB > > >> Now I want is the out put without AAA or BBB. How is this possible > >> with a single sed command? I tried following but didn't work - > >> sed '/AAA/,/BBB/{ 1d; $d; p; }' <file> > > > On some seds (e.g., GNU sed): > > sed -e '/AAA/,/BBB/!d;//d' yourfile > > > But you can always do this too: > > sed -e '/AAA/,/BBB/!d;/AAA/d;/BBB/d' yourfile > > > sed -ne '/AAA/,/BBB/{;/AAA/!{;/BBB/!p;};}' yourfile > > The GNU one seems to work (though I don't understand why, but then I've only > been using sed for 25 years!), but the other 2 delete lines containing the start > pattern if they appear within the selected region: > > $ cat file1 > a > AAA > b > AAA > c > BBB > d > $ sed -e '/AAA/,/BBB/!d;//d' file1 > b > AAA > c > $ sed -e '/AAA/,/BBB/!d;/AAA/d;/BBB/d' file1 > b > c > $ sed -ne '/AAA/,/BBB/{;/AAA/!{;/BBB/!p;};}' file1 > b > c > > Regards, > > Ed. sed -e ' /AAA/,/BBB/!d /AAA/{ x;/./!d;x;b } /BBB/d ' yourfile
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 Prev: Possible to define a variable for only certain directories? Next: starfield in Bash for GNU/Linux |