从第二列的开头删除空格

时间:2016-08-27 17:43:07

标签: bash awk sed cut

我有一个空格分隔的文件,如下所示:

12  12.57428314.57490104 ENSG00000065361 rs2271194 rs61939899
2  2.198148577.198835577 ENSG00000065413 rs4524134 rs2697288 rs6738721
6  6.84279922.84407274 ENSG00000065609 rs2016358 rs35791305
10  10.104585135.104956335 ENSG00000065613 rs72811696

我想从第二列中删除前导空格(有两个空格分隔第1列和第2列而不是一个空格)。有没有人知道sed或awk命令?

5 个答案:

答案 0 :(得分:2)

随着削减:

cut -d " " -f 1,3- file

输出:

12 12.57428314.57490104 ENSG00000065361 rs2271194 rs61939899
2 2.198148577.198835577 ENSG00000065413 rs4524134 rs2697288 rs6738721
6 6.84279922.84407274 ENSG00000065609 rs2016358 rs35791305
10 10.104585135.104956335 ENSG00000065613 rs72811696

答案 1 :(得分:1)

tr -s(或tr --squeeze-repeats)将删除重复的字符。所以如果你想要替换所有重复的空格,你可以写:

tr -s ' '   < input-file   > output-file

输入:

12  12.57428314.57490104 ENSG00000065361 rs2271194 rs61939899
2  2.198148577.198835577 ENSG00000065413 rs4524134 rs2697288 rs6738721
6  6.84279922.84407274 ENSG00000065609 rs2016358 rs35791305
10  10.104585135.104956335 ENSG00000065613 rs72811696

输出:

12.57428314.57490104 ENSG00000065361 rs2271194 rs61939899
2 2.198148577.198835577 ENSG00000065413 rs4524134 rs2697288 rs6738721
6 6.84279922.84407274 ENSG00000065609 rs2016358 rs35791305
10 10.104585135.104956335 ENSG00000065613 rs72811696

答案 2 :(得分:1)

使用GNU sed,在第一列之后用一个空格替换多个空白字符

sed -E 's/^(\S+)\s+/\1 /' ip.txt

对于其他版本,请使用

    {li> [[:space:]] \s {li> [^[:space:]] \S

:blank:(空格和制表符)代替:space:(空白字符)

答案 3 :(得分:1)

此AWK用单个空格替换所有连续空格的出现:

$ awk 'gsub(/ +/," ")' file 
12 12.57428314.57490104 ENSG00000065361 rs2271194 rs61939899
2 2.198148577.198835577 ENSG00000065413 rs4524134 rs2697288 rs6738721
6 6.84279922.84407274 ENSG00000065609 rs2016358 rs35791305
10 10.104585135.104956335 ENSG00000065613 rs72811696

答案 4 :(得分:1)

只需删除每行的第一个空格:

$ sed 's/ //' file
12 12.57428314.57490104 ENSG00000065361 rs2271194 rs61939899
2 2.198148577.198835577 ENSG00000065413 rs4524134 rs2697288 rs6738721
6 6.84279922.84407274 ENSG00000065609 rs2016358 rs35791305
10 10.104585135.104956335 ENSG00000065613 rs72811696