There is a view script

awk '/before[az]+after/{print > ?? ".txt" }' filename 

Instead ?? it is required to obtain the recognized value of the [az] + pattern, so that the file name equals this value.

How to achieve this?

    4 answers 4

    Not this way?

      awk '{if (match($0, /before([az]+)after/, a)) {print a[1]}}' 

    However, another encrypted question ...

       ...print > $1... [s@pandora6 20130303]$ cat qwert.txt beforeaaaaafter beforebbbaaaaafter beforeaacccaaafter beforeassssaaaafter [s@pandora6 20130303]$ awk '/before[az]+after/{print > $1 ".txt" }' qwert.txt [s@pandora6 20130303]$ ls -t1r beforebbbaaaaafter.txt beforeassssaaaafter.txt beforeaacccaaafter.txt beforeaaaaafter.txt 

      I do not know why you need it .. but it seems so))

        Assuming that you can use standard backreferences in awk. Take [az] + in parentheses (you get a group) and you can get its content using the group number \ \ group number.

        Total should make something like

         awk '/before([az]+)after/{print > \1 ".txt" }' filename 
        • Not as far as I know - ReinRaus

        Who cares, it turned out through gawk:

         cat filename | gawk 'match($0, /before([az]+)after/, x) {print $0 > x[1] ".txt" }' 
        • I wanted to offer the same, did not have time :) gawk and match - ReinRaus
        • @ReinRaus and where is not the infamous awk? - alexlz
        • @alexlz, I did not understand the question - ReinRaus
        • @ReinRaus awk, which is not gnu awk, i.e. gawk - alexlz
        • I have one on bubunt. For example, the command from this answer to awk will generate a syntax error, and gawk will execute and produce a result, which means that awk is not GNUshny. - ReinRaus