Prev: Possible to define a variable for only certain directories?
Next: starfield in Bash for GNU/Linux
From: pk on 18 Nov 2009 18:36 #! /shell/nerd wrote: > HI, > > sed '/AAA/,/BBB/p' <file> gives me > > AAA > some text > some text > BBB I don't think so. > 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 }'
From: Ed Morton on 18 Nov 2009 19:29 #! /shell/nerd 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? Chances are it'll require an apparently random chain of single characters in some arcane configuration. sed is an excellent tool for simple substitutions on a single line. For anything else you should use awk, perl, etc. In this case: awk '/BBB/{f=0} f; /AAA/{f=1}' file Ed.
From: Randal L. Schwartz on 18 Nov 2009 20:23 >>>>> "#!" == #! /shell/nerd <vikasera(a)gmail.com> writes: #!> 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> If you don't mind a bit of Perl, Perl gives hints when you're at the end of a range: perl -ne 'my $in = /AAA/../BBB/; print if $in and $in > 1 and not $in =~ /E/' Explanation: $in will go from 1 to n for each line within the matched range. Clearly, we don't want to print line 1. But Perl also codes line 'n' with "E0" appended, which doesn't affect its numeric value but can be detected with a regex. Slick. -- Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095 <merlyn(a)stonehenge.com> <URL:http://www.stonehenge.com/merlyn/> Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc. See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
From: Ed Morton on 18 Nov 2009 20:58 pk wrote: > #! /shell/nerd wrote: > >> HI, >> >> sed '/AAA/,/BBB/p' <file> gives me >> >> AAA >> some text >> some text >> BBB > > I don't think so. > >> 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: $ cat file1 a b AAA c d AAA e f BBB g h $ awk '/BBB/{f=0} f; /AAA/{f=1}' file1 c d AAA e f $ sed -n '/AAA/,/BBB/{ /AAA/b /BBB/b p }' file1 c d e f Regards, Ed.
From: Janis Papanagnou on 18 Nov 2009 21:04 #! /shell/nerd wrote: > On Nov 18, 5:25 pm, Janis Papanagnou <janis_papanag...(a)hotmail.com> > wrote: >> #! /shell/nerd 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> >> Mind using awk? >> >> awk '/BBB/{f=0};f;/AAA/{f=1}' >> >> Janis > > It works. Please explain it to me. I'm not so good with awk. Also I > find the Second regex is used first. Why is this order important. More legible the awk expressions are written one on each line /BBB/ {f=0} f # here default action is: print $0 (i.e. whole line) /AAA/ {f=1} This is a state machine that operates with a flag variable f which controls printing. For each input line the three awk expressions are executed sequentially. If /BBB/ is found the flag to print is reset to 0. The check for reset must come first, otherwise a line containing /BBB/ would be printed (with the second awk statement) which is not desired. Assume /AAA/ comes before the line containing f, then the line containing /AAA/ would be printed which is also undesired. Janis
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 |