0

I am writing a shell script, and in that I am using three html files. Now I am trying to concatenate all three files, and send the content as an email to my self.

Please see below what I am trying to do three file: hello.html, hello1.html , hello2.html.

Now I am appending the output of all these three files to file Final.Html file like below and sending it, as an email.

>Final.html
cat hello.html >> Final.html
cat hello1.html >> Final.html
cat hello2.html >> Final.html
cat Final.html | sendmail -t

Now my input of all three files is shown below

$ cat hello.html
Hello world
$ cat hello1.html
india is my world
$ cat hello2.html
India is the best

After sending the mail, the output I am getting is below

Hello world
india is my world
India is the best

The output I am looking for is below with one empty line between each file. To get a clean and clear output.

Hello world

india is my world

India is the best
Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
Nikhil
  • 21
  • I think Brian may have been referring to the term “o/p” as an abbreviation for “output”. I agree that “output” is more quickly understood. If these are really HTML files, do you need a <br> or similar HTML code, versus a blank line? – Jeff Schaller Oct 01 '18 at 20:47

4 Answers4

3

This should give the required output,

awk 'NR>1 && FNR==1{print ""};1' ./*.html > /path/to/Final.html

(make sure the output file is not in the list of input files)

  • 1
    This is a neat solution, but it would be an even better answer if it had some description of what it is actually doing. – Kusalananda Oct 02 '18 at 11:33
  • It will merge the contents of all files ending with .html by adding a new line after the contents of each file to Final.html – Kamil Pathan Oct 02 '18 at 11:36
0

You can do that all with one use of cat which, while it is often used to display the contents of a file on the terminal, is actually made for con- cat -enating files (i. e. attaching them together):

$ cat hello.html hello1.html hello2.html | mail -s "Subject Goes Here" myself@mailhost.example.com
DopeGhoti
  • 76,081
  • Thanks, but will that really work, will cat command not take only 1 filename as a file and others as an argument?, I need a space between each file merge so that the final content shall have three different line with a space between the , i will be more than happy to check your way out as well. – Nikhil Oct 01 '18 at 17:26
  • 1
    This will do the same as original in question, it will not add the blank lines. – ctrl-alt-delor Oct 01 '18 at 17:31
0

This will do it, I have added code to add the blank lines. echo will output a newline, it can also be used to output more.

>Final.html
cat hello.html >> Final.html
echo >> Final.html
cat hello1.html >> Final.html
echo >> Final.html
cat hello2.html >> Final.html

This next one uses a bracket, to reduce the amount of code.

{
     cat hello.html
     echo
     cat hello1.html
     echo
     cat hello2.html 
}> Final.html
  • Since i am writing in Linux text while, I shall be using echo as well right before all the lines you have mentioned? – Nikhil Oct 01 '18 at 17:39
  • Both of my examples (above), should produce exactly the same output. However the 2nd has repeated code, so is nicer. (it only has > Final.html once) – ctrl-alt-delor Oct 01 '18 at 17:41
0

Using GNU awk (which supports the ENDFILE special pattern):

awk '1; ENDFILE { print "" }' hello.html hello1.html hello2.html >Final.html

The awk script passes the data unmodified data of each file in turn (the 1; may be replaced by { print }, which does the same thing). At the end of each file, the ENDFILE block will output an empty line.

To additionally insert the name of the file before the data of each file (this was not part of the question):

awk 'BEGINFILE { print FILENAME } 1
     ENDFILE   { print "" }' hello.html hello1.html hello2.html >Final.html

The special pattern BEGINFILE works just like ENDFILE but will be triggered before reading the first line of a new file. The FILENAME variable is a standard awk variable and contains the pathname of the current input file.

Kusalananda
  • 333,661