[awk] Math & Bold-Font?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [awk] Math & Bold-Font?
# 8  
Old 02-12-2015
If you want 5120 kb to be displayed as 5 MB, bitrates=(substr(bitrates,3) would yield "20" which should not be considered a correct value. Use substr (bitrates,1,1) , or, even better, bitrates/=1024.

---------- Post updated at 21:07 ---------- Previous update was at 20:56 ----------

Would this do what you want:
Code:
{bitrate=$3+$4
ext=bitrate >= 1024 ?"MB":"kB"
bitrate/=bitrate >= 1024 ?1024:1}

This User Gave Thanks to RudiC For This Post:
# 9  
Old 02-12-2015
Ok, so far so good, still stuck with if blocks...

4 Awk If Statement Examples ( if, if else, if else if, :? )
The AWK Manual - String Functions (guess this doesnt work because of nawk != awk)
But even when starting with the 'awk' manual, i end up: The GNU Awk User Smilie

Code:
	$AWK '/^#/ { next } ; 
	/^scrn/ { next } ;
	{ 
		bext="kb";
		bitrates=$3+$4;
		len=( length(bitrates) );
		if(len >= 4) 
			ext="mb"
			cut=len-3
			bitrates=substr(bitrates,1,cut);
			
		split($2, A, "x");
		pext="k";
		pixels=A[1] * A[2];
		len=( length(pixels) );
	#	if(len >= 7 )
	#		ext="bil"
	#		cut=len-6
	#		pixels=substr(pixels,1,cut);
	#	else
			if(len >= 4)
				ext="mil"
				cut=len-3
				pixels=substr(pixels,1,cut);
		
		print "\t* "BOLD$1RESET,$2 "   ",pixels pext,$3,$4,"~" bitrates bext,$5,$6" "$7" "$8" "$9" "$10" "$11" "$12" "$13" "$14" "$15;
		
	}' BOLD="\033[1m" RESET="\033[0m" UNDERLINE="\033[4m" OFS="\t" "$PRESETS"

Prints out as:
Code:
	  Label	Resolution 	Pixels	Vidbit	Audbit	Bitrate	1min	Comment
	* qvga	320x240   	76k	240	128	~kb	2.8mb	Quarter of VGA, mobile devices     
	* hvga	480x320   	153k	320	128	~kb	3.3mb	Half VGA, mobile devices      
	* nhd	640x360   	230k	512	192	~kb	5.2mb	Ninth of HD, mobile devices     
	* vga	640x480   	307k	640	192	~kb	6.1mb	VGA         
	* a-vga	640x480   	307k	512	192	~kb	5.2mb	VGA, optimized for anime cartoons     
	* wvga	768x480   	368k	768	192	~kb	7.6mb	Wide VGA        
	* dvd	720x480   	345k	720	256	~kb	7.2mb	DVD - PAL       
	* wdvd	720x576   	414k	800	256	~1kb	7.7mb	DVD-wide - Pal       
	* fwvga	854x480   	409k	768	256	~1kb	7.5mb	DVD-wide - NTCS, mobile devices     
	* hd	1280x720   	921k	1024	384	~1kb	10.1mb	HD aka HD Ready      
	* a-hd	1280x720   	921k	768	256	~1kb	8.5mb	HD, optimized for anime cartoons     
	* fhd	1920x1080   	2073k	1792	384	~2kb	16.7mb	Full HD        
	* a-fhd	1920x1080   	2073k	1280	256	~1kb	12.4mb	Full HD, optimized for anime cartoons    
	* qhd	2560x1440   	3686k	3072	448	~3kb	26.9mb	Quad HD - 4xHD      
	* uhd	3840x2160   	8294k	5120	512	~5kb	43.4mb	4K, Ultra HD TV

What confuses me is the diffrence between the pixel and bitrate columns - allthough the 'producing' codeblock is identical.

It should only cut off 3 numbers when there are at least 4 numbers, for the pixels it doesnt work, as in when i uncomment those lines checking for 'bil' values, it wont print any output at all.
And regarding the bitrate, i seem to not check it properly, what am i doing wrong?

Thank you

---------- Post updated at 21:44 ---------- Previous update was at 21:34 ----------

What i try to say/achieve:
All the highlighted k(b)'s should be m(b)'s.
The red ~ should be followed by 3 numbers for kb, and 'all' the megabyte number and followed by the mb string for numbers within that range.

---------- Post updated at 21:53 ---------- Previous update was at 21:44 ----------

@ RudiC, so focused on cutting of number of chars, that i forgot to do the math Smilie
However, i would need to limit the amount of decimals after the dot.
Output example:
Code:
# RAW: 0.359375
~0.359375mb

Desired:
Code:
# RAW: 0.3
# RAW: 10.0 ## or just 10
~0.3mb
~10.0mb

And since i already have problems cutting the right position of a integer number, i'm clueless where to start for this one.

Thank you

Last edited by sea; 02-12-2015 at 04:40 PM..
# 10  
Old 02-12-2015
1) You print bext but assign "mb" to ext .
2) awk's if needs {...} around a compound statement; your snippet executes ext="mb" only if len>=4, but cut= etc for every line.
3) cf. formatted printf to do some rounding on decimal places.
This User Gave Thanks to RudiC For This Post:
# 11  
Old 02-13-2015
Its working now as expected, thank you guys.
Got any improvement suggestions?

