Paste the Linux script… here is a solution to the problem.
Paste the Linux script
I have a small question, thank you very much for your help.
I need to merge different text files together using the paste command :
paste -d, ~/Desktop/*.txt > ~/Desktop/Out/merge.txt
However, the files were merged out of order. (Text file numbers 1, 2, 3, etc.).
I’m using *.txt
because different scenes have different numbers of files.
Would you mind helping me?
Solution
If you use modern bash, you can write like this:
paste -d, ~/Desktop/{1..10}.txt > ~/Desktop/Out/merge.txt
If not, you have to use something like :
paste -d, $(seq 1 10 | sed 's@.*@~/Desktop/&.txt) > ~/Desktop/Out/merge.txt
If you don’t know what files are in your directory,
You can list them and sort them:
cd ~/Desktop/
paste -d, $(ls -1d *.txt| sort -n) > ~/Desktop/Out/merge.txt
Example:
$ 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
Example 2:
$ echo hello > 1.txt
$ echo dear > 5.txt
$ echo friend > 11.txt
$ paste -d, $(ls -1d *.txt| sort -n)
hello,dear,friend