我有两个.txt文件
INPUT FILE 1: contents of file 1(REQUEST DETAILS: RegRequest.txt):
2020-12-21 18:28:32,0000000001,abc@gmail.com,919876543210
2020-12-21 18:28:32,0000000002,abc@yahoo.com,919876543211
2020-12-21 18:28:32,0000000003,abc@gmail.com,919876543212
INPUT FILE 2: contents of file 2(RESPONSE DETAILS: RegReponse.txt):
0000000001
0000000003
Output file:
2020-12-21 18:28:32,0000000001,abc@gmail.com,919876543210,true
2020-12-21 18:28:32,0000000002,abc@yahoo.com,919876543211,false
2020-12-21 18:28:32,0000000003,abc@gmail.com,919876543212,true
输入文件2的内容告诉我们哪一个请求都成功,在上面的情况下,0000000001和0000000003都成功,所以我必须创建一个包含请求文件数据的输出文件,并添加新列,说明它是成功还是失败
如下所示:2020-12-21 18:28:320000000001,abc@gmail.com,919876543210,true
。
我将得到一个单独的文件,这两个文件是通过执行我的脚本生成的,但我在这一点上陷入了困境。
这是我写的脚本,根据的要求生成2个不同的文件
#!/bin/bash
#fetching only one file
`cat mainfile.cdr | grep -v Orig |awk '
BEGIN{ FS="|"; OFS="," }
{
split($4,RegReqArr,":");
if (RegReqArr[4] == 10) {
split($1,dateTime,",");
split($2,ReqidArr,":");
gsub(/^[^<]*|;.*/,"",$8)
split($17,MsisdnArr,":");
print dateTime[1],ReqidArr[2],substr($8,2,length($8)-2),MsisdnArr[2] } }' > RegRequest.txt`
`cat mainfile.cdr | grep -v Orig |awk '
BEGIN{ FS="|"; OFS="," }
{
split($4,RegReqArr,":");
if (RegReqArr[4] == 11) {
split($2,ReqidArr,":");
split($7,Response,":");
if (Response[2]==200) {
print ReqidArr[2]
} } }' > RegResponse.txt`
在生成这两个文件RegRequest.txt和RegReponse.txt后,我想生成如下的最终文件
2020-12-21 18:28:32,0000000001,abc@gmail.com,919876543210,true
2020-12-21 18:28:32,0000000002,abc@yahoo.com,919876543211,false
2020-12-21 18:28:32,0000000003,abc@gmail.com,919876543212,true
对于您显示的示例,您可以尝试以下操作吗。使用GNUawk
编写和测试。
awk '
BEGIN{
FS=OFS=","
}
FNR==NR{
arr[$0]
next
}
{
print $0,($2 in arr)?"true":"false"
}
' Input_file2 Input_file1
此外,您将字段分隔符设置为|
,这在这里是无效的,因为您显示的示例显示您有逗号分隔的行,所以在上面将其更改为,
。
解释:添加以上详细解释。
awk ' ##Starting awk program from here.
BEGIN{ ##Starting BEGIN section of this program from here.
FS=OFS="," ##Setting field separator and output field separator as , here.
}
FNR==NR{ ##Checking condition which will be TRUE when file2 is being read.
arr[$0] ##Creating array arr with index of current line.
next ##next will skip all further statements from here.
}
{
print $0,($2 in arr)?"true":"false" ##Printing current line and then printing either true OR false based on 2nd field is present in arr or not.
}
' file2 file1 ##Mentioning Input_file name here.