Code:
	$AWK '/^#/ { next } ; 
	/^scrn/ { next } ;
	{ 
		bext="kb";
		pext="k";
		
	# Bitrates
		bitrates=$3+$4;
		len=( length(bitrates) );
		if(len >= 4) {
			bext="mb"
			bitrates/=1024
		}
		split(bitrates, B, ".");
		bitrates=B[1] ;
		dots=B[2];
		if (dots == "") { dots="" } else { dots="."dots }
		
	# Pixels
		split($2, A, "x");
		pixels=A[1] * A[2];
		len=( length(pixels) );
		if(len >= 7 ) {
			pext="mil"
			cut=len-6
			#pixels=substr(pixels,1,cut);
			pixels=pixels / 1000 / 1000
		} else {
			if(len >= 4) {
				pext="k"
				cut=len-3
				#pixels=substr(pixels,1,cut);
				pixels/=1000
			}
		}
		split(pixels, P, ".");
		pix=P[1] ;
		pots=P[2];
		if (pots == "") { pots="" } else { pots="."pots }
		
	# Output
		print "\t* "BOLD$1RESET,$2 "   ",pix substr(pots,1,2) pext,$3,$4,"~" bitrates substr(dots,1,2) bext,$5,$6" "$7" "$8" "$9" "$10" "$11
		
	}' BOLD="\033[1m" RESET="\033[0m" OFS="\t" "$PRESETS"

One last thing, the calculation is a bit weird... see screenshot (wide).
They both refer to this calculated amount of pixels, but get diffrent end values, eventhough i initialy try the same approach.
Code:
	76800	
	153600	
	230400	
	307200	
	307200	
	368640	
	345600	
	414720	
	409920	
	921600	
	921600	
	2073600	
	2073600	
	3686400	
	8294400

Any ideas?
Thank you and have a good weekend Smilie
[awk] Math & Bold-Font?-help-text-comparejpg
# 12  
Old 02-13-2015
LOOL scratch that, figured i did the left script (original) shown with wrong cutting...
/solved & good night Smilie

---------- Post updated at 00:19 ---------- Previous update was at 00:19 ----------

