4

Assume I have three text files, data_1, data_2, and data_3. Firstly, I need to copy data_1 inside to another new text file (new_data). Then I need to add data_2 to new_data (includes data_1). Finally I need to add data_3 to new_data (includes data_1 and data_2).

For example,

data_1=  10 10 10
         15 15 15

data_2=5 5 5


data_3=  11 11 11
         12 12 12


new_data=   10 10 10
            15 15 15
             5  5  5
            11 11 11
            12 12 12

How can I perform this task as a sequential way for multiple text files?

3
  • 1
    'cat data_1 data_2 data_3 > new_data' or with wildcards: 'cat data_? > new_data' ('cat' means 'concatenate'). If it must be in different steps, do 'cat data_1 > new_data; cat data_2 >> new_data; cat data_3 >> new_data' etc.
    – ridgy
    Aug 9, 2016 at 11:00
  • @ridgy If this is the answer to the question, why not post it as an answer?
    – Mark Kirby
    Aug 9, 2016 at 11:04
  • @ridgy, will you post your answer? Aug 9, 2016 at 11:12

1 Answer 1

12

As you are just appending the file contents one after another, use cat maintaining the sequence you want, currently this should do:

cat data_{1..3} >new_data

The shell will expand data_{1..3} into data_1 data_2 data_3, so the operation would eventually be:

cat data_1 data_2 data_3 >new_data
2
  • 1
    Just because I like this bash-ishm I want to point out that you can type cat data and then esc { and it will fill in the 1,2,3 so for however many files you have you can have bash match them all.
    – Mitch
    Aug 9, 2016 at 18:26
  • cat is originally short for "concatenate", as this was its intended purpose (concatenating files together).
    – user13161
    Aug 9, 2016 at 19:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .