如何使用sed在第三个下划线和逗号之间删除?

时间:2019-11-12 17:28:58

标签: regex sed

我正在尝试从第三个下划线和第一个逗号之间的csv的每一行中删除文本。

我已经能够删除第三个下划线之后的所有内容,并且我尝试通过在下划线和逗号之间的模式范围内将这种删除括起来来限制删除,但由于我似乎不起作用希望。

我尝试了以下操作:

sed '/_/,/,/ s/^\([^_]*\(_[^_]*\)\{2\}\).*/\1/'<Initial.csv >Final.csv

A_G_F_Triple 1,0.001079516,0.000763178,0.001008407,0.001695147
A_G_D_cluster 2,0.002292753,0.001215668,0.001070863,0.001350103
A_P_Z_Home 3,0.001066035,0.002260941,0.002276146,0.002331612

我希望得到以下输出:

A_G_F,0.001079516,0.000763178,0.001008407,0.001695147
A_G_D,0.002292753,0.001215668,0.001070863,0.001350103
A_P_Z,0.001066035,0.002260941,0.002276146,0.002331612

但是实际输出是:

A_G_F
A_G_D
A_P_Z

1 个答案:

答案 0 :(得分:0)

您可以使用

sed 's/^\([^_]*_[^_]*_[^_]*\)_[^,]*/\1/'

详细信息

  • ^-字符串的开头
  • \([^_]*_[^_]*_[^_]*\)-组1:除__之外的0+个字符,__以外的0+个字符和其他0+个字符比_
  • _-下划线
  • [^,]*-除,之外的0个或更多字符

查看online sed demo

s="A_G_F_Triple 1,0.001079516,0.000763178,0.001008407,0.001695147
A_G_D_cluster 2,0.002292753,0.001215668,0.001070863,0.001350103
A_P_Z_Home 3,0.001066035,0.002260941,0.002276146,0.002331612"
sed 's/^\([^_]*_[^_]*_[^_]*\)_[^,]*/\1/' <<< "$s"