onl*_*lyf 2 sorting bash preserve
我有一个文件,看起来像
1254543534523233434
3453453454323233434
2342342343223233535
0909909092324243535
Run Code Online (Sandbox Code Playgroud)
bash 中是否有一种方法/命令可以根据特定的子字符串删除上面文件中的重复项,而不更改它们在输出中的顺序?
IE
(使用子字符串 -> ${line:11:8}
1254543534523233434
2342342343223233535
0909909092324243535
Run Code Online (Sandbox Code Playgroud)
我知道 :
sort -u : sorts them numerically, then removes duplicates
sort -kx,x -u : The same
cat filein | uniq : requires them to be sorted already or it will not work
Run Code Online (Sandbox Code Playgroud)
我想弄清楚是否有本地 linux 解决方案,而不必为它解析 perl 代码。先感谢您。
您可以使用 awk 而无需任何排序:
awk '!uniq[substr($0, 12, 8)]++' file
1254543534523233434
2342342343223233535
0909909092324243535
Run Code Online (Sandbox Code Playgroud)
1您需要使用substr($0, 12, 8)以从第 12 个位置开始获取所需的 8 个字符长文本。uniq是一个关联数组,带有使用substr函数检索的子字符串。++ 将数组的值设置为 1