1

Which command should I use to print a part of text files knowing where this part starts and finishes?

For example:

columnA columnB columnC
baba     bobo    bibi
caca     coco    cici  
.         .      .
.         .      .
zaza    zeze     zizi
EndA    EndB    EndC

In all my files I have exactly the same text for columnA(columnB ...) and EndA(EndB ...), what I want to do is to print what is in between them, i.e., baba(bobo ...) which is different for each file.

I was doing that using, grep | cut | tail, but then I have to write ( and find) new patterns each time I have a new file as the number of lines of each of them are different. I suppose there is a much more smarter ( and generic) way to do that using awk or sed.

Rich
  • 5,417
  • 9
  • 36
  • 58
ziulfer
  • 1,271
  • 5
  • 15
  • 28
  • IIUC, [this](http://stackoverflow.com/questions/1187354/excluding-first-and-last-lines-from-sed-start-end) might be an answer to your question. – Thor Feb 11 '13 at 17:34

4 Answers4

1

If you're looking print all but the first and last lines you could do

sed '1d; $d' file.txt
Eric
  • 2,006
  • 13
  • 11
1

and next to the sed solution, awk can be used as wel:

BEGIN {
  output=0;
}

$1 ~ /columnA/ || $1 ~ /endA/ {
  output=!output;
  next;
}

{ if(start_output) print $0 }
0

sed has ranges:

 sed -ne '/^columnA/,/^EndA/p'
thiton
  • 35,082
  • 3
  • 67
  • 98
0

you can use head and tail

head -n-1 temp.txt | tail -n +2

Output

baba     bobo    bibi
caca     coco    cici
.         .      .
.         .      .
zaza    zeze     zizi
Mirage
  • 29,790
  • 59
  • 163
  • 256