2

This question already has an answer here:

I have a bunch of html files and I want to cat them all (individually) with a header file (several lines at least in length), say header.txt. I'm almost there. I can cat each file with the header file with the following command, but how can I then output the result to save it under the original filename?

find . -iname "*.html" | xargs -L 1 cat header.txt 

The above prints out what I want, but how can I then reference the xargs argument to add a final > original file name?

So in short, I want to insert the text from header.txt into the beginning of every html file. Can I modify the above command to do so? Or alternately are there other ways to achieve this?


As an update, here's what I've tried since posting, which hasn't worked:

find . -iname "*.html" | xargs -L 1 cat header.txt {} > {}.new

This does fine except it prints to a file literally named {}.new. That file looks as it should but this isn't helpful.

Also tried @Dave's suggestion, but there is a syntax error I can't fix:

 find . -iname "*.html" -exec (cat header.txt; cat {}) > {}.new
-bash: syntax error near unexpected token `('

marked as duplicate by roaima, don_crissti, chaos, mdpc, cas Oct 29 '15 at 6:51

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • This sounds like you want to insert the header.txt at the beginning of all your *.html files. Have I understood you correctly? – roaima Oct 28 '15 at 18:15
  • Maybe you want to reference stackoverflow.com/questions/9533679/…. Similar idea except the text you're adding is from cating a file and you're performing a find. I would lean towards something like find . -iname "*.html" -exec (cat header.txt; cat {}) > {}.new I would also check over the new files before scripting them to overwrite the originals. (I didn't test that command, it was meant to give an idea) – Centimane Oct 28 '15 at 18:21
  • @roaima yes that's exactly it. I added the clarification to the original question. Thanks. – sunny Oct 28 '15 at 18:24
  • @Dave thanks for the suggestion. Will try that out now. – sunny Oct 28 '15 at 18:25
  • @sunny Now that I'm thinking about it awk may also be a good tool for this, as you can insert text at line 1 using it – Centimane Oct 28 '15 at 18:28
1

I want to insert the text from header.txt into the beginning of every html file.

To prepend the contents of header.txt to all HTML files, use this oneliner:

for i in `find . -name '*.html'` ; do cat /path/to/header.txt > "$i".tmp && cat "$i" >> "$i".tmp && mv "$i".tmp "$i" ; done

Contents of 1.html:

first html file contents

Contents of header.txt:

my header

Contents of 1.html after running the script:

my header
first html file contents

(I've tested it, but do backup your files just in case.)

  • No problem, glad I could help. – A.P. Oct 28 '15 at 19:58
0

You can use a while loop, like the following:

find . -iname "*.html" | while read line; do cat header.txt "$line" > "${line}.new" ; done
  • thanks for your suggestion. It almost did the trick but it deposited the files in files with the literal ".new" at the end, which is definitely easy to fix in a second line anyhow. – sunny Oct 28 '15 at 19:50

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