
[英]If line starts with a specific string, print only the ip addresses contained within that line using awk, one per line
[英]awk list of files - output print only one line per inputfile
我想从输入文件列表中提取数据。 如果字符串包含“日期”,我需要$ 1,如果字段$ 3是“总和”,则需要$ 4,$ 5,$ 6和$ 7。 输入以制表符分隔。 input1.txt
Bla-1
Bla-2
Bla-3
Report
Date 2016.01.04
Blub-a
Blub-b
Blub-c
Blub-n
text text amount fee fee2 transit
bluber 50 5 1
blubber 40 4 1
blubbest 10 1 1
Sum 100 10 3 87
input2.txt
Bla-1
Bla-2
Bla-3
Report
Date 2016.01.11
Blub-1
Blub-2
Blub-3
Blub-n
text text amount fee fee2 transit
bluber 50 5 1
blubber 40 4 1
blubbest 10 1 1
Sum 200 10 10 180
我的输出是:
Date Sum Sum of Fee Transit
2016.01.04
2016.01.04
2016.01.04
2016.01.04
2016.01.04
2016.01.04
2016.01.04
2016.01.04
2016.01.04
2016.01.04 100 13 87
2016.01.11 100 13 87
2016.01.11 100 13 87
2016.01.11 100 13 87
2016.01.11 100 13 87
2016.01.11 100 13 87
2016.01.11 100 13 87
2016.01.11 100 13 87
2016.01.11 100 13 87
2016.01.11 100 13 87
2016.01.11 200 23 177
我想要的输出是:
Date Sum Sum of Fee Transit
2016.01.04 100 13 87
2016.01.11 200 23 177
我的awk必须经过数百个输入* .txt,而我只想提取每个输入文件所需输出中给出的信息。 AWK:
BEGIN { FS="\t"; OFS="\t";
print "Date \t Sum \t Sum of Fee \t Transit"
}
FNR==1 {flag=0}
{
if ($1~/Date/) {$1=substr($1,6,11); date=$1};
if ($3~/Sum/) {amount=$4; fee=$5+$6; transit=$7};
}
flag!=0 {print date OFS amount OFS fee OFS transit};
/Report/ {flag=1}
awk '
BEGIN {
FS=OFS="\t"
print "Date", "Sum", "Sum of Fee", "Transit"
}
sub(/^Date /,"",$1) { date = $1 }
$3=="Sum" { print date, $4, $5+$6, $7 }
' input*.txt
Date Sum Sum of Fee Transit
2016.01.04 100 13 87
2016.01.11 200 20 180
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.