om-*_*-ha 3 grep bash text-processing regular-expression
是否可以打印不唯一且跨多行的重复单词?不仅仅是单行中的独特单词。
这个问题解决了在同一行中查找重复单词的问题。它还存在一个问题,即将结束词边界与起始词边界相匹配。
[
{
entity:
{
id: int
employee:
{
id: int
company: {
name: string
area:
{
country: string
city: string
zipcode: string
}
}
person:
{
id: int
firstName: string
middleName: string
lastName: string
}
}
}
entity:
{
id: int
person:
{
id: int
firstName: string
middleName: string
lastName: string
}
area:
{
country: string
city: string
zipcode: string
}
}
}
]
Run Code Online (Sandbox Code Playgroud)
area
city
country
entity
firstName
id
int
lastName
middleName
person
string
zipcode
Run Code Online (Sandbox Code Playgroud)
company
employee
name
Run Code Online (Sandbox Code Playgroud)
grep -wo "[[:alnum:]]\+" input_file.txt | sort | uniq [-c | -d | -u]
Run Code Online (Sandbox Code Playgroud)
egrep -wo "[[:alnum:]]+" input_file.txt | sort | uniq [-c | -d | -u]
Run Code Online (Sandbox Code Playgroud)
首先,您可以使用 标记单词grep -wo,每个单词都打印在单行上。
然后您可以使用 对标记化的单词进行排序sort。
最后可以用 找到连续的唯一或重复的单词uniq。
3.1. uniq -c这将打印单词及其计数。涵盖所有匹配的单词——重复的和唯一的。
3.2. uniq -d这会打印所有重复的单词。
3.3. uniq -u这会打印所有独特的单词。
abc line 1
xyz zzz
123 456
abc end line
Run Code Online (Sandbox Code Playgroud)
grep -wo "[[:alnum:]]\+" input_file.txt | sort | uniq [-c | -d | -u]
Run Code Online (Sandbox Code Playgroud)
输出:
1 1
1 123
1 456
2 abc
1 end
2 line
1 xyz
1 zzz
Run Code Online (Sandbox Code Playgroud)
egrep -wo "[[:alnum:]]+" input_file.txt | sort | uniq [-c | -d | -u]
Run Code Online (Sandbox Code Playgroud)
输出:
abc
line
Run Code Online (Sandbox Code Playgroud)
abc line 1
xyz zzz
123 456
abc end line
Run Code Online (Sandbox Code Playgroud)
输出
1
123
456
end
xyz
zzz
Run Code Online (Sandbox Code Playgroud)
grep参数
grep正则表达式
[[:alnum:]]字母数字字符\+克莱恩加角色。匹配一次或多次出现。sortuniq
-c打印单词及其重复计数。-d仅打印重复的行。-u仅打印非重复(唯一)行。