4

Given files named 1.csv, 2.csv, 3.csv, ...89.csv... n.csv how do I append them together in numeric order)(1 to n) in bash shell script? is there a one-liner for the solution?

2 Answers 2

2

If your files where 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 hard to get a true numeric order, given

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

(in both samples, note that the option to ls is the 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

Assuming all your csv files are numbered.

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

To see what is happening add shell debugging/trace use

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

.

IHTH.

Sign up to request clarification or add additional context in comments.

2 Comments

ls -l *.csv|sort -n seems to be out of order: ls -l *.csv |sort -n -rw-r--r-- 1 root root 110427099 Sep 4 20:37 94.csv -rw-r--r-- 1 root root 110439250 Sep 4 20:15 93.csv -rw-r--r-- 1 root root 111962812 Sep 4 19:53 92.csv -rw-r--r-- 1 root root 114716696 Sep 4 14:35 65.csv -rw-r--r-- 1 root root 115487626 Sep 4 08:05 35.csv -rw-r--r-- 1 root root 115777573 Sep 3 20:25 1.csv
Note that is -1 (one), not -l (the letter L). Good luck.
0

One liner with inline params (you should replace N with maximal file number). Script will skip unexisted files, so if you miss few files in sequence you wouldn't get error :)

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

Or script with params

#!/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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.