0

I have 3000 text files in a directory and each .txt file contain single column data. i want to arrange them side by side to make it a mxn matrix file.

For example: paste 1.txt 2.txt 3.txt 4.txt .............3000.txt

For this i tried

printf "%s\n" *.txt | sort -n | xargs -d '\n' paste

However it gives error paste: filename.txt: Too many open files

please suggest a better solution for the same

manas
  • 479
  • 2
  • 14
  • Maybe paste the files in smaller groups, e.g. 1..100, 101..200 etc, then paste the results again. – Bodo Jul 02 '21 at 13:47
  • Don't open the same question more than one time, they will be closed as duplicates. Instead, update the existing question if it's not answered to your satisfaction. – Trenton McKinney Aug 05 '21 at 21:25

1 Answers1

0

Does

files=$(ls file* |sort -n);for eachfile in ${files[@]};do cat $eachfile|tr "\n" "\t";echo;done|rs -c -C -T

solve your problem?

What this solution does is convert your list of files to a sorted array, print the values of each into a tab delimited line, and then convert the rows to columns.

Kyle Banerjee
  • 2,554
  • 4
  • 22
  • 30
  • rs command not found error – manas Jul 03 '21 at 04:39
  • You'll need to install it with your package manager (apt, yum, whatever your distro is) http://manpages.ubuntu.com/manpages/trusty/man1/rs.1.html – Kyle Banerjee Jul 03 '21 at 05:36
  • If you can't get rs, just take the rs part out and redirect the output to a file. Then run that file through this awk solution https://stackoverflow.com/questions/1729824/an-efficient-way-to-transpose-a-file-in-bash – Kyle Banerjee Jul 03 '21 at 06:36