print block of text to file from awk script [banner like]

前端 未结 3 1049
小蘑菇
小蘑菇 2020-12-22 01:13

I have awk script doing some processing and sending it\'s output to a file. How would I writeout in BEGIN block of my awk program a banner-like mes

3条回答
  •  春和景丽
    2020-12-22 01:24

    Is this some that you look for?

    var="Peter Hanson"
    
    awk -v auth="$var" '
    BEGIN {print "#########################################"
        print "#      generated by some author         #"
        printf "#";
        l=int((41-length(auth))/2)
        r=((41-length(auth))/2-l)*2
            for (i=1;i<=l;i++) 
            printf " "
        printf "%s",auth
        for (i=1;i<=l+r-2;i++) 
            printf " "
        print "#"
        print "#########################################"
        }' file
    #########################################
    #      generated by some author         #
    #              Peter Hanson             #
    #########################################
    

    This will take the data in the variable var and print it as the second line.
    It does adjust the field with, so it its centered.
    You need to enter your code for the rest after the last print

提交回复
热议问题