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,603
  • 9
  • 39
  • 61
ziulfer
  • 1,339
  • 5
  • 18
  • 30
  • 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,056
  • 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,651
  • 4
  • 70
  • 100
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
  • 30,868
  • 62
  • 166
  • 261