0

I am relatively new to Bash and I would like to chunk a txt contained in a zip file. The chunk must be from 5 lines up from "channel8" to 2 lines up from "channel10"

12082008;pull done;ret=34;Y
08072008;push hanged;s=3;N
15082008;pull done;ret=34;Y
01062008;psuh done;ret=23;Y
18082007;old entry;old;N
08072008;push hanged;s=3;N
15082008;pull done;ret=34;Y
01062008;psuh done;ret=23;Y
18082007;old entry;old;N
08072008;push hanged;s=3;N
15082008;pull done;ret=34;Y
01062008;psuh done;ret=23;Y
18082007;old entry;old;N
08072008;push hanged;s=3;N
15082008;pull done;ret=34;Y
01062008;psuh done;ret=23;Y
18082007;old entry;old;N
Channel8
08072008;push hanged;s=3;N
15082008;pull done;ret=34;Y
01062008;psuh done;ret=23;Y
18082007;old entry;old;N
Channel9
08072008;push hanged;s=3;N
15082008;pull done;ret=34;Y
01062008;psuh done;ret=23;Y
18082007;old entry;old;N
Channel10
08072008;push hanged;s=3;N
15082008;pull done;ret=34;Y
01062008;psuh done;ret=23;Y
18082007;old entry;old;N
Channel11
01062008;psuh done;ret=23;Y

So far I have only succeed to chunk up to channel 10, see below:

12082008;pull done;ret=34;Y
08072008;push hanged;s=3;N
15082008;pull done;ret=34;Y
01062008;psuh done;ret=23;Y
18082007;old entry;old;N
08072008;push hanged;s=3;N
15082008;pull done;ret=34;Y
01062008;psuh done;ret=23;Y
18082007;old entry;old;N
08072008;push hanged;s=3;N
15082008;pull done;ret=34;Y
01062008;psuh done;ret=23;Y
18082007;old entry;old;N
08072008;push hanged;s=3;N
15082008;pull done;ret=34;Y
01062008;psuh done;ret=23;Y
18082007;old entry;old;N
Channel8
08072008;push hanged;s=3;N
15082008;pull done;ret=34;Y
01062008;psuh done;ret=23;Y
18082007;old entry;old;N
Channel9
08072008;push hanged;s=3;N
15082008;pull done;ret=34;Y
01062008;psuh done;ret=23;Y
18082007;old entry;old;N
Channel10
08072008;push hanged;s=3;N
15082008;pull done;ret=34;Y
01062008;psuh done;ret=23;Y
18082007;old entry;old;N

but I do not know how to use my bash output to select from channel 8.

This is my code

for file in ./*.zip; do head -$(($(unzip -c $file | grep -n $channel_after | cut -f1,1 -d":")-1)) <(unzip -c $file);done
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
JPV
  • 1,079
  • 1
  • 18
  • 44
  • 1
    Post the expected output. It's not clear when you say "up to channel 10" is you mean "up to the line that contains the string `channel10`" or "up to the end of the block that starts with the line `channel10`" or something else so different people are making different assumptions in their answers. – Ed Morton Jul 19 '18 at 21:41

2 Answers2

1

Would sed do the job?

unzip -c $file | sed -n '/Channel8/,/Channel10/p'

That sed command will print everything between Channel8 and Channel10, including the Channel lines.

If you don't want the Channel lines included you could use another sed to delete rather than print.

unzip -c $file | sed '1,/Channel8/d;/Channel10/,$d'
Jon
  • 3,573
  • 2
  • 17
  • 24
  • Thanks! What If I would like to chunk 4 lines up from Channel8 and 2 lines up from Channel10? Consider now that the word channel is not at the beginning of the line. – JPV Jul 20 '18 at 00:47
-1

it sounds like what you need is:

unzip -c "$file" |
    awk '
        /^Channel8$/ { seenBeg = 1 }

        seenBeg {
            if ( seenEnd && /^Channel/ ) {
                exit
            }
            else if ( /^Channel10$/ {
                seenEnd = 1
            }
            print
        }
    '
Ed Morton
  • 188,023
  • 17
  • 78
  • 185