Ste*_*fan 2 shell-script text-processing sort uniq join
我有一个制表符分隔的文件,如下所示:
123 some text
123 some different text
334 some other text
341 more text
Run Code Online (Sandbox Code Playgroud)
我想做两件事。一种是按数字顺序排列所有内容(这很容易做到),另一种是删除一行,如果它的数字已经存在。即输出将如下所示:
123 some text
334 some other text
341 more text
Run Code Online (Sandbox Code Playgroud)
我尝试获取仅包含唯一数字的文件,即
123
334
341
Run Code Online (Sandbox Code Playgroud)
并将其与原始文件连接:
join -j 1 justNumbers.txt original.txt
Run Code Online (Sandbox Code Playgroud)
但这给了我原始文件。有任何想法吗?
如果您想对第一个字段的唯一性进行排序/测试,并且您的系统具有 GNUcoreutils版本sort,那么我认为您可以使用
sort -nu file
Run Code Online (Sandbox Code Playgroud)
即。
$ sort -nu file
123 some text
334 some other text
341 more text
Run Code Online (Sandbox Code Playgroud)
从 info coreutils 'sort invocation'
命令
sort -u和sort | uniq是等效的,但这种等效性不扩展到任意sort选项。 例如,在sort -n -u检查 uniqueness 时仅检查初始数字字符串的值,而sort -n | uniq检查整行。