简体   繁体   English

在外壳中使用特定分隔符前的最后一个单词对文本进行排序

[英]Sort text in shell using last word before specific delimiter

How do I sort a text file alphabetically in shell using the last word before a certain delimiter as key? 如何在外壳中按一定定界符之前的最后一个字母作为关键字按字母顺序对文本文件进行排序?

Eg the following file: 例如以下文件:

Manfred1 Mustermann1 (Berlin)
Manfred Siegfried Müller (Aachen)
Gerd A. Meier (Stuttgart)

In this case the delimiter would be " (" and I would like to sort using the last name of the persons in the list. The number of words before the last name is varying. 在这种情况下,定界符将为“(”,我想使用列表中人员的姓氏进行排序。姓氏前的单词数是变化的。

Given a line of text I know how to extract the key: 给定一行文本,我知道如何提取密钥:

s="Manfred1 Mustermann1 (Berlin)"
w=${s%% (*}
l=${w##* }
echo $l

This displays "Mustermann1", but I did not get any further. 这将显示“ Mustermann1”,但是我没有得到任何进一步的信息。

EDIT Using l0b0's answer, this is the complete code that did it: 编辑使用l0b0的答案,这是完成此操作的完整代码:

while read s; do
w=${s%% (*}
l=${w##* }
printf "%s\t%s\n" "$l" "$s"
done <names.txt | sort -k 1 | cut  -f 2 > names_sorted.txt

where names.txt contains the text and the sorted list will be printed to names_sorted.txt 其中names.txt包含文本,并且已排序的列表将被打印到names_sorted.txt

Once you've got the sort key ( $l ) you can proceed like this: 获得排序键( $l )后,您可以像这样继续:

  1. Print the sort key followed by a tab character followed by the original line, as in printf %s\\t%s\\n . 如在printf %s\\t%s\\n一样,打印排序键,然后是制表符,然后是原始行。
  2. Use sort 's --key option to sort by the first column (tab should be the default key separator). 使用sort--key选项按第一列进行排序(选项卡应为默认的键分隔符)。
  3. Remove the sort key and tab character by using awk or grep . 使用awkgrep删除排序键和制表符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM