想要读取第6行(var = i),第9行(var = j),第12行(var = k)然后输出为第一行 然后增加10行(i + 10; j + 10; k + 10)并输出为第二行,依此类推......
INPUT.TXT
MN
LIST
DETAILS
AAAA BBBB CCCC
123421751116849 119865688385 IDLE
DATE TIME DDDD
150203 161044 123-42-50300-14481
EEEE
111394052866500
DETAILS
AAAA BBBB CCCC
123421825027128 119698116932 IDLE
DATE TIME DDDD
150204 183507 123-42-3200-10793
EEEE
352708042150790
DETAILS
AAAA BBBB CCCC
123421893844511 118675320014 IDLE
DATE TIME DDDD
150204 222245 123-42-50000-1823
EEEE
352663056076930
DETAILS
AAAA BBBB CCCC
预期的Output.txt
AAAA,BBBB,CCCC,日期,时间,DDDD,EEEE
123421751116849,119865688385,IDLE,150203,161044,123-42-50300-14481,111394052866500
123421825027128,119698116932,IDLE,150204,183507,123-42-3200-10793,352708042150790
123421893844511,118675320014,IDLE,150204,222245,123-42-50000-1823,352663056076930
我已尝试过以下命令和in-complete
awk 'BEGIN { FS" "; OFS=","} { for (i=6;j=9;k=12;i<=NR;j<=NR;k<=NR;i+10;j+10;k+10) print $i,$j,$j }' Input.txt
寻找你的建议!!!
答案 0 :(得分:2)
我使用
awk -v RS=DETAILS -v OFS=, 'NR == 2 { print $1, $2, $3, $7, $8, $9, $13 } NR != 1 { print $4, $5, $6, $10, $11, $12, $14 }' filename
诀窍是将文件拆分为由DETAILS
分隔的记录而不是换行符,然后从这些记录中挑选出适当的字段。 NR == 2
块仅用于生成标题行。 NR == 1
是第一个DETAILS
之前的绒毛,因此被忽略。