Copy the content from txt file and create a html file

I have a txt file with a list of error messages in a xml tag format, and each error message is separated with a identifier(endresult).Need to split that and copy and create a new html file.Error message has some special character. how to escape the special character and insert my data into the file
Tried the below code

readsrror=`cat newfile.txt`
 echo "<html>" >>email.html
 echo "<body>" >>email.html
for i in $(echo $readsrror| sed "s/endresult/ /g")
do
    echo "<tr>" >>email.html
    echo "$i" >>email.html# only the tag is getting copied not the entire content.
    echo  "</tr>" >>email.html
done
echo "</body>" >>email.html
 echo "</html>" >>email.html

What exactly are you after?

When copying the content to another file only single words gets copied. Not sure if I need to escape the special characters before copying

$(echo $readsrror|

Try so

$(echo "$readsrror" |

--- Post updated at 11:45 ---

and

OFS=$IFS
IFS=$'\n'
for
...
done
IFS=$OFS

--- Post updated at 12:01 ---

:frowning: I'm wrong

--- Post updated at 12:14 ---

one more

awk 'BEGIN {print "<html>\n<body>"} {print "<tr>" $0 "</tr>"} END {print "</html>\n</body>"}' newfile.txt

or

awk 'BEGIN {print "<html>\n<body>"} {print "<tr>" $0 "</tr>"} END {print "</html>\n</body>"}' RS='[[:space:]]' newfile.txt

Thanks nezabudka , but these commands copy the entire content to my file in <tr> row,but I need to split the content from my base file and iterate through and copy each content into a <tr> tag.
Eg: Something like the below

read file
Split based on value <end>
for i to splitcount
<tr> $i</tr>
done

output:

<tr> errormessga [object not found :line 30 ]</tr>
<tr> Null pointer exceptions [no value available for the input :name ]</tr>

?

awk '...' $(<splitfile.txt)

--- Post updated at 08:26 ---

cat splitfile.txt

file1.txt file2.txt note.txt
end.txt

May be so, sorry I don't quite understand

awk 'BEGIN {print "<body><html>\n<tr>"}; FNR == 1 && NR != 1 {print "</tr>\n<tr>"} END {print "</tr>\n</body></html>"} 1' $(<splitfile.txt)
1 Like

It would seem that you would be much more likely to get what you want if you process the contents of your XML file directly instead of reading it into a variable and then echo ing those contents into sed .

You are asking us to make lots of guesses about what you are doing since you haven't shown us the input file that you are hoping will be translated to the output you say you want in post #5 in this thread. And the pseudo-code in post #5 doesn't really help because it contains a syntax error (i.e., there is no for i to splitcount command in any shell command language I've seen. Please show us the contents of the file (in CODE tags) you want to process to produce the output shown in post #5.

What shell are you using?

What operating system are you using? (Note that the standards say that the results are unspecified if the input file you feed into sed is not a text file. Using echo to convert the contents of a file into a single line may exceed the line length allowed in a text file depending on what operating system and what version of sed on that operating system you're using.)