This section will cover cases where the input file will always contain the same number of starting and ending patterns and arranged in alternating fashion. For example, there cannot be two starting patterns appearing without an ending pattern between them and vice versa. Zero or more records of text can appear inside such groups as well as in between the groups.
The sample file shown below will be used to illustrate examples in this section. For simplicity, assume that the starting pattern is marked by startand the ending pattern by end. They have also been given group numbers to make it easier to visualize the transformation between input and output for the commands discussed in this section.
$ cat uniform.txt mango icecream --start 1-- 1234 6789 **end 1** how are you have a nice day --start 2-- a b c **end 2** par,far,mar,tar
Case 1: Processing all the groups of records based on the distinct markers, including the records matched by markers themselves. For simplicity, the below command will just print all such records.
$ awk '/start/{f=1} f; /end/{f=0}' uniform.txt --start 1-- 1234 6789 **end 1** --start 2-- a b c **end 2**
Similar to sed -n '/start/,/end/p' you can also use awk '/start/,/end/' but the state machine format is more suitable to change for various cases to follow.
Case 2: Processing all the groups of records but excluding the records matched by markers themselves.
$ awk '/end/{f=0} f{print "*", $0} /start/{f=1}' uniform.txt * 1234 * 6789 * a * b * c
Case 3-4: Processing all the groups of records but excluding either of the markers.
$ awk '/start/{f=1} /end/{f=0} f' uniform.txt --start 1-- 1234 6789 --start 2-- a b c $ awk 'f; /start/{f=1} /end/{f=0}' uniform.txt 1234 6789 **end 1** a b c **end 2**
The next four cases are obtained by just using !f instead of f from the cases shown above.
Case 5: Processing all input records except the groups of records bound by the markers.
$ awk '/start/{f=1} !f{print $0 "."} /end/{f=0}' uniform.txt mango. icecream. how are you. have a nice day. par,far,mar,tar.
Case 6 Processing all input records except the groups of records between the markers.
$ awk '/end/{f=0} !f; /start/{f=1}' uniform.txt mango icecream --start 1-- **end 1** how are you have a nice day --start 2-- **end 2** par,far,mar,tar
Case 7-8: Similar to case 6, but include only one of the markers.
$ awk '!f; /start/{f=1} /end/{f=0}' uniform.txt mango icecream --start 1-- how are you have a nice day --start 2-- par,far,mar,tar $ awk '/start/{f=1} /end/{f=0} !f' uniform.txt mango icecream **end 1** how are you have a nice day **end 2** par,far,mar,tar