LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to cat contents of text file to a table data or row (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-cat-contents-of-text-file-to-a-table-data-or-row-4175592301/)

Turbocapitalist 10-27-2016 01:22 PM

Quote:

Originally Posted by sneakyimp (Post 5623755)
But presumes the existence of the file. If I'm not mistaken NOEXEC will prevent one from running any BASH commands?

Yep. That and any other executables. It does not prevent running a cron job, using incron, or having other triggers. Again, when more information is available from the OP, then more precise proposals can be offered.

CaptainDerp 10-27-2016 02:48 PM

Quote:

Originally Posted by AnanthaP (Post 5623531)
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

The problem is this solution is actually creating the <td> and dumping the entire file name of target file. What I need is ONLY the line count output of wc -l, minus the file name, like.

Quote:

wc -l ../file/source.txt | awk '{print$1}'
3256
And I need it to be inserted inside the existing unique table data box, which I have given unique identifiers.

So I have these empty boxes ready for the line counts.

<td id="someid"></td>

Somehow I need to get this working like the following broken example: Just have sed insert the output of awk after the unit td tag. Im not sure how to make that work tho.

Quote:

wc -l ../dir/target.txt | awk '{print$1}' | sed '/\[option\]/<td id="someid">'
However you are on to something here, clearly this can be done using js as well, thank you for the example. I wouldnt want it to execute a wc -l however each time the page refreshed, this would cause serious delays on loading the web pages and cause serious IO issues with the web server due to the number and size of these files. Instead rather this needs to be scheduled via cron and executed on the server occasionally throughout the day.


So does anyone know how to get sed to insert the output of the following command directly into a unique table data box by getting sed to printing the output of the following command after <td id="someid">
Quote:

wc -l ../dir/source.txt | awk '{print$1}'
Surely this simple solution should work wonders if one of you brilliant volunteers wouldnt mind sharing the key with me.

Turbocapitalist 10-27-2016 10:18 PM

You're quite close. awk can do a lot with its printf function and you could then keep that in a variable, for later use in "sed":

Code:

w=$(wc -l ../dir/target.txt | awk '{printf "<td id=\"someid\">%d</td>\n", $1 }')
Then in "sed" you can search and replace, as long as no one has gone in and messed with the spacing or line breaks. It breaks easily in that way, thus the comment above for finger-crossing. Note the hash for delimiter instead of slash and that the double quotes need to be escaped:

Code:

sed -e "s#<td id=\"someid\">[[:digit:]]*</td>#<td id=\"someid\">$w</td>#" file.html
Your version of "sed" probably has an option for in-place editing of files and can even make a backup of the old file. For more robust XHTML parsing, such that will survive variations, see one of the proper XHTML parsers such as perl CPAN's HTML::TreeBuilder or HTML::TreeBuilder::XPath or something in your favorite scripting language if it not perl.

However, with what you describe, putting the output of "wc" into a text file and then using SSI to read that text file also works well.

"cron" can work quite well for you but if your file gets changed at more random intervals, then "incron" can launch scripts when a watched file is updated. That would give you more real-time data for your page.

keefaz 10-28-2016 08:41 AM

Look like it's a waste to rewrite the whole html file each time a td value needs to be actualized
It would be more efficient to use php or maybe javascript ajax

For example, write each file lines count in a file: count.txt
Code:

#!/bin/bash

for file in bla.txt other.txt thing.txt; do
  wc -l $file >> count.txt
done

And index.php:
PHP Code:

...
<?php $lines file('count.txt'); ?>
<table>
<?php 
foreach ($lines as $line) { 
  
// in hope there is no filename with spaces
  
list($count$name) = explode(' '$line);
?>
<tr><td class="filename"><?php echo $name?></td><td class="linescount"><?php echo $count?></td></tr>
<?php ?>
</table>
...


allend 10-28-2016 08:50 AM

If this produces a promising looking command
Code:

echo 'sed "s/\(<td id=\"someid\">\)[[:digit:]]*\(<\)/\1'$(wc -l < ../dir/target.txt)'\2/" file.html'
then look at the -i option to sed and the eval command.

ryz2theokzhun 10-28-2016 09:00 AM

This works on my machine. In a bash script, I wrote the following.

Code:

data=$(sed 's/.*="\(.*\)".*/\1/' /some_directory/stuff.html)
for file in $data
do
    wc_data=$(wc -l $file | awk '{print $1}')
    sed -i -e 's/'"$file"'">[0-9]\+</'"$file"'">'"$wc_data"'</' /some_directory/stuff.html
done

stuff.html was filled with the following text.

Code:

<td id="backup_home.sh">1</td>
<td id="bld.sh">8</td>
<td id="capture_start.sh">32</td>
<td id="config_ee.sh">14</td>
<td id="dpkg_query.sh">2</td>
<td id="fg.sh">11</td>
<td id="files_loop2.sh">11</td>
<td id="files_loop.sh">8</td>
<td id="find_and_report.sh">1</td>
<td id="git-meld.sh">2</td>
<td id="mldirs.sh">1</td>
<td id="prep_bbuild.sh">26</td>
<td id="sed_script.sh">24</td>
<td id="svn_ci_everything_but.sh">17</td>
<td id="test.sh">2</td>
<td id="tst1.sh">1</td>
<td id="update_dns.sh">11</td>

Hope that helps.

CaptainDerp 10-28-2016 12:46 PM

Quote:

Originally Posted by allend (Post 5624060)
If this produces a promising looking command
Code:

echo 'sed "s/\(<td id=\"someid\">\)[[:digit:]]*\(<\)/\1'$(wc -l < ../dir/target.txt)'\2/" file.html'
then look at the -i option to sed and the eval command.

I actually got this implemented, the only bit thats undesirable is the 'quotes' it prints on the numbers.

Note:

Placed the index.org which is copied to > index.html during the script run, it is in fact rather crude, but indeed functional.

Thank you.

CaptainDerp 10-28-2016 12:52 PM

Quote:

Originally Posted by ryz2theokzhun (Post 5624064)
This works on my machine. In a bash script, I wrote the following.

Code:

data=$(sed 's/.*="\(.*\)".*/\1/' /some_directory/stuff.html)
for file in $data
do
    wc_data=$(wc -l $file | awk '{print $1}')
    sed -i -e 's/'"$file"'">[0-9]\+</'"$file"'">'"$wc_data"'</' /some_directory/stuff.html
done

stuff.html was filled with the following text.

Code:

<td id="backup_home.sh">1</td>
<td id="bld.sh">8</td>
<td id="capture_start.sh">32</td>
<td id="config_ee.sh">14</td>
<td id="dpkg_query.sh">2</td>
<td id="fg.sh">11</td>
<td id="files_loop2.sh">11</td>
<td id="files_loop.sh">8</td>
<td id="find_and_report.sh">1</td>
<td id="git-meld.sh">2</td>
<td id="mldirs.sh">1</td>
<td id="prep_bbuild.sh">26</td>
<td id="sed_script.sh">24</td>
<td id="svn_ci_everything_but.sh">17</td>
<td id="test.sh">2</td>
<td id="tst1.sh">1</td>
<td id="update_dns.sh">11</td>

Hope that helps.

Indeed, I like that.

keefaz 10-28-2016 08:34 PM

sed -i creates temp file then moves it to original file, it's not too bad for a table update, although it could be better


All times are GMT -5. The time now is 07:57 PM.