【问题标题】:using grep -E to find peoples last names that begin with S but not include them is their first names begin with S使用 grep -E 查找以 S 开头但不包括他们的人们的姓氏是他们的名字以 S 开头
【发布时间】:2018-08-02 06:02:59
【问题描述】:

在 people.txt 文件中查找姓氏以 S 开头的人(您的表达式应该 匹配整个姓氏,而不仅仅是首字母 S,并且只有大写 S 符合条件)。确保这件事 不包括以 S 开头的名字。

people.txt 包括以下内容:

Cardoza, Fred
Catto, Philipa
Duncan, Jean
Edwards, Neil
England, Elizabeth
Main, Robert
Martin, Jane
Meens, Carol
Patrick, Harry
Paul, Jeanine
Roberts, Clementine
Schmidt, Paul
Sells, Simon
Smith, Peter
Stephens, Sheila
Wales, Gareth
Zinni, Hamish

答案应该是:

Schmidt, Paul
Smith, Peter

我目前的回答包括:

Schmidt, Paul
Sells, Simon
Smith, Peter
Stephens, Sheila

请帮帮我

【问题讨论】:

  • 原来我的代码是 grep -E "[^
  • 我随后对其进行了编辑,使其不包括名字以 s 开头的人,即 "[\
  • grep -E "[^<S]" 将查找包含不是"S""<" 的任何字符的行(这有点奇怪)。你的第二个说它必须有"<""S",然后是任何one小写字母,然后是逗号,然后可能是空格,然后是"^""<""S"。你能解释一下你的意图吗?此外,它们似乎与您声称获得的输出不对应。 CWLiu 已经为您提供了解决方案,但请务必查看正则表达式以了解他的解决方案的含义。

标签: grep data-management


【解决方案1】:
grep -E "^S[^,]*, [^S]" People.txt

Regex ^S[^,]*, [^S] 将匹配逗号后面以“S”和“non-S”开头的字符串。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-26
    • 2019-12-25
    • 1970-01-01
    • 1970-01-01
    • 2015-02-08
    • 1970-01-01
    相关资源
    最近更新 更多