LOOL scratch that, figured i did the left script (original) shown with wrong cutting...
/solved & good night Smilie
# 13  
Old 02-14-2015
Well, hmmm, I'm not sure I understand what you're doing up there ... mayhap you want to look into this one:
Code:
awk     'BEGIN  {split ("B kB MB GB", UNT)
                 ln10=log(10)
                 print "          Label Resolution      Pixels             Vidbit Audbit  Bitrate 1min    Comment"
                }
         NR==1 ||
         /^#/ ||
         /^scrn/ { next } ;

        {
        # Bitrates
                TMP=$3+$4;
                XP=int(log(TMP)/ln10/3)
                bitrate = sprintf ("%.2f", TMP / 10^(3*XP))
                bUNT=UNT[1+XP]
                
        # Pixels
                split($2, A, "x");
                TMP=A[1] * A[2];  
                XP=int(log(TMP)/ln10/3)
                pixels = sprintf("%.2f", TMP / 10^(3*XP))
                pUNT = UNT[1+XP]
 
        # Output
                print "\t* "BOLD$1RESET,$2 "   ",pixels pUNT , $3, $4, "~" bitrate bUNT, $5, $6" "$7" "$8" "$9" "$10" "$11
                
        }' BOLD="\033[1m" RESET="\033[0m" OFS="\t" file
     Label    Resolution    Pixels   Vidbit    Audbit    Bitrate    1min    Comment
    * qvga    320x240        76.80kB    240    128    ~368.00B    ~kb    2.8mb Quarter of VGA, mobile devices
    * hvga    480x320       153.60kB    320    128    ~448.00B    ~kb    3.3mb Half VGA, mobile devices 
    * nhd     640x360       230.40kB    512    192    ~704.00B    ~kb    5.2mb Ninth of HD, mobile devices
    * vga     640x480       307.20kB    640    192    ~832.00B    ~kb    6.1mb VGA 
    * a-hd   1280x720       921.60kB    768    256    ~1.02kB    ~1kb    8.5mb HD, optimized for anime cartoons
    * fhd   1920x1080       2.07MB     1792    384    ~2.18kB    ~2kb    16.7mb Full HD   
    * a-fhd 1920x1080       2.07MB     1280    256    ~1.54kB    ~1kb    12.4mb Full HD, optimized for anime

Units may have to be adapted as well as <TAB>s, but anyway... might be worthwhile?

---------- Post updated at 15:20 ---------- Previous update was at 14:56 ----------

