我的输出类似于以下内容:
[test output] my name is bob this is a random string
[test output] my name is jim this is a randong string2
[test output] my name is bob this is a randong string3
[test output] my name is alice this is a randong string4
[test output] my name is bob this is a randong string5
[test output] my name is dave this is a randong string6
[test output] my name is jim this is a randong string7
[test output] my name is jim this is a randong string8
预期输出:
my name is bob
my name is jim
my name is alice
my name is dave
在名称可以是任何字符串之后,我尝试仅输出上述内容,每个名称一行。它们按什么顺序出现并不重要。
有人可以帮忙吗?
答案1
和awk
:
awk '{ s = $3" "$4" "$5" "$6 };!(s in a){a[s];print s}' <file
答案2
如果您不介意排序输出,那么也可以使用cut
and来完成sort
:
cut -d' ' -f3-6 file | sort -u
-d
atcut
指定分隔符,在您的情况下是空格。-f
atcut
指定要输出的字段。在您的情况下,您需要从 3 到 6 的所有字段。-u
在sort
输出时仅输出相同行的第一行。
答案3
您sed
还可以使用
sed -e 's/^.*] //g' -e 's/ this.*$'//g file | sort -u