Issue
These are 2 grep statements which I am using to grep date and name from the path and then converting the output to html table format. Can you guys please help me in getting the desired output.
grep 'date' /path/to/file | cut -d "=" -f2-7 | cut -d " " -f2-7 | sed -e 's~.*=\(.*\)~<tr><td>\1</td></tr>~;1i\<html><table border="1">' -e '$a\</table>\n</html>';
grep 'name' path/to/file | cut -d " " -f1 | sed -e 's~.*=\(.*\)~<tr><td>\1</td></tr>~;1i\<html><table border="1">' -e '$a\</table>\n</html>';
The output I am getting:
<html>
<table border ="1">
<tr><td>Sun Sep 18 23:08:20 PDT 2022</td></tr>
<tr><td>Sun Sep 18 23:15:50 PDT 2022</td></tr>
<tr><td>Sun Sep 18 23:49:08 PDT 2022</td></tr>
</table>
</html>
<html>
<table border ="1">
<tr><td>aastha</td></tr>
<tr><td>anukriti</td></tr>
<tr><td>aakriti</td></tr>
</table>
</html>
Desired Output:
<html><table border="1">
<tr><td>Wed Sep 15 13:27:03 PDT 2021</td><td>aastha</td></tr>
<tr><td>Wed Sep 15 14:36:11 PDT 2021</td><td>kiyo</td></tr>
<tr><td>Wed Sep 15 21:13:12 PDT 2021</td><td>amisha</td></tr>
<tr><td>Thu Sep 16 06:31:22 PDT 2021</td><td>anukriti</td></tr>
<tr><td>Thu Sep 16 09:36:32 PDT 2021</td><td>aakriti</td></tr>
<tr><td>Thu Sep 16 09:39:09 PDT 2021</td><td>harni</td></tr>
<tr><td>Sun Sep 18 23:15:50 PDT 2022</td><td>harshu</td></tr>
</table>
</html>
Sample input:
name=aastha date=Sun Sep 18 23:49:08 PDT 2022
name=anukriti date=Sun Sep 18 23:15:50 PDT 2022
name=aakriti date=Sun Sep 18 23:08:20 PDT 2022
Solution
Using sed
$ sed -Ee '1i<html><table border="1">' -e 's~^name=([^ ]*)[^=]*=(.*)~<tr><td>\2</td><td>\1</td></tr>~;${a</table>\n</html>' -e '}' input_file
$ cat script.sed
1i<html><table border="1">
s~^name=([^ ]*)[^=]*=(.*)~<tr><td>\2</td><td>\1</td></tr>~
$ {
a</table>\n</html>
}
$ sed -Ef script.sed input_file
<html><table border="1">
<tr><td>Sun Sep 18 23:49:08 PDT 2022</td><td>aastha</td></tr>
<tr><td>Sun Sep 18 23:15:50 PDT 2022</td><td>anukriti</td></tr>
<tr><td>Sun Sep 18 23:08:20 PDT 2022</td><td>aakriti</td></tr>
</table>
</html>
Answered By - HatLess Answer Checked By - Mary Flores (WPSolving Volunteer)