Or even
Code:
awk     'BEGIN  {split ("B kB MB GB", BUNT)
                 split ("p kp Mp Gp", PUNT)
                 ln10=log(10)
                 print "          Label Resolution      Pixels          Vidbit  Audbit  Bitrate 1min    Comment"
                }

         function FMT(NBR, U)
                {XP=int(log(NBR)/ln10/3)
                 return sprintf ("%.2f %s", NBR / 10^(3*XP), U[1+XP])
                }

         NR==1 ||
         /^#/ ||
         /^scrn/ { next } ;

        {
        # Bitrates
                bitrate = FMT($3+$4, BUNT);

        # Pixels
                split($2, A, "x");
                pixels = FMT(A[1] * A[2], PUNT);

        # Output


Last edited by RudiC; 02-14-2015 at 10:12 AM..
This User Gave Thanks to RudiC For This Post:
# 14  
Old 02-14-2015
Thank you Rudic, that is something to study on monday Smilie
The above is my - obviously with help - first awk 'script'.

So by the time i really got annoyed by the attempt to substr the proper number of chars of the numbers, to print 1 or 2 decimals (couldnt decide) after the dot, and the dot only if required there were uneven numbers.
So i came up with a solution using methods i knew or just learned and did the job i wanted Smilie

Since i had problems to get the amount of full numbers before the dot, to the remove them and the dot, to just use the first (two) char/s then to display, i used split to get full and uneven (decimal) numbers and did just the same, with the 'extracted' string in the array.

Have a good weekend Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. What is on Your Mind?

Slowly Removing Bold Font Style - Step-by-Step

FYI, I'm slowly removing a lot of the bold font-styles from titles of discussions, forum titles, etc I'm not removing bold for the entire site because we do need bold from time to time, especially in posts and sometimes in other places. However, the original forum style had way too much... (3 Replies)
Discussion started by: Neo
3 Replies

2. Shell Programming and Scripting

Math count %memory using awk

Hi expert, i have log this: Memory: 74410384 Memory: 75831176 Memory: 77961232 Memory: 77074656 Memory: 76086160 Memory: 77128592 Memory: 78045384 Memory: 76696040 Memory: 72401176 Memory: 72520016 Memory: 72137016 Memory: 73175832 Memory: 73034528 Memory: 71770736 Memory:... (4 Replies)
Discussion started by: justbow
4 Replies

3. Shell Programming and Scripting

Count math using awk

Hi expert, I have log : TOTAL-TIME : 2125264636 DATA-BYTES-DOWN : 3766111307032 DATA-BYTES-UP : 455032157567 DL = (3766111307032/2125264636)/1024 = 1.73 UL = (455032157567/2125264636)/1024 = 0.21 I want the result : TOTAL = 1.94 ... (4 Replies)
Discussion started by: justbow
4 Replies

4. Shell Programming and Scripting

awk --> math-operation in a array

Hi main object is categorize the difference of data-values (TLUFT02B - TLUFT12B). herefor i read out data-files which are named acording to the timeformat yyyymmddhhmm. WR030B 266.48 Grad 0 WR050B 271.46 Grad 0 WR120B 268.11 Grad 0 WV030B 2.51 m/s ... (6 Replies)
Discussion started by: IMPe
6 Replies

5. Shell Programming and Scripting

awk in horizontal and vertical math

Based on input ail,UTT,id1_0,COMBO,21,24,21,19,85 al,UTHAST,id1_0,COMBO,342,390,361,361,1454 and awk code as awk -F, '{ K=0; for(i=NF; i>=(NF-4); i--) { K=K+$i; J=J+$i;} { print K } } END { for ( l in J ) printf("%s ",J); }' I'm trying to add columns and lines in single line. line... (6 Replies)
Discussion started by: busyboy
6 Replies

6. Shell Programming and Scripting

awk math and csv output

Hi I have this list 592;1;Z:\WB\DOCS;/FS3_100G/FILER112/BU/MPS/DOCS;;;;\\FILER112\BUMPS-DOCS\;580,116,544,878 Bytes;656,561 ;77,560 592;2;Z:\WB\FOCUS;/FS3_100G/FILER112/BU/MPS/FOCUS;;;;\\FILER112\BUMPS-FOCUS\;172,430 Bytes;6 ;0 ... (12 Replies)
Discussion started by: nakaedu
12 Replies

7. Shell Programming and Scripting

output text in bold font using SED

hi I want to write a script, while using the SED editor, to output the text, in this case a variable, to the result file but highlighted it in bold, is it possible to do that? can you tell me how? eg. in text.txt sed '$ a\ '$variable' ' <text.txt >text2.txt so it will add the... (2 Replies)
Discussion started by: piynik
2 Replies

8. Shell Programming and Scripting

Need help with AWK math

I am trying to do some math, so that I can compare the average of six numbers to a variable. Here is what it looks like (note that when I divide really big numbers, it isn't a real number): $ tail -n 6 named.stats | awk -F\, '{print$1}' 1141804 1140566 1139429 1134210 1084682 895045... (3 Replies)
Discussion started by: brianjb
3 Replies

9. UNIX for Dummies Questions & Answers

colored/highlighted/bold matching pattern with awk ???

Hi ! Just wondering, is it possible to color or highlight or underline a matching pattern with awk ? Or write it in bold, italic.....? (3 Replies)
Discussion started by: lucasvs
3 Replies

10. UNIX for Dummies Questions & Answers

awk logic and math help

Hi, My file has 2 fields and millions of lines. variableStep chrom=Uextra span=25 201 0.5952 226 0.330693 251 0.121004 276 0.0736858 301 0.0646982 326 0.0736858 401 0.2952 426 0.230693 451 0.221004 476 0.2736858 Each field either has a... (6 Replies)
Discussion started by: wyarosh
6 Replies
Login or Register to Ask a Question