简体   繁体   English

如何从字符串“ATATAT”中删除2次出现的字符串“ATAT”。 我只得到一个

[英]How can I grep 2 occurrences of string “ATAT” out of string “ATATAT”. I only get one

I am trying to write a command to grep the number of occurrences in a string, but my string is "ATATAT" and I want to grep "ATAT". 我试图写一个命令来grep字符串中出现的次数,但我的字符串是“ATATAT”,我想grep“ATAT”。 I am expecting to get 2 outputs when I use command I get only 1. 当我使用命令时,我期望得到2个输出我只得到1。

echo "ATATAT" |grep -o "ATAT" 

I have tried surrounding the string with ** but still it only matches one pattern. 我试过用**包围字符串但仍然只匹配一个模式。

The simplest way - make Python do it for you: 最简单的方法 - 让Python为您完成:

python -c "import re; print(re.findall(r'(?=(ATAT))', 'ATATAT'))"
['ATAT', 'ATAT']

The long way with bash: 与bash的漫长道路:

string="ATATAT"
regex="ATAT"
length="${#string}"
counter=0

for((i=0;i<$length;i++)); do
  [[ "${string:$i}" =~ ^$regex ]] && ((counter++))
done

echo "$counter"

Output: 输出:

2

Inspired by the Python answer, here's a solution using ripgrep 受Python回答的启发,这是使用ripgrep的解决方案

$ echo 'ATATAT' | rg -oP '(?=(ATAT))' -r '$1'
ATAT
ATAT
$ echo 'ATATXAT' | rg -oP '(?=(ATAT))' -r '$1'
ATAT
$ echo 'ATATATATAT' | rg -oP '(?=(ATAT))' -r '$1'
ATAT
ATAT
ATAT
ATAT

(?=(ATAT)) is a positive lookahead (see also What does this regex mean? ), it will check a condition without consuming characters and thus possible to do overlapping matches. (?=(ATAT))是一个积极的先行(参见这个正则表达式是什么意思? ),它将检查一个条件而不消耗字符,从而可以进行重叠匹配。 -r option allows to replace the matching portion with something else. -r选项允许用其他东西替换匹配的部分。

Or, use perl 或者,使用perl

$ # the if condition is there to prevent empty lines for non-matching input lines
$ echo 'ATATATATAT' | perl -lne 'print join "\n", //g if /(?=(ATAT))/'
ATAT
ATAT
ATAT
ATAT


If you just need the count: 如果您只需要计数:

$ echo 'ATATATATAT' | rg -coP '(?=(ATAT))'
4
$ # with GNU grep, if PCRE is available
$ echo 'ATATATATAT' | grep -oP 'AT(?=(AT))' | wc -l
4

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

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