abhijit dhakane
abhijit dhakane

Reputation: 27

Bash for file parsing every nth occurance with a structured file having fix pattern

I have a big file with start pattern END at every instance (a snapshots), How do I write new file that can write at every nth instance. e.g. every 2nd snap [1,3,5,..] or every 25th snap [1, 25, 50, 75, 100, ..] with file having snapshots [1,2,3,4,5...1000] with following example pattern.

example:

END
qweqweq
qweqweq
qweqwe
END
typowoeri
ertpoietp
operitope
END
retorter
erteteert
ewrtert

I want

END
qweqweq
qweqweq
qweqwe
END
retorter
erteteert
ewrtert
awk '/END/{i++}{print > "file."i}' file

I used above code to split the file into individual files. I have 3 snaps, and I am able get 3 individual files. But I want a single file with every 2nd snap in it?

Upvotes: 2

Views: 57

Answers (1)

anubhava
anubhava

Reputation: 785721

You can probably use this awk that prints lines for each END block that is not divisible by 2:

awk '/^END$/{++n} n%2' file

END
qweqweq
qweqweq
qweqwe
END
retorter
erteteert
ewrtert

Upvotes: 2

Related Questions