删除第二个“_”之后的所有内容,但保留其他列

Ped*_*ell 2 bash awk sed

我正在尝试格式化fam 文件中的系列 ID ,该文件的示例和系列 ID 相同,并按以下方式编码:

Continent_Breed_Ind-ID

这个想法是将第一列转换为仅包含大陆+品种的内容,但保留其他列。

模拟数据集:

Continent1_Breed1_Ind-ID1 Continent1_Breed1_Ind-ID1 0 0 0 -9
Continent1_Breed2_Ind-ID2 Continent1_Breed2_Ind-ID1 0 0 0 -0
Continent2_Breed1_Ind-ID1 Continent2_Breed1_Ind-ID1 0 0 0 -9
Run Code Online (Sandbox Code Playgroud)

期望的结果:

Continent1_Breed1 Continent1_Breed1_Ind-ID1 0 0 0 -9
Continent1_Breed2 Continent1_Breed2_Ind-ID1 0 0 0 -0
Continent2_Breed1 Continent2_Breed1_Ind-ID1 0 0 0 -9
Run Code Online (Sandbox Code Playgroud)

我尝试使用 sed 如下:

sed -r 's/_[^_]*//2g' file.fam
Run Code Online (Sandbox Code Playgroud)

但这只给了我第一列。

有任何想法吗?

anu*_*ava 5

您可以使用这个简单的sed命令:

sed 's/_[^_]* / /' file

Continent1_Breed1 Continent1_Breed1_Ind-ID1 0 0 0 -9
Continent1_Breed2 Continent1_Breed2_Ind-ID1 0 0 0 -0
Continent2_Breed1 Continent2_Breed1_Ind-ID1 0 0 0 -9
Run Code Online (Sandbox Code Playgroud)

在线代码演示

这里:

  • _[^_]* :匹配_后跟 0 个或多个非_字符,后跟空格
  • 我们用空格替换这个匹配,以恢复第一列和第二列之间的空格

PS:请注意,这里没有使用全局标志。