Prev: extract field
Next: replacing / with - in bash
From: Ed Morton on 7 May 2010 16:31 On 5/7/2010 1:46 PM, Dave Gibson wrote: > Ed Morton<mortonspam(a)gmail.com> wrote: >> On 5/7/2010 6:31 AM, Ed Morton wrote: >>> On 5/6/2010 4:30 PM, Bit Twister wrote: >>>> On Thu, 6 May 2010 14:18:50 -0700 (PDT), James wrote: >>>>> How to print head and tail (let's say 7 lines)? >>>> >>>> head -7 /some/filename ; tail -7 /same/filename >>>> >>> >>> For the OP - if the file contains less than 14 lines do you want the >>> middle ones printed twice? >> >> If the answer's no, then use: >> >> awk '(NR<=7) || (NR>(nr-7))' nr=$(wc -l< file) file > > { head -n 7 ; tail -n 7 ; }< file OK, I tried it and it worked. How? Ed.
From: Janis Papanagnou on 7 May 2010 16:48 Ed Morton wrote: > On 5/7/2010 1:46 PM, Dave Gibson wrote: >> Ed Morton<mortonspam(a)gmail.com> wrote: >>> On 5/7/2010 6:31 AM, Ed Morton wrote: >>>> On 5/6/2010 4:30 PM, Bit Twister wrote: >>>>> On Thu, 6 May 2010 14:18:50 -0700 (PDT), James wrote: >>>>>> How to print head and tail (let's say 7 lines)? >>>>> >>>>> head -7 /some/filename ; tail -7 /same/filename >>>>> >>>> >>>> For the OP - if the file contains less than 14 lines do you want the >>>> middle ones printed twice? >>> >>> If the answer's no, then use: >>> >>> awk '(NR<=7) || (NR>(nr-7))' nr=$(wc -l< file) file >> >> { head -n 7 ; tail -n 7 ; }< file > > OK, I tried it and it worked. How? The compound command will be fed by the file contents; first head will read from stdin until it got what it wants (seven lines), then the next command tail will start and read the rest of the input. A cute solution, isn't it? Janis > > Ed.
From: Stephane CHAZELAS on 9 May 2010 05:51 2010-05-07, 22:48(+02), Janis Papanagnou: [...] >>> { head -n 7 ; tail -n 7 ; }< file >> >> OK, I tried it and it worked. How? > > The compound command will be fed by the file contents; first head will read > from stdin until it got what it wants (seven lines), then the next command > tail will start and read the rest of the input. A cute solution, isn't it? [...] It should work with POSIX head and tail on seekable files, but there's no guarantee on non-seekable ones as head(1) may read past the 7th last line of the file: $ mkfifo p $ seq 40 > p & $ { head -n7; tail -n7; } < p 1 2 3 4 5 6 7 $ seq 4000 > p & $ { head -n7; tail -n7; } < p 1 2 3 4 5 6 7 3994 3995 3996 3997 3998 3999 4000 $ -- Stéphane
From: Michael Paoli on 12 May 2010 03:02
On May 6, 2:18 pm, James <hslee911(a)yahoo.com> wrote: > How to print head and tail (let's say 7 lines)? Let's presume you don't want lines in duplicate for input of less than 14 lines, then: sed -ne ' 1,7{;p;$q;d;} 8{ $!N;$!N;$!N;$!N;$!N;$!N } :b $!{;N;D;bb;} p ' |