Add csv files one after another in numerical order using bash shell script

The specified files with the name 1.csv, 2.csv, 3.csv, ... 89.csv ... n.csv how to add them together in numerical order) (from 1 to n) in the bash shell script? Is there a single-line solution to the solution?

+4
source share
2 answers

If your files are named with leading zeros, it would be easier, i.e.

cat [0-9].csv [0-9][0-9].csv .... > new.csv 

But its not too difficult to get a true serial number, given

 ls -1 1 10 11 12 13 2 20 21 3 7 8 9 

(in both examples, note that the ls option is number one, (1), not the letter L (l))

and

 ls -1 [0-9]* | sort -n 1 2 3 7 8 9 10 11 12 13 20 21 

THEN

  cat $( ls -1 *.csv | sort -n ) > new.csv 

Suppose all your csv files are numbered.

If you have more than 1000 files, the processing of file files in the shell may break, and you should post a new question for the correct use of xargs.

Add shell debug / shell trace to see what happens

  set -vx # to turn on set +vx # to turn it off 

.

IHTH.

+2
source

One insert with built-in parameters (you must replace N with the maximum file number). Script will skip unused files, so if you skip several files in a sequence, you will not get an error :)

 for i in `seq 0 N`; do test -f $i.csv && cat $i.csv; done >> result.csv 

Or Script with parameters

 #!/bin/bash for i in `seq 0 $1` do if [ -f "$i.csv" ] then cat $i.csv >> $2 fi done 

Usage: ./ script.sh MAX_CSV_ID RESULT_FILE

Example: ./ script.sh 89 new.csv

0
source

Source: https://habr.com/ru/post/1445154/


All Articles