0

I have found a brute force python programmatic way of concatenating multiple files while inserting some text characters in between the files.

Example:
test_file1 + " \'id#\',\',name,\' " +...+ test_fileN

BUT, is there a way to do this using only BASH commands (sed, grep, cat,...)?

2
  • 3
    You can use {...} command grouping and then i.e { cat file1; echo "foo bar baz"; cat file2 ; } > concat_all.txt Oct 12, 2021 at 19:46
  • yes, that would be good for a small number of files but not 100. TY
    – mccurcio
    Oct 13, 2021 at 16:29

2 Answers 2

1

I would type :

(for a in test_file*; do cat $a;echo " \'id#\',\',name,\' ";done) | sed '$d'

You just have to replace the test_file* by your actual name list (separated by spaces).

0

Method using shell builtins set, shift and printf, with no loop:

echo " \'id#\',\',name,\' " > /tmp/foo
set -- test_file[0-9]*
f="$1"
shift
cat "$1" $(printf '/tmp/foo %s ' "$@")
rm /tmp/foo    

You must log in to answer this question.

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