Bash 删除大文件一列空格的方法

Bash way to remove spaces in one column of huge file

我有一个大的 (15 Gb) 制表符分隔的文本文件(比如 'Test.csv'):

ID1         ID2         ID3                  ID4
Some text   Some text   Text to be cleared   Some text

问题是如何通过bash 脚本(Mac OS X 10.10) 删除列ID3 中的所有空格。因此,结果应该是这样的:

ID1         ID2         ID3                  ID4
Some text   Some text   Texttobecleared   Some text

列分隔符是'\t'。

你应该可以做这样的事情

awk -F"\t" -v OFS="\t" '{gsub(" ", "", ); print}' < file

file 替换为您的文件路径。

-F"\t" 表示在从输入中识别列时使用制表符作为分隔符。

-v OFS="\t" 表示使用制表符分隔输出中的列。

gsub 修改第 3 列 </code> 并将所有空格 <code>" " 替换为空字符串。

使用 bash 的内置命令:

while IFS=$'\t' read id1 id2 id3 id4; do echo -e "$id1\t$id2\t${id3// /}\t$id4"; done < file_original > file_new

输出:

ID1     ID2     ID3     ID4
Some text       Some text       Texttobecleared Some text

sed 方式

sed ':T;s/^\(\([^\t]*\t\)\{2\}\)\([^\t]*\) //;tT' file

或使用 GNU 和 -r

ssed -r ':T;s/^(([^\t]*\t){2})([^\t]*) //;tT' file