Text-Processing
將大文件拆分為具有唯一文件名的新文件
我需要將文件拆分為唯一的文件名。
例如,我可以使用
sed
命令 來完成它,sed -n '/scaffold135_/w 135-scaf.txt' input file.txt
但這很耗時,所以我需要一種聰明的方法來更快地完成它。下面是一個輸入樣本(原始文件有一百萬行):scaffold1_115,T,N,N,N,N,A,N,N,N,N,N,N,T,N,T,T,N,A,A,N,N,A scaffold1_123,A,N,N,N,N,G,N,N,N,N,N,N,A,N,A,A,N,G,G,N,N,G scaffold1_140,C,N,N,N,N,C,N,N,N,N,N,N,C,N,C,C,N,T,C,N,N,C scaffold2_161,G,N,N,N,N,G,N,C,N,N,C,N,G,N,G,G,N,G,G,C,N,G scaffold2_162,C,N,N,N,N,C,N,T,N,N,T,N,C,N,C,C,N,C,C,T,N,C scaffold2_180,C,N,N,N,N,C,N,T,N,N,C,C,C,T,C,C,T,C,C,C,N,C scaffold2_194,C,N,N,C,N,C,C,C,C,C,C,C,C,C,T,C,C,C,C,C,N,C scaffold3_195,G,N,N,G,G,C,G,G,G,G,G,G,C,G,C,G,G,C,C,G,N,C scaffold3_234,T,N,A,T,A,A,T,T,T,A,T,A,A,T,A,A,T,A,A,T,N,A scaffold101_282,C,T,T,T,C,C,T,C,T,C,C,C,C,T,C,C,T,C,C,C,N,C scaffold101_371,T,T,T,T,T,C,T,T,T,T,T,T,T,T,T,T,T,T,T,T,N,C scaffold101_372,T,T,T,T,C,C,T,T,T,T,T,T,T,T,T,T,T,T,T,T,N,C
線條很獨特。我希望將每個特定的行
scafold
放入一個單獨的文件中,例如所有以開頭的行scaffold1_
到一個名為的文件中scaffold1.txt
,依此類推,直到scaffold10156.txt
包含以開頭的行scaffold10156_
您應該能夠使用重定向
awk
awk -F'_' '{print > $1".txt"}' file
如果共享
scaffoldn_
前綴的行是連續的,您可以執行以下操作以避免違反打開文件句柄限制awk -F'_' 'NR == 1 || $1 != prev{if (f) close(f);f=$1".txt"; prev=$1}; {print > f};END{if (f) close(f)}' file
對於GNU sed:
sed 's/scaffold\([0-9]*\)_.*/echo "&" >> "\1-scaf.txt"/e' infile