Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 删除文件中的匹配对 在我寻找C++代码中特别持久内存泄漏的过程中,我决定将所有分配写入日志文件,格式如下:_Regex_Shell_Sed_Awk - Fatal编程技术网

Regex 删除文件中的匹配对 在我寻找C++代码中特别持久内存泄漏的过程中,我决定将所有分配写入日志文件,格式如下:

Regex 删除文件中的匹配对 在我寻找C++代码中特别持久内存泄漏的过程中,我决定将所有分配写入日志文件,格式如下:,regex,shell,sed,awk,Regex,Shell,Sed,Awk,例如,这给了我: alloc 232108 60 405766 file1.cpp (3572) free 232128 60 405766 alloc 232108 60 405767 file1.cpp (3572) free 232128 60 405767 alloc 7a3620 12516 405768 file2.cpp (11435) free 7a3640 12516 405768 alloc 2306c8

例如,这给了我:

alloc 232108     60   405766 file1.cpp (3572)
free  232128     60   405766
alloc 232108     60   405767 file1.cpp (3572)
free  232128     60   405767
alloc 7a3620  12516   405768 file2.cpp (11435)
free  7a3640  12516   405768
alloc 2306c8    256   405769 file3.cpp (3646)
alloc 746160   6144   405770 file3.cpp (20462)
alloc 6f3528   2048   405771 file4.h (153)
alloc 6aca50    128   405772 file4.h (153)
alloc 632ec8    128   405773 file4.h (153)
alloc 732ff0    128   405774 file4.h (153)
free  746180   6144   405770
free  632ee8    128   405773
alloc 6a7610   2972   405778 this_alloc_has_no_counterpart.cpp (123)
free  6aca70    128   405772
free  733010    128   405774
free  6f3548   2048   405771
alloc 6a7610   2972   405775 file3.cpp (18043)
alloc 7a3620  12316   405776 file5.cpp (474)
alloc 631e00    256   405777 file3.cpp (18059)
free  7a3640  12316   405776
free  6a7630   2972   405775
free  631e20    256   405777
free  2306e8    256   405769
我试图将每个
alloc
匹配到一个
free
,只保留
alloc
s而没有
free
对应项,例如,分配号
405778

我能想到的是以下shell脚本:

#/垃圾箱/垃圾箱
grep“^alloc”test.txt”同时读取行
做
alloc_nr=`echo$line | awk'{print$4}`` arg4=分配号
echo“处理$alloc\n”
sed-i“/${alloc_nr}/{//d}”test.txt
完成
正如您可能已经猜到的,在一个25MB的文件上,这个速度非常慢(即每秒2个循环),大约有144000个
alloc
s,因为我使用
sed
的效率非常低


如果有人能给我一个正确的方向,告诉我如何在不花费三个小时的情况下实现这一点,我将不胜感激。

似乎您只需要ID,而不是整个系列:

awk '/^alloc/ { a[$4]=$0; }
     /^free/ { delete a[$4]; }
     END { for (i in a) {print a[i]; }' test.txt
$ awk '{print $4}' file | sort | uniq -u
405778
awk'{print$4}'
只打印ID列

排序
对列进行排序

uniq-u
仅显示唯一的ID

编辑:

管道到
grep-f-file
以匹配整行,无需循环:

$ awk '{print $4}' file | sort | uniq -u | grep -f - file
alloc 6a7610   2972   405778 this_alloc_has_no_counterpart.cpp (123)
grep-f
匹配文件中的模式,
-
表示使用
stdin