如何删除shell中最后一列中的空格

时间:2017-04-28 17:47:12

标签: bash shell

我有一个包含此字符串的文件

select chrom,chromStart,chromEnd,name from snp147 where name="rs12414460      ";
select chrom,chromStart,chromEnd,name from snp147 where name="rs12456              ";
select chrom,chromStart,chromEnd,name from snp147 where name="rs12434212334               ";

我想删除name字段中的空格。

我怎样才能获得这个:

select chrom,chromStart,chromEnd,name from snp147 where name="rs12414460";
select chrom,chromStart,chromEnd,name from snp147 where name="rs12456";
select chrom,chromStart,chromEnd,name from snp147 where name="rs1243421111111";

请帮帮我

我尝试过:cut test.txt | cut -d ' ' -f1,2,3,4,5,6但它不起作用

1 个答案:

答案 0 :(得分:2)

sed这比bash更多。

sed 's/ *";/";/' test.txt

或(作为本文档的演示):

sed 's/ *";/";/'  <<EOF
> select chrom,chromStart,chromEnd,name from snp147 where name="rs12414460      ";
> select chrom,chromStart,chromEnd,name from snp147 where name="rs12456              ";
> select chrom,chromStart,chromEnd,name from snp147 where name="rs12434212334               ";
> EOF
select chrom,chromStart,chromEnd,name from snp147 where name="rs12414460";
select chrom,chromStart,chromEnd,name from snp147 where name="rs12456";
select chrom,chromStart,chromEnd,name from snp147 where name="rs12434212334";