The problem:
Find pieces of text in a file enclosed by @
Input:
@abc@ abc @ABC@
cba @cba@ CBA
Output:
@abc@ @ABC@
@cba@
I've tried the following:
cat test.txt | perl -ne 'BEGIN { $/ = undef; } print $1 if(/(@.*@)/s)."\n"'
But this results in:
@abc@ abc @ABC@
cba @cba@
Additional: I was not complete. The goal of the above is the replace the characters between the @ with something else: a should become chr(0x430) b should become chr(0x431) c should become chr(0x446) A should become chr(0x410) B should become chr(0x411) C should become chr(0x426) so with the above input in mind it should result in: абц abc АБЦ
cba цба CBA
Sorry for my imcompleteness. Thanks Kluther
The problem with (@.*@)
is that *
is greedy: it matches the largest amount possible. Thus it will match everything between the first @
in the string and the last one.
You can make it non-greedy with (@.*?@)
. However, a better approach is to match everything that is not @
in between:
(@[^@]*@)
If you want to match every occurrence instead of the first one, you also need to use the /g
modifier and modify your code to use a loop:
perl -ne 'BEGIN { $/ = undef; } print $1 while(/(\@[^@]*\@)/gs); print "\n"'
使用这样的模式
@[a-zA-Z]+@
使用此正则表达式:
cat test.txt | perl -pe 's/(?:(@ )|^[^@]).*?(?: (@)|$)/$1$2/g'
Use non-greedy search .+?
or /(\\@([^@]*)\\@)/gsm
.
cat test.txt | perl -ne 'BEGIN { $/ = undef; } print $1." " while(/(\@([^@]*)\@)/gsm); print "\n";'
One way:
$ perl -pe '@a=$_=~/@[^@]+@/g; $_="@a";' file
@abc@ @ABC@ @cba@
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.