2

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 `('
sunny
  • 123
  • This sounds like you want to insert the header.txt at the beginning of all your *.html files. Have I understood you correctly? – Chris Davies Oct 28 '15 at 18:15
  • Maybe you want to reference http://stackoverflow.com/questions/9533679/how-to-insert-a-text-at-the-beginning-of-a-file. 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
  • @roaima I'd say adding to the start of a file is different because you can't take the same luxuries, the biggest difference being that concatenation in place will not work: cat header file > file throws an error, and so command grouping is required – Centimane Oct 28 '15 at 18:31
  • @roaima thanks for the link. I am having a difficult time translating their recommendations about adding a string into recommendations about adding a file. It's not obvious how to make it work. – sunny Oct 28 '15 at 18:34
  • @Dave I tried out your example but it keeps saying "unexpected symbol (" in response to the (exec – sunny Oct 28 '15 at 18:34
  • @don_crissti the header file is several lines. – sunny Oct 28 '15 at 19:44
  • On that link I've suggested, look at the answer from rook – Chris Davies Oct 28 '15 at 20:04

2 Answers2

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.)

A.P.
  • 1,416
  • 9
  • 11
0

You can use a while loop, like the following:

find . -iname "*.html" | while read line; do cat header.txt "$line" > "${line}.new" ; done
Kira
  • 4,807
  • 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