AwK align longer columns to next line and keep sam

2019-08-14 15:10发布

Hi i have this problem with this type of input data:

input file which is contain those columns:

file1 (original file)

7 LINE_A G_VALUEFA D_VALUEFA SEAT01 SEAT02 SEAT03 SEAT04      
7 LINE_B G_VALUEFA D_VALUEFA SEAT22 SEAT25 SEAT27 EXNUM899999SSSSS9S8S5S2S8 EXNUM899999SSSSS9S8S5S2S8 EXNUM899999SSSSS9S8S5S2S8 EXNUM899999SSSSS9S8S5S2S8
7 LINE_C G_PREFX D_VALUEFX SEAT01 SEAT02 SEAT03 SEAT04      
8 LINE_G G_PREFX D_VALUEFX POSITION55 POSITION82 VALUE85 POSITION44 POSITION448 POSITION448 POSITION448
7 LINE_C G_PREFA D_VALUEFA SEAT01 SEAT02 SEAT03 

5 Lines processed

I tried to make it align long columns to next line which is resulting with help of following code of:

awk '{for (i = 1; i <= NF; i += 9) print $i, $(i+1), $(i+2), $(i+3), $(i+4), $(i+5), $(i+6), $(i+7), $(i+8), $(i+9)}' file1

file1 (processed file)

7 LINE_A G_VALUEFA D_VALUEFA SEAT01 SEAT02 SEAT03 SEAT04
7 LINE_B G_VALUEFA D_VALUEFA SEAT22 SEAT25 SEAT27 EXNUM899999SSSSS9S8S5S2S8 EXNUM899999SSSSS9S8S5S2S8 EXNUM899999SSSSS9S8S5S2S8
EXNUM899999SSSSS9S8S5S2S8
7 LINE_C G_PREFX D_VALUEFX SEAT01 SEAT02 SEAT03 SEAT04
8 LINE_G G_PREFX D_VALUEFX POSITION55 POSITION82 VALUE85 POSITION44 POSITION448 POSITION448
POSITION448 
7 LINE_C G_PREFA D_VALUEFA SEAT01 SEAT02 SEAT03

7 Lines processed

@Goal is to make it output like this or even better based on your idea, but still to keep same number of processed lines

7 LINE_A G_VALUEFA D_VALUEFA SEAT01 SEAT02 SEAT03 SEAT04
7 LINE_B G_VALUEFA D_VALUEFA SEAT22 SEAT25 SEAT27 EXNUM899999SSSSS9S8S5S2S8 EXNUM899999SSSSS9S8S5S2S8 EXNUM899999SSSSS9S8S5S2S8
          EXNUM899999SSSSS9S8S5S2S8
7 LINE_C G_PREFX D_VALUEFX SEAT01 SEAT02 SEAT03 SEAT04
8 LINE_G G_PREFX D_VALUEFX POSITION55 POSITION82 VALUE85 POSITION44 POSITION448 POSITION448
          POSITION448 
7 LINE_C G_PREFA D_VALUEFA SEAT01 SEAT02 SEAT03

5 Lines processed  

for counting i am using this code below: but after shortening with AWK loop code, it resulting into NR of lines 7

awk '{print $0;NR;}END {print "\n" "\033[48;5;085;38;5;000m" NR, "Lines processed""\033[0m";}'

Thank you, for any ideas with this

1条回答
时光不老,我们不散
2楼-- · 2019-08-14 15:21

is this what you're looking for?

$ cat file
7 LINE_A G_VALUEFA D_VALUEFA SEAT01 SEAT02 SEAT03 SEAT04
7 LINE_B G_VALUEFA D_VALUEFA SEAT22 SEAT25 SEAT27 EXNUM899999SSSSS9S8S5S2S8 EXNUM899999SSSSS9S8S5S2S8 EXNUM899999SSSSS9S8S5S2S8
7 LINE_C G_PREFX D_VALUEFX SEAT01 SEAT02 SEAT03 SEAT04
8 LINE_G G_PREFX D_VALUEFX POSITION55 POSITION82 VALUE85 POSITION44 POSITION448 POSITION448 POSITION448
7 LINE_C G_PREFA D_VALUEFA SEAT01 SEAT02 SEAT03

$ cat tst.awk
{
    for (i=1; i<=NF; i++) {
        printf "%s%s", $i, (i<NF ? (i%9 ? OFS : ORS "\t") : ORS)
    }
}

$ awk -f tst.awk file
7 LINE_A G_VALUEFA D_VALUEFA SEAT01 SEAT02 SEAT03 SEAT04
7 LINE_B G_VALUEFA D_VALUEFA SEAT22 SEAT25 SEAT27 EXNUM899999SSSSS9S8S5S2S8 EXNUM899999SSSSS9S8S5S2S8
        EXNUM899999SSSSS9S8S5S2S8
7 LINE_C G_PREFX D_VALUEFX SEAT01 SEAT02 SEAT03 SEAT04
8 LINE_G G_PREFX D_VALUEFX POSITION55 POSITION82 VALUE85 POSITION44 POSITION448
        POSITION448 POSITION448
7 LINE_C G_PREFA D_VALUEFA SEAT01 SEAT02 SEAT03

$ awk -f tst.awk file | awk '!/^\t/{++c} END{print c+0, "Lines processed"}'
5 Lines processed

Obviously you could just print NR in the END of the first script if you wanted to print the "Lines processed" number there so I'm assuming you want to print it from some subsequent script though I can't imagine why.

查看更多
登录 后发表回答