Skip to content
Advertisement

Merge many csv files with similar names [closed]

I have many csv files in a particular format like 1file1.csv 2file1.csv 3file2.csv 4file2.csv 5file3.csv 6file3.csv and so on.

I want to merge all the files which have the same digit at the end of filename. Example 1file1.csv and 2file1.csv should be concatenated. Similarly, 3file2.csv and 4file2.csv should be concatenated.

Are there any linux commands to do it? Or an optimised python code?

Advertisement

Answer

Solution in bash:

printf "%sn" [0-9]*file[0-9]*.csv | sed 's/.*file//;' | sort -u | xargs -I{} -- sh -c 'cat [0-9]*file{} > output_file{}'

Files 1file1.csv 2file1.csv will be concatenated in output_file1.csv
Files 3file2.csv 4file2.csv will be concatenated in output_file2.csv

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement