You are not logged in.

#1 2012-09-10 16:29:04

TheSaint
Member
From: my computer
Registered: 2007-08-19
Posts: 1,523

Bash string manipulation

Hello,

I'd like for some reason reformat the output from one program to mine.
Some is a still repeating line, such as mplayer. (They overwrite on the same line)
My vane efforts are trying to extract the percentage from those lines, but fifo or pipe don't get that line. Perhaps because it's expected lines ended by a newline.
Beside that, my trials with expr or sed going no where. I'm puzzled to find the world which has % leading sign and 2 or 3 digits before the decimal point and 1 or 2 digits after the decimal point like $$.$$% or $$.$%.
I've read many manuals of sed, bash, regex but seems that some regex doesn't do what I would like to be done.

So, what would be a simple extraction with small programs like sed, grep, awk or bash itself?


do it good first, it will be faster than do it twice the saint wink

Offline

#2 2012-09-10 16:37:51

Vain
Member
Registered: 2008-10-19
Posts: 179
Website

Re: Bash string manipulation

TheSaint wrote:

Perhaps because it's expected lines ended by a newline.

That’s your main problem. Try replacing \r with \n:

mplayer foo.avi | tr '\r' '\n' | sed ...

Offline

#3 2012-09-10 16:58:56

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Bash string manipulation

If there are no newlines, you have to read it byte by byte:

a=0
{ while sleep 0.5
do echo -n $((a+=1))
((a==4))&&break
done } | while read -n 1; do
[[ $REPLY == 3 ]] && echo -n 5 || echo -n $REPLY
done

Offline

#4 2012-09-10 18:41:25

TheSaint
Member
From: my computer
Registered: 2007-08-19
Posts: 1,523

Re: Bash string manipulation

Vain wrote:

Try replacing \r with \n:

That's a great idea. it will probably get the output into a (named) pipe and catch the last line with tail -n1
==================================================================
EDIT
That's wouldn't work, because even tr won't get the output, for the exact reason that isn't  "\n" terminated.
It took me some long thinking smile
==================================================================
More research of mine I could get what I need, by use of awk

$ a='xfe43.6%gege 96% vvbf yyy'                # A sample string for test.
echo $a | awk '{for (i=1; i <=NF; i++) {if ($i ~ /([0-9]*%$)/) print $i}}'
# result ==> 96%

In human thinking it means 'read all fields and if one of them contain % print it out'. I don't know (yet) a more accurate regex to avoid false positive results.
For one line reading I can get the figure and I may use it on zenity to display a progress bar tongue

Last edited by TheSaint (2012-09-11 10:44:48)


do it good first, it will be faster than do it twice the saint wink

Offline

#5 2012-09-11 13:00:33

quigybo
Member
Registered: 2009-01-15
Posts: 223

Re: Bash string manipulation

Check out stdbuf for running commands with different buffering operations.

Offline

#6 2012-09-11 14:15:58

Vain
Member
Registered: 2008-10-19
Posts: 179
Website

Re: Bash string manipulation

TheSaint wrote:
Vain wrote:

Try replacing \r with \n:

That's a great idea. it will probably get the output into a (named) pipe and catch the last line with tail -n1
==================================================================
EDIT
That's wouldn't work

Well, yes, it works. smile But it may appear not to work right now due to buffering issues. See quigybo’s comment and have a look at stdbuf.

Offline

#7 2012-09-12 14:16:39

TheSaint
Member
From: my computer
Registered: 2007-08-19
Posts: 1,523

Re: Bash string manipulation

Thanks to enlight me.  It was something I just read but I didn't find a solution to reduce the buffer size, like this.
I found mentioning that normal buffering is 4 Kb as standard size. Each pipe would add a new buffer, therefore the delay would grow proportionally.
Another of mine trial was to catch the aria2c output, but I'm bit confuse. would it be some as stdbuf -o 80 aria2c | myscript ?
Should I use to flush the stdin?

Last edited by TheSaint (2012-09-12 14:17:22)


do it good first, it will be faster than do it twice the saint wink

Offline

Board footer

Powered by FluxBB