我有一个小问题,非常感谢你的帮助。
我需要使用粘贴命令将不同的文本文件合并为:
paste -d, ~/Desktop/*.txt > ~/Desktop/Out/merge.txt
但是,文件无序合并。 (文本文件编号为1,2,3等)
我正在使用*.txt
,因为不同的场景存在不同数量的文件。
请你介意帮帮我。
答案 0 :(得分:4)
如果你使用现代bash,你可以写:
paste -d, ~/Desktop/{1..10}.txt > ~/Desktop/Out/merge.txt
如果没有,您必须使用以下内容:
paste -d, $(seq 1 10 | sed 's@.*@~/Desktop/&.txt) > ~/Desktop/Out/merge.txt
如果您不知道目录中包含哪些文件, 你可以列出并对它们进行排序:
cd ~/Desktop/
paste -d, $(ls -1d *.txt| sort -n) > ~/Desktop/Out/merge.txt
示例:
$ touch {1..20}.txt
$ echo $(ls -1 | sort -n)
1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt 9.txt 10.txt 11.txt 12.txt 13.txt 14.txt 15.txt 16.txt 17.txt 18.txt 19.txt 20.txt
例2:
$ echo hello > 1.txt
$ echo dear > 5.txt
$ echo friend > 11.txt
$ paste -d, $(ls -1d *.txt| sort -n)
hello,dear,friend
答案 1 :(得分:0)
paste -d, $(ls ~/Desktop/*.txt) > ~/Desktop/Out/merge.txt
*正在被按字母顺序排列的目录文件名列表替换。
3.5.8 Filename Expansion
Bash会扫描每个单词中的字符'*','?'和'['。如果出现其中一个字符,则该单词将被视为模式,并替换为与该模式匹配的按字母顺序排列的文件名列表。
所以filenaming不必是连续的;)
答案 2 :(得分:0)
这是一个相当长的方式,但在一行中做同样的事情。
paste -d, $(ls ~/Desktop/*.txt | awk -F/ '{print $NF"/"$0}' | sort -n | cut -d/ -f2-) > ~/Desktop/merge.txt
我喜欢一个衬垫: - )