LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 10-26-2016, 04:55 PM   #1
CaptainDerp
LQ Newbie
 
Registered: Mar 2013
Posts: 27

Rep: Reputation: Disabled
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.
 
Old 10-26-2016, 05:22 PM   #2
sneakyimp
Senior Member
 
Registered: Dec 2004
Posts: 1,056

Rep: Reputation: 78
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.
 
Old 10-26-2016, 05:31 PM   #3
CaptainDerp
LQ Newbie
 
Registered: Mar 2013
Posts: 27

Original Poster
Rep: Reputation: Disabled
Well

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.
 
Old 10-26-2016, 05:44 PM   #4
sneakyimp
Senior Member
 
Registered: Dec 2004
Posts: 1,056

Rep: Reputation: 78
Quote:
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?
 
Old 10-26-2016, 06:10 PM   #5
CaptainDerp
LQ Newbie
 
Registered: Mar 2013
Posts: 27

Original Poster
Rep: Reputation: Disabled
Perhaps

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.
 
Old 10-26-2016, 06:21 PM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,103

Rep: Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117
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.
 
Old 10-26-2016, 07:51 PM   #7
sneakyimp
Senior Member
 
Registered: Dec 2004
Posts: 1,056

Rep: Reputation: 78
Quote:
Originally Posted by CaptainDerp View Post
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...
 
Old 10-26-2016, 10:18 PM   #8
CaptainDerp
LQ Newbie
 
Registered: Mar 2013
Posts: 27

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by sneakyimp View Post
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.
 
Old 10-26-2016, 10:47 PM   #9
sneakyimp
Senior Member
 
Registered: Dec 2004
Posts: 1,056

Rep: Reputation: 78
plz answer questions i posed before

Quote:
* 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?
 
Old 10-26-2016, 11:57 PM   #10
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
Have a one line file ONELINER.js since a javascript file can be included freely in plain html even.

Its content is got by echo 'document.write("<td id="target">' $(wc -l somefile.txt) '</td>")' > ONELINER.js

This should create the one liner that can be included in the appropriate place in the script by>

<script source=ONELINER.js></script>

OK

Last edited by AnanthaP; 10-27-2016 at 12:02 AM.
 
Old 10-27-2016, 03:25 AM   #11
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,258
Blog Entries: 3

Rep: Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713
Quote:
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:

Code:
<!--# include virtual="/my_table.html"-->
Then your web server configuration, you'd add IncludesNOEXEC to the appropriate Options directive.

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.
 
1 members found this post helpful.
Old 10-27-2016, 04:25 AM   #12
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,103

Rep: Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117
No need to hold back - say what you really mean ...
 
Old 10-27-2016, 12:41 PM   #13
sneakyimp
Senior Member
 
Registered: Dec 2004
Posts: 1,056

Rep: Reputation: 78
Quote:
Originally Posted by Turbocapitalist View Post
Inserting a file into a web page is a text book case for the use of Server-Side Includes (SSI)
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 View Post
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.
 
Old 10-27-2016, 12:58 PM   #14
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,258
Blog Entries: 3

Rep: Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713
Quote:
Originally Posted by sneakyimp View Post
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 View Post
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"
 
Old 10-27-2016, 01:19 PM   #15
sneakyimp
Senior Member
 
Registered: Dec 2004
Posts: 1,056

Rep: Reputation: 78
Quote:
Originally Posted by Turbocapitalist View Post
SSI is the easy way.
But presumes the existence of the file. If I'm not mistaken NOEXEC will prevent one from running any BASH commands?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Move file contents to another file with bash script (find, mimetype and cat) _furrycat_ Linux - Newbie 1 10-06-2013 11:12 AM
Script, display contents of a file using cat Lindy70 Linux - Newbie 2 05-08-2012 10:13 AM
how to read data from text file and output into a table? j123 Linux - Newbie 26 04-15-2010 12:58 AM
display a row of data from a database in a table format 3vra Programming 2 03-13-2009 10:25 AM
Delete the first row from a text file loopoo Linux - Newbie 2 08-15-2006 02:57 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:08 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration