我尝试将标题添加到输出文件中并尝试" print" /" printf"但仍然没有成功将标题添加到输出文件。有没有其他方法可以将标题添加到输出文件中?
以下是我使用的代码:
begin {
print "rtype cell# IMSI IMEI rcID scID SrID"
}
{
a1=substr($0,1,2) ## Record type
a2=substr($0,3,18) ## Cellular number
a3=substr($0,21,15) ## IMSI number
a4=substr($0,36,15) ## IMEI
a5=substr($0,51,8) ## Receiving company ID
a6=substr($0,59,8) ## Sending company ID
a7=substr($0,67,8) ## Serving company ID
a8=substr($0,75,8) ## Call placed date
a9=substr($0,83,6) ## Call placed time
b1=substr($0,89,6) ## Elapsed time
b2=substr($0,95,6) ## Chargeable time
b3=substr($0,101,6) ## Chargeable units
b4=substr($0,107,8) ## Rated date
b5=substr($0,115,21) ## Called number
b6=substr($0,136,1) ## Call direction
b7=substr($0,137,1) ## Completion indicator
b8=substr($0,138,1) ## Termination Indicator
b9=substr($0,139,8) ## Latitude
c1=substr($0,147,8) ## Longitude
c2=substr($0,155,1) ## Air time rate period
c3=substr($0,156,11) ## Air time charges
c4=substr($0,167,11) ## Surcharges
c5=substr($0,178,1) ## Action code (1)
c6=substr($0,179,2) ## Supp service code (1)
c7=substr($0,181,1) ## Action code (2)
c8=substr($0,182,2) ## Supp service code (2)
c9=substr($0,184,1) ## Action code (3)
d1=substr($0,185,2) ## Supp service code (3)
d2=substr($0,187,1) ## Action code (4)
d3=substr($0,188,2) ## Supp service code (4)
d4=substr($0,190,1) ## Action code (5)
d5=substr($0,191,2) ## Supp service code (5)
d6=substr($0,193,15) ## Called place
d7=substr($0,208,15) ## Calling place
d8=substr($0,223,1) ## Zone indicator
d9=substr($0,224,1) ## Toll rate period
e1=substr($0,225,8) ## Toll call placed date
e2=substr($0,233,6) ## Toll call placed time
e3=substr($0,239,6) ## Toll call time
e4=substr($0,245,11) ## Toll charges
e5=substr($0,256,7) ## Distance
e6=substr($0,263,3) ## Transaction Code
e7=substr($0,266,5) ## Product Plan Code
e8=substr($0,271,4) ## Bill Cycle
e9=substr($0,275,11) ## Total Taxes
f1=substr($0,286,11) ## Total Charges
f2=substr($0,297,1) ## Call Type
f3=substr($0,298,1) ## GSM Service Type
f4=substr($0,299,2) ## GSM Service Code
f5=substr($0,301,15) ## MSC ID
f6=substr($0,316,2) ## Relaxed Rate Discount Flag
f7=substr($0,318,1) ## Filler
printf("%s|%s|%s|%s|%s|%s|%s|%s|%s|",a1,a2,a3,a4,a5,a6,a7,a8,a9)
printf("%s|%s|%s|%s|%s|%s|%s|%s|%s|",b1,b2,b3,b4,b5,b6,b7,b8,b9)
printf("%s|%s|%s|%s|%s|%s|%s|%s|%s|",c1,c2,c3,c4,c5,c6,c7,c8,c9)
printf("%s|%s|%s|%s|%s|%s|%s|%s|%s|",d1,d2,d3,d4,d5,d6,d7,d8,d9)
printf("%s|%s|%s|%s|%s|%s|%s|%s|%s|",e1,e2,e3,e4,e5,e6,e7,e8,e9)
printf("%s|%s|%s|%s|%s|%s|%s\n",f1,f2,f3,f4,f5,f6,f7)
}
END {
}
答案 0 :(得分:1)
正如@Kaz所提到的,您需要BEGIN
而不是begin
,但如果您使用的是GNU awk,则不需要所有这些substr()调用和显式printf语句您可以使用FIELDWIDTHS指定每个字段的宽度,在字段之间使用|
重新编译记录,然后执行一次打印。例如:
$ cat file
abcdefghijklmnopqrst
12345678901234567890
$ awk 'BEGIN{FIELDWIDTHS="2 3 2 4 2 5 2"; OFS="|"} {$1=$1} 1' file
ab|cde|fg|hijk|lm|nopqr|st
12|345|67|8901|23|45678|90
在您的情况下,您只需:
BEGIN {
FIELDWIDTHS="2 18 15 15 8 8 8 ..."
OFS="|"
print "rtype","cell#","IMSI","IMEI","rcID","scID","SrID"
}
{
$1=$1
print
}
请注意,指定空END部分无用