【问题标题】:How to grep a line starting with an single quote , alphabet followed by fixed digit only?如何grep以单引号开头的行,字母后跟固定数字?
【发布时间】:2021-06-10 11:40:29
【问题描述】:

我希望 grep 一行以字母开头,后跟 5 个数字。

我的方法是 例如

'A12345'
'A123456'


output should be only line starting with Alphabet followed by 5 numbers 

ie 'A12345'

 My approach : it doesn't work 
 grep -E '[A-Z]''[0-9]{5}'

【问题讨论】:

  • 我想你想要grep -E "^[A-Z][0-9]{5}$
  • @Sundeep 这些数字在单引号内,我怎样才能用它实现相同的 grep

标签: shell grep


【解决方案1】:
# PCRE allows \xHH escapes
$ grep -P '^\x27[A-Z][0-9]{5}\x27' ip.txt
'A12345'

# double quotes can also be used here since there's no clash
$ grep -E "^'[A-Z][0-9]{5}'" ip.txt
'A12345'

# this is same as ^' followed by [A-Z][0-9]{5} and then another '
$ grep -E "^'"'[A-Z][0-9]{5}'"'" ip.txt
'A12345'

这是一个双引号可能有问题的示例。有关详细信息,请参阅 https://mywiki.wooledge.org/QuotesDifference between single and double quotes in Bash

$ echo '1a$(z)b' | grep -E "a$(z)b"
z: command not found

【讨论】:

  • 谢谢 Sandeep,如果我有两个字符串,每个字符串都在一行的单引号内。它没有 grep 任何东西。你能告诉我这个原因可能是什么......例如,如果我有这样的字符串'A12345''A12345'
  • 除非您显示您尝试过的确切命令,否则我无法猜测什么会失败。而且我不知道你可以有什么样的输入变化。最好提出一个新问题并正确使用代码格式,这在 cmets 中很难做到。
  • @yamut - 添加o 标志然后:grep -Eo '[A-Z][0-9]{5}'
  • @Sundeep 我创建了一个新问题,请您看一下。 stackoverflow.com/questions/67926274/…
猜你喜欢
  • 1970-01-01
  • 2022-11-23
  • 1970-01-01
  • 1970-01-01
  • 2017-09-20
  • 1970-01-01
  • 2011-01-26
  • 2023-02-21
  • 2015-07-14
相关资源
最近更新 更多