shell - 通过比较 shell 脚本中的 2 个文件来生成输出文件
问题描述
我有 2 个 .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:32,0000000001,abc@gmail.com,919876543210, true
.
我将获得 1 个单个文件,这 2 个文件是通过执行我的脚本生成的,但此时我被卡住了。
这是我编写的脚本,用于根据要求生成 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
解决方案
使用您显示的示例,您能否尝试以下操作。用 GNU 编写和测试awk
。
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.
推荐阅读
- multithreading - Android Jetpack 在可组合项之间传递数据
- docker - 如何使用 Docker Inspect 识别暂存图像?
- python - Python 导入在使用文件作为模块时和直接调用时都有效
- c - 如何测试 fork(); 窗户上的功能
- javascript - “未捕获(承诺)错误:'activeTab' 权限无效,因为尚未调用此扩展。” 在 Chrome 扩展程序中
- r - 如何保存具有从 for 循环创建的多个子图的图
- c# - 生产服务器上的 Microsoft Edge 兼容性
- spring-batch - Spring Batch JdbcPagingItemReader 按两列/键排序
- javascript - 使用 React 和 VS Code 导入模块
- swift - 如何在 Xcode 中将标签更改为按钮