user836087
user836087

Reputation: 2491

How can i append files to one another in the order i want in linux using pipes or redirects?

Lets say i have different files in a folder that contains the same day data such as :

ThisFile_2012-10-01.txt
ThatFile_2012-10-01.txt
AnotherSilly_2012-10-01.txt
InnovativeFilesEH_2012-10-01.txt

How to i append them to each other in any preferred order? Would below be the exact way i need to type in my shellscript? The folder gets same files everyday but with different dates. Old dates disappear so every day there are these 4 files.

InnovativeFilesEH_*.txt >> ThatFile_*.txt
ThisFile_*.txt >> ThatFile_*.txt
AnotherSilly_*.txt >> ThatFile_*.txt

Upvotes: 1

Views: 112

Answers (2)

sampson-chen
sampson-chen

Reputation: 47267

Assumption:

  • Want to preserve some specific ordering in which these files are appended.

Using the example you provided:

#!/bin/sh

# First find the actual files we want to operate on
# and save them into shell variables:

final_output_file="Desired_File_Name.txt"

that_file=$(find -name ThatFile_*.txt)
inno_file=$(find -name InnovativeFilesEH_*.txt)
this_file=$(find -name ThisFile_*.txt)
another_silly_file=$(find -name AnotherSilly_*.txt)

# Now append the 4 files to Desired_File_Name.txt in the specific order:

cat $that_file > $final_output_file
cat $inno_file >> $final_output_file
cat $this_file >> $final_output_file
cat $another_silly_file >> $final_output_file

Adjust the ordering in which you want the files to be appended by reordering / modifying the cat statements

Upvotes: 0

Ed Morton
Ed Morton

Reputation: 203665

Finally, a use for "cat" as intended :-):

cat InnovativeFilesEH_*.txt ThisFile_*.txt AnotherSilly_*.txt >> ThatFile_*.txt

Upvotes: 2

Related Questions