gpt4 book ai didi

regex - 如何在awk中打​​印没有字段分隔符的行?

转载 作者:行者123 更新时间:2023-12-04 14:37:09 24 4
gpt4 key购买 nike

我有这样的数据(文件名为 list-in.dat)

a ; b ; c ; i
d
e ; f ; a ; b
g ; h ; i
我想要一个这样的列表(输出文件 list-out.dat ),其中包含所有项目,按字母顺序(不区分大小写)和每个唯一项目仅一次。
a
b
c
d
e
f
g
h
i
我的尝试是:
awk -F " ; " ' BEGIN { OFS="\n" ; } {for(i=0; i<=NF; i++) print $i} ' file-in.dat | uniq | sort -uf > file-out.dat
但我最终得到了所有的 Ant ,除了那些只有一个项目的行:
a
b
c
e
f
g
h
i

How can I get all (unique, sorted) items no matter how many items are in one line / if the field separator is missing?

最佳答案

使用 gnu-awk :

awk -F '[[:blank:]]*;[[:blank:]]*' '{
for (i=1; i<=NF; i++) uniq[$i]
}
END {
PROCINFO["sorted_in"]="@ind_str_asc"
for (i in uniq)
print i
}' file
a
b
c
d
e
f
g
h
i

对于 non-gnu awk用:
awk -F '[[:blank:]]*;[[:blank:]]*' '{for (i=1; i<=NF; i++) uniq[$i]} 
END{for (i in uniq) print i}' file | sort

关于regex - 如何在awk中打​​印没有字段分隔符的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63446510/

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