A few days back, we got the requirement that we need to parse an HTML table through shell script and generate a report based on the contents of the table. I tried many things and after much brainstorming created a shell script that parses the HTML tables into a CSV file. I will try to explain each and every step of this script.
As we can see that this script is divided into four aptly functions. Let’s discuss the functions one by one.
First, we come across the html_clean function. This function removes all the HTML syntax from the HTML page and it creates a text file which contains the data which was originally in the table which needed to be parsed. We can change the values in the sed command to get various desired (also undesired) effects in the output text file.
Then, there is clean_text function. This function needs some knowledge about the tables that are being utilized here. As I knew that there are 6 rows in my table, so I created a loop which will automatically format the text file in such a way that each row is in a separate line. Reading and manipulating this text file is much easier than the output from the previous function.
Now, we come across create_csv() function. As the name suggests, this function is responsible to read the file from the previous function and convert it into a CSV file. This function has been optimized to utilize the output of the previous function.
The last function check_status is just to create a report which in this case is all of the records lying in a particular time range. We can put any logic in here and in the end, we get a CSV file which can be used for further manipulation. In my case, the CSV file was used in an automation pipeline to check the status of some finished jobs of some application.
Hope, this blog helps. Happy Reading !!!