Shell command output into HTML

Hi unix geeks out there,
I have a file that contains the output of a command in text format

------------------------------------------------------------------------ r201535 | kashyapgiri@gmail.com| 2012-06-21 05:00:01 +0530 (Thu, 21 Jun 2012) |  1 line Changed paths: M /client/j2me/cc/cool/v.0/j2me/src/main/java/com/airone/cclient/j2me/db/RL.java Bug 63493 - J2ME : Pending Contact Count issue after SAS in Samsung 5230, Samsung b3410  URL:cor.airone/vv/AirOne/client/j2me/cc/cool/v.0/j2me/src/main/java/com/airone/cclient/j2me/db/RL.java ------------------------------------------------------------------------ r201572 | kashyapmanju@gmail.com | 2012-06-21 22:27:37 +0530 (Thu, 21 Jun 2012) |  1 line Changed paths: M /client/j2me/cc/cool/v.0/j2me/src/main/java/com/airone/cclient/j2me/db/resources.xml Error message has changed for invalid pin.  URL:cor.airone/vv/AirOne/client/j2me/cc/cool/v.0/j2me/src/main/java/com/airone/cclient/j2me/db/resources.xml  ------------------------------------------------------------------------ r201575 | ngupta@gmail.com | 2012-06-21 22:45:06 +0530 (Thu, 21 Jun 2012) |  1 line Changed paths: M /client/j2me/cc/cool/v.0/j2me/src/main/java/com/airone/cclient/j2me/db/MB.java Samsung Wave S8500 UI changes  URL:cor.airone/vv/AirOne/client/j2me/cc/cool/v.0/j2me/src/main/java/com/airone/cclient/j2me/db/MB.java

,

The giant task that lies in front of me is I need to edit each line of the file and format it into HTML output in order to send a mail.
The major problem is the highlighted part should be represented as a link with the name THE LINK ,on clicking the link i should be able to go to the actual page.
The output i need is as below,

------------------------------------------------------------------------  r201535 | kashyapgiri@gmail.com| 2012-06-21 05:00:01 +0530 (Thu, 21 Jun  2012) |  1 line Changed paths: M  /client/j2me/cc/cool/v.0/j2me/src/main/java/com/airone/cclient/j2me/db/RL.java  Bug 63493 - J2ME : Pending Contact Count issue after SAS in Samsung  5230, Samsung b3410  THE LINK------------------------------------------------------------------------  r201572 | kashyapmanju@gmail.com | 2012-06-21 22:27:37 +0530 (Thu, 21  Jun 2012) |  1 line Changed paths: M  /client/j2me/cc/cool/v.0/j2me/src/main/java/com/airone/cclient/j2me/db/resources.xml  Error message has changed for invalid pin.  THE LINK  ------------------------------------------------------------------------  r201575 | ngupta@gmail.com | 2012-06-21 22:45:06 +0530 (Thu, 21 Jun  2012) |  1 line Changed paths: M  /client/j2me/cc/cool/v.0/j2me/src/main/java/com/airone/cclient/j2me/db/MB.java  Samsung Wave S8500 UI changes  THE LINK  ------------------------------------------------------------------------

The thing is even though "THE LINK" part is displayed same, it should take me to different pages based on the selection.
Also i am allowed to place the remaining part of the file in a table or so.
I hope i have explained my problem well.
Please help me out in achieving this task.

Thanks,
Kashyap.

A link in HTML looks like the following:

<a href=[target URL here]>[link text here]</a>

The following sed-command should get you what you want:

sed 's/\(URL:[^ ]*\)/<a href=\1>THE LINK<\/a>/g' /path/to/infile > outfile

I hope this helps.

bakunin

1 Like

Do you want a piece of code to accomplish this ?

Hi SathyaOnunix,
Yep i do need a piece of code as a solution.
Please do help me.
Thanks,
Kashyap.

---------- Post updated at 02:19 PM ---------- Previous update was at 02:18 PM ----------

HI Bakunin,

I hav tried the the result u have given me.
Its not the solution i need.
Thanks,
Kashyap.

You can try this, but they may be more easier way than mine :slight_smile:

sed 's/--*[$-]/\n/g' file  | awk -F'|' 'BEGIN{print "<html>\n<table border="3" cellspacing="1" cellpadding="2">"} {print "<tr>";for(i=1;i<=NF;i++)print "<td>" $i"</td>";p
rint "</tr>"} END{print "</table>\n</html>"}' | sed 's/\(URL:[^ ]*\)/<a href=\1>THE LINK<\/a>/g'
1 Like

Then show the input you fed the script, the output it produced and in which way this differs from the desired result. I can write scripts, but not read minds.

bakunin

Hi bakunin,
I didn't mean to say your script wont work , it is part of my task.
As sathyaonunix suggested i needed the code to fulfill my task.
Hope i am clear. And please don consider my reply as a disrespect.
Your code works fine and creates the links as required.
Thanks,
Kashyap.

---------- Post updated at 04:28 PM ---------- Previous update was at 04:22 PM ----------

Hi sathya,

Your piece of code works marvellous.
But there is a slight hindrance i my task.
Will try to explain to you.
After i obtain the html tags as output i need to send a mail in the html format,
Hope u understand my problem.
I am using the below code to send the mail.

echo "Sending mail....."
cat -  <<EOF | sendmail -oi -t
From: ${MAILFROM}
To: ${MAILTO}
Subject: Mail script
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit
MIME-Version: 1.0
EOF
echo "Done!!!"

The Now please tell me how i can encapsulate the html output that you provided me into the mail.
I hope you understood my problem.
In simple words the output of the html should be encapsulated in the mail.
Thanks,
Kashyap.

you can try this:

sendmail -t <<EOF
Content-type: text/html
TO: $mailaddress
CC: $mailccaddress
FROM: kashyap@xxx.com
SUBJECT: My html email
IMPORTANCE: High
$(sed 's/--*[$-]/\n/g' file  | awk -F'|' 'BEGIN{print "<html>\n<table border="3" cellspacing="1" cellpadding="2">"} {print "<tr>";for(i=1;i<=NF;i++)print "<td>" $i"</td>";p
rint "</tr>"} END{print "</table>\n</html>"}' | sed 's/\(URL:[^ ]*\)/<a href=\1>THE LINK<\/a>/g')
EOF
1 Like

HI,
First of all happy new year.
Yes i got the required output.
Thanks for the help.
I don know to close the thread will you please do it for me,
Thanks,
Kashyap