删除第一个选项卡和最后一个分号之间的所有内容

Bio*_*guy 0 perl

我有一个文件,其行如下:

EF457507|S000834932     Root;Bacteria;"Acidobacteria";Acidobacteria_Gp4;Gp4
EF457374|S000834799     Root;Bacteria;"Acidobacteria";Acidobacteria_Gp14;Gp14
AJ133184|S000323093     Root;Bacteria;Cyanobacteria/Chloroplast;Cyanobacteria;Family I;GpI
DQ490004|S000686022     Root;Bacteria;"Armatimonadetes";Armatimonadetes_gp7
AF268998|S000340459     Root;Bacteria;TM7;TM7_genera_incertae_sedis
Run Code Online (Sandbox Code Playgroud)

我想在第一个标签和最后一个分号之间打印任何东西,就像那样

EF457507|S000834932     Gp4
EF457374|S000834799     Gp14
AJ133184|S000323093     GpI
DQ490004|S000686022     Armatimonadetes_gp7
AF268998|S000340459     TM7_genera_incertae_sedis
Run Code Online (Sandbox Code Playgroud)

我试图使用正则表达式,但它不起作用,有没有办法使用Linux,awk或Perl?

Ste*_*eve 5

你可以使用sed:

sed 's/\t.*;/\t/' file

## This matches a tab character '\t'; followed by any character '.' any number of
## times '*'; followed by a semicolon; and; replaces all of this with a tab 
## character '\t'.

sed 's/[^\t]*;//' file

## Things inside square brackets become a character class. For example, '[0-9]' 
## is a character class. Obviously, this would match any digit between zero and
## nine. However, when the first character in the character class is a '^', the
## character class becomes negated. So '[^\t]*;' means match anything not a tab
## character any number of times followed by a semicolon.
Run Code Online (Sandbox Code Playgroud)

或者awk:

awk 'BEGIN { FS=OFS="\t" } { sub(/.*;/,"",$2) }1' file

awk '{ sub(/[^\t]*;/,"") }1' file
Run Code Online (Sandbox Code Playgroud)

结果:

EF457507|S000834932     Gp4
EF457374|S000834799     Gp14
AJ133184|S000323093     GpI
DQ490004|S000686022     Armatimonadetes_gp7
AF268998|S000340459     TM7_genera_incertae_sedis
Run Code Online (Sandbox Code Playgroud)

根据下面的评论,"删除最后一个分号后的所有内容",使用sed:

sed 's/[^;]*$//' file

## '[^;]*$' will match anything not a semicolon any number of times anchored to 
## the end of the line.
Run Code Online (Sandbox Code Playgroud)

或者awk:

awk 'BEGIN { FS=OFS="\t" } { sub(/[^;]*$/,"",$2) }1' file

awk '{ sub(/[^;]*$/,"") }1' file
Run Code Online (Sandbox Code Playgroud)