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
CodePudding user response:
With your shown samples, please try following single awk
code. We need NOT to use so many tools for this task, this code will help you in terms of maintaining it also for future, since its done in single language.
awk -v FS='name=| date=' '
BEGIN{
print "<html><table border=\"1\">"
}
{
print "<tr><td>" $NF "</td><td>" $2 "</td></tr>"
}
END{
print "</table>\n</html>"
}
' Input_file
OR same above code in one-liner form will be as follows:
awk -v FS='name=| date=' 'BEGIN{print "<html><table border=\"1\">"} {print "<tr><td>" $NF "</td><td>" $2 "</td></tr>"} END{print "</table>\n</html>"}' Input_file
CodePudding user response:
Remove the <html>
and <table>
tags from the sed
commands.
Run the first command with its output to dates.txt
. It should just put the <tr>
tag at the beginning of each result line.
Run the second commands with its output to names.txt
. It should just put the </tr>
tag at the end of each result line.
Then use paste
to concatenate them side by side. Use simple printf
statements to add the <html>
and <table>
tags around it.
grep 'date' /path/to/file | cut -d "=" -f2-7 | cut -d " " -f2-7 | sed 's~.*=\(.*\)~<tr><td>\1</td>~' > dates.txt
grep 'name' path/to/file | cut -d " " -f1 | sed 's~.*=\(.*\)~<td>\1</td></tr>~' > names.txt
printf '<html>\n<table border="1">\n'
paste dates.txt names.txt
printf '</table>\n</html>\n'
CodePudding user response:
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>