Chunk text files with BASH

191 views Asked by At

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
2

There are 2 answers

1
Jon On

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'
0
Ed Morton On

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
        }
    '