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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
|
|
10-27-2016, 02:22 PM
|
#16
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,642
|
Quote:
Originally Posted by sneakyimp
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.
|
|
|
10-27-2016, 03:48 PM
|
#17
|
LQ Newbie
Registered: Mar 2013
Posts: 27
Original Poster
Rep:
|
Quote:
Originally Posted by AnanthaP
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.
|
|
|
10-27-2016, 11:18 PM
|
#18
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,642
|
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.
|
|
|
10-28-2016, 09:41 AM
|
#19
|
LQ Guru
Registered: Mar 2004
Distribution: Slackware
Posts: 6,791
|
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.
|
|
|
10-28-2016, 09:50 AM
|
#20
|
LQ 5k Club
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,493
|
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.
|
|
|
10-28-2016, 10:00 AM
|
#21
|
LQ Newbie
Registered: Oct 2016
Posts: 1
Rep:
|
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.
|
10-28-2016, 01:46 PM
|
#22
|
LQ Newbie
Registered: Mar 2013
Posts: 27
Original Poster
Rep:
|
Quote:
Originally Posted by allend
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.
|
|
|
10-28-2016, 01:52 PM
|
#23
|
LQ Newbie
Registered: Mar 2013
Posts: 27
Original Poster
Rep:
|
Quote:
Originally Posted by ryz2theokzhun
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.
|
|
|
10-28-2016, 09:34 PM
|
#24
|
LQ Guru
Registered: Mar 2004
Distribution: Slackware
Posts: 6,791
|
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.
|
|
|
All times are GMT -5. The time now is 11:36 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|