I want to concatenate a couple files, but I want the filename of each file to be a…
- User
I want to concatenate a couple files, but I want the filename of each file to be a header preceding the file's contents. Either a small script or a shell command would work.
Assistant
To concatenate files with each filename as a header, you can use a simple shell loop. Here's a concise command:
for file; do echo "### $file ###" && cat "$file"; doneHow it works:
for fileiterates over all provided filenamesecho "### $file ###"prints the filename as a headercat "$file"outputs the file contents- The headers and contents are streamed together
Example usage:
# For specific files ./script.sh file1.txt file2.log > combined.txt # For all files in current directory ./script.sh * > combined.txt
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.