How to cat contents of text file to a table data or row
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How to cat contents of text file to a table data or row
Does anybody know how to how to cat/pipe or print, (w/e works best) the output of a command, in this case a simple wc -l query directly to a table data or row?
What Im trying to do is print the output of a command to a table data box inside of an html table.
So specifically.
I need to display the line count of individual files and crontab that script or command so its updated regularly, automatically on the website.
So I need the output ( in this example --> 123456 and no the output isnt static, it changes daily, so I cant simply print 123456 over and over, this is the whole reason it needs to be updated )
Ok so.
wc -l somefile.txt 123456
And I need that to be dumped to a table within /var/www/index.html
Which has a table in it, but this is the the specific <td> where the data needs to go, I have already added id= for this very reason
This is what I need.
<td id="target">123456</td>
Thanks in advance, google isnt really bringing me what I need, so hopefully if this gets answered somebody else whos as stumped as me can find it in the future.
Last edited by CaptainDerp; 10-26-2016 at 04:56 PM.
Your post doesn't really explain your question very well, but it sounds like you have a file, /var/www/index.html, presumably existing on a web server, and you want this file to report how many lines are in, somefile.txt, that presumably exists somewhere on your web server's file system (you didn't give us a path).
I would say that just running some linux command to count the lines in somefile.txt and locate where to put it in the index.html file sounds a bit tricky, but maybe not for a BASH expert.
Sadly, html files are usually not able to run server-side scripts unless you take special care to configure your web server to allow server-side code execution (e.g., server-side includes.
If, instead of index.html, you wanted to use PHP instead, then you could write a bit of PHP code to check the size of the file dynamically when the page is requested and just echo the information in response to a page request.
Well all I need to do is have the output of wc -l on /var/www/somefile.txt be printed or cat or echo'd or w/e works best, into a specific table, which means, inserting it between two specific objects located in /var/www/index.html
The two objects are unique within the file, and as I mentioned, the output needs to be placed directly between these objects
<td id="target"> and </td> so the desired result is that the output of wc -l /var/www/somefile.txt gets dumped to the html
<td id="target">123456</td> ( well there are actually multiple </td> ending tags in the table, so thats not unique is it, but there is a unique id=" for each td I need this done for, Ive actually got about 23 different files that needs to have this done for, but all I need is one working example to replicate )
Last edited by CaptainDerp; 10-26-2016 at 05:37 PM.
inserting it between two specific objects located in /var/www/index.html
Minimally, this requires a command like awk or grep or sed to search index.html for the appropriate objects. A non-trivial task which will depend very specifically on your file. Also, not something I can accomplish just using a BASH script.
The extra information is helpful, but you might want to be more specific:
* does this only need to happen one time? If so, why not do it by hand?
* does this need to be automated? If so, what is your means of triggering the process that does it?
* does this need to happen in response to a webs server request so that it's always up to date?
If these are all just simple text files, assign the output of the "wc" command to a bash variable. Use that variable in a sed command to substitute the appropriate text. Test it then stick it in a loop.
Make sure your regex is sufficiently specific - especially be aware of regex greediness (multiple ">" per line for example). And I find it's usually best to use backreferences to contain the bits you want to retain.
Perhaps it is not something either of us knows how to do, but it it can be done, and it isnt that complicated.
Which is precisely why I am hoping somebody will come along with a solution.
It isnt difficult to execute a series of sed/awk/grep commands in series in a bash script. The trick is just figuring out precisely how to do it.
I definitely know how to do it using PHP, and could execute this PHP script as a cron job or via a web server (or from a BASH script), but you may not have PHP installed in your server. That's why I was encouraging you to be more specific about how you expected to trigger this action.
Or you can just keep asking the original question...
I definitely know how to do it using PHP, and could execute this PHP script as a cron job or via a web server (or from a BASH script), but you may not have PHP installed in your server. That's why I was encouraging you to be more specific about how you expected to trigger this action.
Or you can just keep asking the original question...
I believe you are correct, a few people I have asked about this also are encouraging me to consider using php, which is actually doable, Im still clueless how to begin to approach it with php, but we have several php snippets here and there already running that have proven to be invaluable solutions in and of themselves.
Im definately open to any suggestions and certainly, very thankful.
* does this only need to happen one time? If so, why not do it by hand?
* does this need to be automated? If so, what is your means of triggering the process that does it?
* does this need to happen in response to a web server request so that it's always up to date?
And I need that to be dumped to a table within /var/www/index.html
Inserting a file into a web page is a text book case for the use of Server-Side Includes (SSI), but only with IncludesNOEXEC. With SSI your program or script can write to a file and then the web server will automatically insert that file into a designated place in your web page. That avoids the maintenance and security implications of PHP and follows the K.I.S.S. principle.
So in your web page, you'd have a line like this pointing to your table at the point in the HTML file where you want the table to be inserted:
SSI gets a bad reputation because it is very secure (with IncludesNOEXEC), very low maintenance, and very easy to set up. So while it gives you an excellent return on investment, it cuts into the revenue of consultants who profit on churn. They tend to be the ones you hear from in the trade press since they are numerous.
Another option could be a simple shell script with a Here Document that produces the entire HTML document, including the table, whenever the table needs updating.
I can appreciate that some fine folks use PHP and that there are many useful systems built with PHP, in spite of PHP itself which is acknowledged to be kind of a dumpster fire. More generally, PHP, Python, Perl, or Java Server Pages could be used, but any of them would be overkill if you are just inserting into a page or two. And client-side javascript has too many issues to address here.
Did you actually read what he's asking? He has an index.html file and can't figure out how to get the line count from another file inserted into that html in a particular table. He is not inserting one file into another. Moreover, offering him higher level concepts (like apache config, et. al) is likely to depress him about more stuff he doesn't know. My guess is that he also doesn't have root access to his server.
Quote:
Originally Posted by Turbocapitalist
if you are just inserting into a page or two. And client-side javascript has too many issues to address here.
Q.v., original post. The OP needs to read the line count of somefile.txt and insert that number (a number/string, not the contents of a file) into index.html. Javascript might actually work in this case whereas your suggestion will not.
Did you actually read what he's asking? He has an index.html file and can't figure out how to get the line count from another file inserted into that html in a particular table. He is not inserting one file into another. Moreover, offering him higher level concepts (like apache config, et. al) is likely to depress him about more stuff he doesn't know. My guess is that he also doesn't have root access to his server.
Yes, I read it. Inserting data from one file into another is what SSI does. Everything is a file including the product of "wc -l", especially if it goes into a proper file first. More information is needed so we don't have to 'guess'. If it's his server, he has root. If it's not, then the sysadmin has root and can help. A concrete concept like dealing with the web server will save him work. It's hardly an abstract concept.
Quote:
Originally Posted by sneakyimp
Q.v., original post. The OP needs to read the line count of somefile.txt and insert that number (a number/string, not the contents of a file) into index.html. Javascript might actually work in this case whereas your suggestion will not.
SSI is the easy way. Try it. The alternatives involve some level of HTML parsing, or else a lot of hoping and finger-crossing. Javascript on the server could work but would be in the pile that includes PHP, Tcl, Lua, Python, Perl, and even sed or awk as choices.
CaptainDerp, can you post an example of the HTML table you are working with? It helps to see as close to the exact spacing and line breaks as possible. You can remove sensitive information and post it between [code] [/code] tags. But do show which cell or row where you want the output from "wc"
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.