Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Here's the situation:

My file A.csv needs to be concatenated individually with all the other *.csv's in the folder and each of the outputs needs to have the same name as the particular *.csv.

How can I do this at the terminal? I have to do this 50 times and the other csv's have no ordered naming convention so typing manually is a pain.

The data is sensitive so I can't fool around with checking if cat A.csv *csv >*.csv works. (I'm guessing it won't)

share|improve this question
2  
FYI > will overwrite the file with the terminal output. >> will append the terminal output to the file. –  justbrowsing Aug 14 '13 at 5:18

1 Answer 1

up vote 3 down vote accepted

Use a for-loop in bash. You can make yourself a harmless test first:

#!/bin/bash
for f in *.csv; do
    if [ "$f" != 'A.csv' ]; then
        echo cat A.csv "$f" \> "$f.new"
    fi
done

This allows you to see what will be executed but not actually execute it.

Notice that I have called the output file $f.new. You can't guarantee that the cat will be performed before the shell starts overwriting the file. So I recommend you do this, or even better write the files into another directory:

mkdir combined
for f in *.csv; do
    if [ "$f" != 'A.csv' ]; then
        echo cat A.csv $f \> "combined/$f"
    fi
done

And of course, once you're happy, remove the echo (and the escaped > character):

cat A.csv "$f" > "combined/$f"
share|improve this answer
1  
If you want to handle spaces properly all you have to do is double-quote your variables, simple as that :) –  slhck Aug 14 '13 at 5:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.