我想删除多个.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/