ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
I am attempting to "export" the progress bar from wget display using sed. Basically, we have an app that starts wget to download a large file and we want to show a progress bar. Our application has a dbus interface to receive the download progress, so we were think of a command like:
wget [] | sed [] | dbus-send[]
The problem at the moment is, how do you get the matched string out of sed and into dbus-send?
I can get the progress string by:
sed -u 's/[0-9]*%/&/'
This populated '&' with the correct percentage, but I cannot seem to get this out of sed.
What environment are you talking about? You say it's an "app", but you're calling external tools like sed and wget? So is this being done as a shell script or what?
Assuming it's a script, you've got a couple of issues here. First of all, the wget output is being made to stderr instead of stdout, so you have to redirect it before sed can read it. Second, sed needs to have the -n option enabled and use "p" to the end of the substitution command so that it only exports the line you want.
Also, what exactly what text format is the dbus expecting for input? It's not clear to me exactly what you want sed to output. I used the following command and got the output shown:
(|& is a new Bash synonym for 2>&1 | , which redirects stderr to stdout, then into a pipe. Try the latter form if your shell is an older model.)
Is that what you want? For some reason the piped output doesn't match the regular output. It probably has something to do with the way wget dynamically updates the display.
But if the above is ok, grep can do the same thing more efficiently.
Code:
wget downloadfile -O/dev/null |& grep "%"
Or if you really want to grab only the percentage numbers and exclude the rest of the line, then you could use something like this:
Thanks for the input. I see now that the question did leave questions about the environment, etc.
To give a more in depth explanation, our application is a C app that calls an init script to launch wget and do a few other thing. The app does this on a host of conditions, etc.
Hence, when wget operates it is in a sh script environment.
Quote:
Originally Posted by David the H.
What environment are you talking about?
wget is operating inside a sh shell script.
Quote:
Originally Posted by David the H.
You say it's an "app", but you're calling external tools like sed and wget? So is this being done as a shell script or what?
The C app calls an init script which has #!/bin/sh operator.
Quote:
Originally Posted by David the H.
Also, what exactly what text format is the dbus expecting for input?
The dbus interface is a simple test string:
dbus-send --dest=[...] --type=method_call [...] [...].progress string:"persentage would go here [0..100]"
Hence in the example you gave, I only want the [0..100] before the percent:
I am not familiar with dbus-send, but after some small research I think dbus-send is not able to read STDIN. So you might want to try
Code:
wget [] | sed [] | xargs dbus-send []
However, you might have to pipe the complete message to it, i.e. use sed to compose the whole message that you want dbus-send to send to your app. Not 100% sure about that.
Example:
Ok,
fix the first issue by changing the substitution command from
Code:
s/.*([0-9]{1,3}%).*/\1/
to
Code:
s/.*[^0-9]([0-9]{1,3}%).*/\1/
About dbus-send:
As I said, I am not familiar with dbus-send, so I hope you know where you have to put the piped data. By default xargs simply appends it at the end.
You can change this by
Code:
# example
xargs -I{} echo Progress is {} ....
Now the output would be
Code:
Progress is 0% ....
So the {} is replaced by whatever comes through the pipe.
Maybe you need to only send a number, i.e. omit the '%' or ...
Just post an example of how you would send data to your app without using the pipe, e.g. how would you just send '12%' to your app?
1. it does not print 100%, it prints only single digits (1,2,..9,0,1,2..)
That's because of the greediness of .* in regex. In your expression:
Code:
.*([0-9]{1,3}%).*
The first .* consumes the longest possible match, which includes the first one or two digits of the percentage if they exist, before the next section kicks in. So the [0-9] only matches the last digit.
You need some way to stop the greediness. In this case, simply including the space before the number appears to work.
Code:
.* ([0-9]{1,3}%).*
But why are you back to using sed for this? It's only necessary if you need to modify the output string in some way, such as including some additional text. If you really only need the percentage, use the grep version I gave before.
I know nothing about dbus, but "badly formed" sounds like there's something in the input string that it doesn't like. I wonder if the % mark is restricted and needs to be escaped or something. In which case you may be back to using sed to modify the line. Try testing it without the percent sign. Just move it outside the parentheses in sed. Here's my slightly modified version.
Code:
sed -nru '/[0-9]%/ s/.* ([0-9]+)%.*/\1/p'
Also, I only put /dev/null in there as a testing device. It simply dumps the downloaded file into the bit-bucket instead of saving it. You probably don't want to do that in the final version. It shouldn't affect anything else.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.