Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have...

me@computer:~/gutenberg/euclid$ ls

book01.html book04.html book07.html book10.html book13.html book02.html book05.html book08.html book11.html book03.html book06.html book09.html book12.html

and I want to join all these .html files into the same big file, in order. What command or command sequence can I use?

share|improve this question
    
Are each on of these files, valid html files with a <head> and <body> section? –  freethinker May 23 '11 at 0:39
    
@freethinker yes, but it's not important –  ixtmixilix May 25 '11 at 9:57
add comment

4 Answers

up vote 2 down vote accepted

In this particular case cat book??.html > book.html will work fine, if you don't care about proper HTML format.

For a more general case, say you had "book1.html" instead of "book01.html", "book2.html" instead of "book02.html" and so forth. The file names don't sort lexically the same as logically. You can do something like this:

(echo book?.html | sort; echo book??.html | sort) | xargs cat > book.html

So in general: script_generating_file_names_in_order | xargs cat > all_one_file

That idiom can go a long way.

share|improve this answer
add comment

I buy the remarks about head/tail, and have a solution, sorted by number without that xargs/sort/echo stuff.

cat book{01..12}.html book-all.html
share|improve this answer
add comment

If you use GNU sort you can use sort -V to sort the names in the correct order:

$ echo -e "book4\nbook2\nbook17\nbook12" | sort -V
book2
book4
book12
book17

This works for any number of files:

ls book*.html | sort -V | xargs cat > allbooks.html
share|improve this answer
add comment

The simple solution will probably work, if your browser is permissiv.

cat book[0-9]*.html > book.html

Sure you could go around stripping stuff to be probably slightly more legal:

perl -e 'undef($/); for($x=0;$x<=$#ARGV;$x++) { open(X,"<",$ARGV[$x]); $_ = <X>; close(X); s/.*<body[^>]*>//s unless ($x == 0);  s|</body>|| unless ($x == $#ARGV); print;' book[0-9]*.html > book.html

But that is no guarantee that you will really have everything you need either, if the chapters have different styles or javascript or whatever.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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