【发布时间】:2020-03-30 23:54:42
【问题描述】:
我确实有生成的 CSV 文件,其中包含重复值。我想使用 AWK 或 Sed 命令删除/删除重复值。
实际输出
10.135.83.48,9042
10.135.83.46,9042
10.135.83.44,9042
10.5.197.25,10334
10.39.8.166,1500
10.135.83.48,9042
10.135.83.46,9042
10.135.83.44,9042
https://t-mobile.com,443
https://t-mobile.com,443
http://localhost:5059/abc/token,80
预期输出
10.135.83.48,9042
10.135.83.46,9042
10.135.83.44,9042
10.5.197.25,10334
10.39.8.166,1500
https://t-mobile.com,443
http://localhost:5059/abc/token,80
从几个属性文件中我得到了这个输出。下面是我正在尝试的脚本
#!/bin/bash
for file in $(ls);
do
#echo " --$file -- ";
grep -P '((?<=[^0-9.]|^)[1-9][0-9]{0,2}(\.([0-9]{0,3})){3}(?=[^0-9.]|$)|(http|ftp|https|ftps|sftp)://([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:/+#-]*[\w@?^=%&/+#-])?|\.port|\.host|contact-points|\.uri|\.endpoint)' $file|grep '^[^#]' |awk '{split($0,a,"#"); print a[1]}'|awk '{split($0,a,"="); print a[1],a[2]}'|sed 's/^\|#/,/g'|awk '/http:\/\// {print $2,80}
/https:\/\// {print $2,443}
/Points/ {print $2,"9042"}
/host/ {h=$2}
/port/ {print h,$2; h=""}'|awk -F'[, ]' '{for(i=1;i<NF;i++){print $i,$NF}}'|awk 'BEGIN{OFS=","} {$1=$1} 1'|sed '/^[0-9]*$/d'|awk -F, '$1 != $2'
done |awk '!a[$0]++'
#echo "Done."
stty echo
cd ..
awk '!a[$0]++' --> 这是我试图与上述脚本结合的命令。单独此命令正在工作。但是当我尝试与上述脚本结合时,它没有按预期工作。
提前感谢您的帮助。
【问题讨论】:
-
这应该在循环内
-
我很想知道实际输入文件的格式......在我看来,有很多 grepping & awking 正在进行......
-
如果顺序无关紧要,只需
sort -u工作,顺便说一句。 -
for file in $(ls);不要那样做。使用for file in *;更多信息请参见mywiki.wooledge.org/ParsingLs。