我有一個包含 10,000 個分子的檔案。每個分子都以關鍵字$$$$結尾。我想將主檔案拆分為 10,000 個單獨的檔案,以便每個檔案只有 1 個分子。每個分子都有不同的行數。我嘗試sed過test_file.txt:
sed '/$$$$/q' test_file.txt > out.txt
輸入:
$ cat test_file.txt
ashu
vishu
jyoti
$$$$
Jatin
Vishal
Shivani
$$$$
輸出:
$ cat out.txt
ashu
vishu
jyoti
$$$$
我可以遍歷整個主檔案以創建 10,000 個單獨的檔案,但是如何洗掉剛剛從主檔案移動到新檔案的最后一個分子。或者請建議是否有更好的方法,我相信有。謝謝。
Edit1:
$ cat short_library.sdf
untitled.cdx
csChFnd80/09142214492D
31 34 0 0 0 0 0 0 0 0999 V2000
8.4660 6.2927 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
8.4660 4.8927 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
1.2124 2.0951 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
2.4249 2.7951 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
1 2 2 0 0 0 0
2 3 1 0 0 0 0
30 31 1 0 0 0 0
31 26 1 0 0 0 0
M END
> <Mol_ID> (1)
1
> <Formula> (1)
C22H24ClFN4O3
> <URL> (1)
http://www.selleckchem.com/products/Gefitinib.html
$$$$
Dimesna.cdx
csChFnd80/09142214492D
16 13 0 0 0 0 0 0 0 0999 V2000
2.4249 1.4000 0.0000 S 0 0 0 0 0 0 0 0 0 0 0 0
3.6415 2.1024 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
4.8540 1.4024 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
5.4904 1.7512 0.0000 Na 0 3 0 0 0 0 0 0 0 0 0 0
1 2 1 0 0 0 0
2 3 1 0 0 0 0
1 14 2 0 0 0 0
M END
> <Mol_ID> (2)
2
> <Formula> (2)
C4H8Na2O6S4
> <URL> (2)
http://www.selleckchem.com/products/Dimesna.html
$$$$
uj5u.com熱心網友回復:
這是一個簡單的標準解決方案awk:
LANG=C awk '
{ mol = (mol == "" ? $0 : mol "\n" $0) }
/^\$\$\$\$\r?$/ {
outFile = "molecule" fn ".sdf"
print mol > outFile
close(outFile)
mol = ""
}
' input.sdf
uj5u.com熱心網友回復:
如果您csplit來自 GNU coreutils:
csplit -s -z -n5 -fmolecule test_file.txt '/^$$$$$/ 1' '{*}'
uj5u.com熱心網友回復:
這將直接完成整個作業bash:
molsplit.sh
#!/bin/bash
filenum=0
end=1
while read -r line; do
if [[ $end -eq 1 ]]; then
end=0
filenum=$((filenum 1))
exec 3>"molecule${filenum}.sdf"
fi
echo "$line" 1>&3
if [[ "$line" = '$$$$' ]]; then
end=1
exec 3>&-
fi
done
輸入是從標準輸入讀取的,盡管這很容易改變。像這樣的東西:
./molsplit.sh < test_file.txt
附錄
從隨后的評論來看,正在處理的輸入檔案似乎具有 Windows 行結尾,而處理環境的本機行結尾格式是 UNIX 樣式的。在這種情況下,如果要保留行終止樣式,那么我們需要修改分隔符的識別方式。例如,上面的這種變體將識別任何$$$$以分子分隔符開頭的行:
#!/bin/bash
filenum=0
end=1
while read -r line; do
if [[ $end -eq 1 ]]; then
end=0
filenum=$((filenum 1))
exec 3>"molecule${filenum}.sdf"
fi
echo "$line" 1>&3
case $line in
'$$$$'*) end=1; exec 3>&-;;
esac
done
uj5u.com熱心網友回復:
您可以使用rquery( https://github.com/fuyuncat/rquery/releases ) 使用一個命令列來執行此操作。
這個想法是使用一個動態變數在匹配'$$$$'時增加1,然后你可以使用這個動態變數作為輸出檔案名的一部分,它會分割內容。
[ rquery]$ ./rq -v "v1:0:tolong(@v1) switch(@raw,'\$\$\$\$',1,0)" -q "s @raw |>'/tmp/test' @v1 '.txt'" samples/short_library.sdf
[ rquery]$ cat /tmp/test0.txt
untitled.cdx
csChFnd80/09142214492D
31 34 0 0 0 0 0 0 0 0999 V2000
8.4660 6.2927 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
8.4660 4.8927 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
1.2124 2.0951 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
2.4249 2.7951 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
1 2 2 0 0 0 0
2 3 1 0 0 0 0
30 31 1 0 0 0 0
31 26 1 0 0 0 0
M END
> <Mol_ID> (1)
1
> <Formula> (1)
C22H24ClFN4O3
> <URL> (1)
http://www.selleckchem.com/products/Gefitinib.html
$$$$
[ rquery]$ cat /tmp/test1.txt
Dimesna.cdx
csChFnd80/09142214492D
16 13 0 0 0 0 0 0 0 0999 V2000
2.4249 1.4000 0.0000 S 0 0 0 0 0 0 0 0 0 0 0 0
3.6415 2.1024 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
4.8540 1.4024 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
5.4904 1.7512 0.0000 Na 0 3 0 0 0 0 0 0 0 0 0 0
1 2 1 0 0 0 0
2 3 1 0 0 0 0
1 14 2 0 0 0 0
M END
> <Mol_ID> (2)
2
> <Formula> (2)
C4H8Na2O6S4
> <URL> (2)
http://www.selleckchem.com/products/Dimesna.html
$$$$
uj5u.com熱心網友回復:
設定當前輸出檔案名的同一陳述句也會關閉前一個。close(_)^_這里與 相同close(_)^0,它確保檔案名始終為下一個遞增,即使close()操作導致錯誤也是如此。
— 如果輸出檔案命名方案允許前沿零,則將該位更改為,對于任何可能的字串或數字,包括所有形式的零、空字串、 -inities 和s close(_)^(_<_),這總是導致 1 。infnan
mawk2 'BEGIN { getline __<(_ = "/dev/null") ORS = RS = "[$][$][$][$][\r]?"(FS = RS) __*= gsub("[^$\n] ", __, ORS) } NF { print > (_ ="mol" (__ =close(_)^_) ".txt") }' test_file.txt
第一部分既不設定也不修改,但它getline的存在確保第一次呼叫它不會出錯。/dev/null$0 | NFNR | FNRclose(_)
gcat -n mol12345.txt
1 Shivani
2 jyoti
3 Shivani
4 $$$$
它相當快 - 從5.60 MB合成測驗檔案187,710中創建的檔案11.652 secs。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/517588.html
標籤:重击大数据
上一篇:使用bash腳本檢查多個檔案
下一篇:重命名變體列
