regex - 狂欢 : regular expressions within backticks

标签 regex linux awk backticks pcregrep

我有一个名为“align_summary.txt”的文件,如下所示:

Left reads:


Input     :  26410324

   Mapped   :  21366875 (80.9% of input)

   of these:    451504 ( 2.1%) have multiple alignments (4372 have >20)

...more text....

... and several more lines of text....

我想在 bash shell 中提取所有左对齐读取(在本例中为 2.1)中多重对齐的百分比。

如果我使用这个:

 pcregrep -M "Left reads.\n..+.\n.\s+Mapped.+.\n.\s+of these" align_summary.txt | awk -F"\\\( " '{print $2}' | awk -F"%" '{print $1}' | sed -n 4p

它立即给我输出:2.1

但是,如果我像这样用反引号括起相同的表达式:

leftmultiple=`pcregrep -M "Left reads.\n..+.\n.\s+Mapped.+.\n.\s+of these" align_summary.txt | awk -F"\\\( " '{print $2}' | awk -F"%" '{print $1}' | sed -n 4p`

我收到一个错误:

awk: syntax error in regular expression (  at 
  input record number 1, file 
  source line number 1

据我了解,将此表达式括在反引号中会影响对包含“(”符号的正则表达式的解释,尽管它已被反斜杠转义。

为什么会发生这种情况以及如何避免这种错误?

如有任何意见和建议,我将不胜感激。

非常感谢,

最佳答案

只需使用awk:

leftmultiple=$(awk '/these:.*multiple/{sub(" ","",$2);print $2}' FS='[(%]' align_summary.txt )

关于regex - 狂欢 : regular expressions within backticks,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38664253/

相关文章:

linux - bash : Replace entire line

c - 信号量只在最后工作

linux - 使用sed更改文件中的字符串?

awk - 打印行号(如 NR),但为 $3 中的每个新字段变量重新编号

Javascript 货币正则表达式

java - 有什么方法可以使用带有两对不同分隔符的正则表达式吗?

c# - 使用正则表达式从 SQL 查询中提取表和列

regex - 使用正则表达式确定字符串是否全部大写

linux - cmake错误设置编译器

linux - 根据模式合并两个文件