gpt4 book ai didi

for-loop - 我可以在其中使用 awk "for loop"执行 '!seen[$0]' 以应用于多个 .txt 文件吗?

转载 作者:行者123 更新时间:2023-12-04 10:00:13 24 4
gpt4 key购买 nike

我想删除多个 .text 文件中的一些重复行
我需要独立分析每个文件,因为它们彼此不相关,使用:

awk '!seen[$0]' file.txt file.out

但是当我尝试:
for f in *.txt; do awk 'seen![$0]' $f $f.out; done

我收到一个错误:
无法打开文件“$f”。
awk 或 '!看到 [$ 0]' 无法识别
有时我会得到一个输出文件,但它是同一个文件......

最佳答案

非 GNU awk你能试试下面的吗?

awk -v temp_out="file.out" '
FNR==1{
if(prev_filename){
close(temp_out)
sub(/\.txt/,".out",prev_filename)
system("mv -- \047" prev_out "\047 \047" prev_filename "\047")
}
prev_filename=FILENAME
delete seen
}
!seen[$0]++{
print > (temp_out)
}
END{
if(prev_filename){
close(temp_out)
sub(/\.txt/,".out",prev_filename)
system("mv -- \047" prev_out "\047 \047" prev_filename "\047")
}
}
' *.txt

说明:为上述代码添加说明。
awk -v temp_out="file.out" '                        ##Starting awk program from here with setting variable prev_out to file.out here.
FNR==1{ ##Checking condition if line is first line then do following.
if(prev_filename){ ##Checking if prev_filename is NOT NULL then do following.
close(temp_out) ##Closing prev_out file here from back-end.
sub(/\.txt/,".out",prev_filename) ##Substitute .txt with .out in previous filename here.
system("mv -- \047" prev_out "\047 \047" prev_filename "\047") ##Using system command to rename temp file prev_out with prev_filename(with .out)
}
prev_filename=FILENAME ##Setting prev_filename to current FILENAME here.
delete seen ##Deleting array seen here.
}
!seen[$0]++{ ##Checking if current line is NOT present in array seen then do following.
print > (temp_out) ##Printing current line to temp file here.
}
END{ ##Starting END block of this program from here.
if(prev_filename){ ##Checking if prev_filename is NOT NULL then do following.
close(temp_out) ##Closing prev_out file here from back-end.
sub(/\.txt/,".out",prev_filename) ##Substitute .txt with .out in previous filename here.
system("mv -- \047" prev_out "\047 \047" prev_filename "\047") ##Using system command to rename temp file prev_out with prev_filename(with .out)
}
}
' *.txt ##Mentioning all .txt files here.

关于for-loop - 我可以在其中使用 awk "for loop"执行 '!seen[$0]' 以应用于多个 .txt 文件吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61847258/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com