LinuxQuestions.org
Help answer threads with 0 replies.
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-27-2016, 02:22 PM   #16
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,642
Blog Entries: 4

Rep: Reputation: 3907Reputation: 3907Reputation: 3907Reputation: 3907Reputation: 3907Reputation: 3907Reputation: 3907Reputation: 3907Reputation: 3907Reputation: 3907Reputation: 3907

Quote:
Originally Posted by sneakyimp View Post
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.
 
Old 10-27-2016, 03:48 PM   #17
CaptainDerp
LQ Newbie
 
Registered: Mar 2013
Posts: 27

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by AnanthaP View Post
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.

Last edited by CaptainDerp; 10-27-2016 at 04:14 PM.
 
Old 10-27-2016, 11:18 PM   #18
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,642
Blog Entries: 4

Rep: Reputation: 3907Reputation: 3907Reputation: 3907Reputation: 3907Reputation: 3907Reputation: 3907Reputation: 3907Reputation: 3907Reputation: 3907Reputation: 3907Reputation: 3907
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.
 
Old 10-28-2016, 09:41 AM   #19
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,791

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
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>
...

Last edited by keefaz; 10-28-2016 at 09:45 AM.
 
Old 10-28-2016, 09:50 AM   #20
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,493

Rep: Reputation: 2809Reputation: 2809Reputation: 2809Reputation: 2809Reputation: 2809Reputation: 2809Reputation: 2809Reputation: 2809Reputation: 2809Reputation: 2809Reputation: 2809
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.
 
Old 10-28-2016, 10:00 AM   #21
ryz2theokzhun
LQ Newbie
 
Registered: Oct 2016
Posts: 1

Rep: Reputation: Disabled
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.
 
1 members found this post helpful.
Old 10-28-2016, 01:46 PM   #22
CaptainDerp
LQ Newbie
 
Registered: Mar 2013
Posts: 27

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by allend View Post
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.
 
Old 10-28-2016, 01:52 PM   #23
CaptainDerp
LQ Newbie
 
Registered: Mar 2013
Posts: 27

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by ryz2theokzhun View Post
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.
 
Old 10-28-2016, 09:34 PM   #24
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,791

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
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

Last edited by keefaz; 10-28-2016 at 09:37 PM.
 
  


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 12:12 PM
Script, display contents of a file using cat Lindy70 Linux - Newbie 2 05-08-2012 11:13 AM
how to read data from text file and output into a table? j123 Linux - Newbie 26 04-15-2010 01:58 AM
display a row of data from a database in a table format 3vra Programming 2 03-13-2009 11:25 AM
Delete the first row from a text file loopoo Linux - Newbie 2 08-15-2006 03:57 AM

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

All times are GMT -5. The time now is 11:36 PM.

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