[awk] Math & Bold-Font?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [awk] Math & Bold-Font?
# 1  
Old 02-12-2015
[awk] Math & Bold-Font?

Heya

There is a script which has presets stored in a tab-seperated file.
That script also has $help_text, which will be shown when called with invalid arguments or -h.
So i do need to have that file ready, so the help text can get the values out of the file, and print it with the $help_text.
That works, but i wonder if there is a way to do this with awk, as figured, it seems quite fast and power full, but until now i failed to see actual use for me.

How the relevant code parts look at the moment:
Code:
PRESETS=$CONFIG_DIR/presets
BOLD="\033[1m"
RESET="\033[0m"
UNDERLINE="\033[4m"
#
#	Check for PRESETS, required for proper help display
#
	WritePresetFile() { #
        # Write a basic table of the presets
        # 
	cat > "$PRESETS" << EOF
# Label	Resolution 	Pixels	Vidbit	Audbit	Bitrate	1min	Comment
# Label	Resolution 	Vidbit	Audbit	1min	Comment	 #--># This is for your orientation, the above is to display
scrn	resolve 	1024	192	?	This is only used for screenrecording
qvga	  320x240	240	128	2.8mb	Quarter of VGA, mobile devices 
hvga	  480x320	320	128	3.3mb	Half VGA, mobile devices
nhd	  640x360	512	192	5.2mb	Ninth of HD, mobile devices
vga	  640x480	640	192	6.1mb	VGA
a-vga	  640x480	512	192	5.2mb	VGA, optimized for anime cartoons
wvga	  768x480	768	192	7.6mb	Wide VGA
dvd	  720x480	720	256	7.2mb	DVD - PAL
wdvd	  720x576	800	256	7.7mb	DVD-wide - Pal
fwvga	  854x480	768	256	7.5mb	DVD-wide - NTCS, mobile devices
hd	 1280x720	1024	384	10.1mb	HD aka HD Ready
a-hd	 1280x720	768	384	8.5mb	HD, optimized for anime cartoons
fhd	1920x1080	1792	384	16.7mb	Full HD
a-fhd	1920x1080	1280	256	12.4mb	Full HD, optimized for anime cartoons
qhd	2560x1440	3072	448	26.9mb	Quad HD - 4xHD
uhd	3840x2160	5120	512	43.4mb	4K, Ultra HD TV
EOF
	}
	[ -f "$PRESETS" ] || \
		WritePresetFile
#
# The whole subshell is what i currently use.
#
# help_text=" .....
# ...
$( 
        printf "\t$UNDERLINE ";$SED s,"#",, "$PRESETS" | $GREP Pix
	printf "$TUI_RESET"
	$GREP -v Audbit "$PRESETS" | \
		while read label res vidbit audbit siz comment
		do
			if [ ! $label = scrn ]
			then	pixels=$[ ${res/x*/} * ${res/*x/} ]
			#	[ ${#pixels} -gt 9 ] && \
			#		dot=${pixels:0:( - 8 )} && \
			#		pixels="${pixels:0:(-9)}.${dot:0:1}bil"
			#	[ ${#pixels} -gt 6 ] && \
			#		dot=${pixels:0:( - 5 )} && \
			#		pixels="${pixels:0:(-6)}.${dot:0:1}mil"
				#[ $pixels -eq 0 ] && pixels="0     "
				if [ ${#pixels} -gt 6 ]
				then	dot_cut=5
					cut=6
					ext=mil
				elif [ ${#pixels} -ge 4 ]
				then	dot_cut=2
					cut=3
					ext=k
				elif [ ${#pixels} -le 3 ]
				then	dot_cut=""
					cut=""
					ext=""
				fi
				[ ! -z "$dot_cut" ] && \
					dot="${pixels:0:(-$dot_cut )}" && \
					pixels="${pixels:0:(-$cut)}.${dot:0:1}$ext" || \
					pixels="${pixels}$ext"

				bitrate=$[ $vidbit + $audbit ]
				#echo ${#bitrate} $bitrate
				if [ ${#bitrate} -gt 6 ]
				then	dot_cut=5
					cut=6
					ext=gb
				elif [ ${#bitrate} -ge 4 ]
				then	dot_cut=2
					cut=3
					ext=mb
				elif [ ${#bitrate} -le 3 ]
				then	dot_cut=""
					cut=""
					ext=kb
				fi

				[ HD = "${comment:0:2}" ] && \
					comment="${BOLD}HD${RESET}${comment:2}"
				[ ! -z "$dot_cut" ] && \
					dot="${bitrate:0:( - $dot_cut )}" && \
					bitrate="${bitrate:0:(-$cut)}.${dot:(${#dot} - 1):1}$ext" || \
					bitrate="${bitrate}$ext"

				printf "\t* ${BOLD}$label${RESET}\t$res \t$pixels\t$vidbit\t$audbit\t~$bitrate\t$siz\t$comment\n"
			fi
		done
)
"

Which then should output like:
Code:
	  Label	Resolution 	Pixels	Vidbit	Audbit	Bitrate	1min	Comment
	* qvga	320x240 	76.7k	240	128	~368kb	2.8mb	Quarter of VGA, mobile devices
	* hvga	480x320 	153.1k	320	128	~448kb	3.3mb	Half VGA, mobile devices
	* nhd	640x360 	230.2k	512	192	~704kb	5.2mb	Ninth of HD, mobile devices
	* vga	640x480 	307.3k	640	192	~832kb	6.1mb	VGA
	* a-vga	640x480 	307.3k	512	192	~704kb	5.2mb	VGA, optimized for anime cartoons
	* wvga	768x480 	368.3k	768	192	~960kb	7.6mb	Wide VGA
	* dvd	720x480 	345.3k	720	256	~976kb	7.2mb	DVD - PAL
	* wdvd	720x576 	414.4k	800	256	~1.0mb	7.7mb	DVD-wide - Pal
	* fwvga	854x480 	409.4k	768	256	~1.0mb	7.5mb	DVD-wide - NTCS, mobile devices
	* hd	1280x720 	921.9k	1024	384	~1.4mb	10.1mb	HD aka HD Ready
	* a-hd	1280x720 	921.9k	768	384	~1.1mb	8.5mb	HD, optimized for anime cartoons
	* fhd	1920x1080 	2.2mil	1792	384	~2.1mb	16.7mb	Full HD
	* a-fhd	1920x1080 	2.2mil	1280	256	~1.5mb	12.4mb	Full HD, optimized for anime cartoons
	* qhd	2560x1440 	3.3mil	3072	448	~3.5mb	26.9mb	Quad HD - 4xHD
	* uhd	3840x2160 	8.8mil	5120	512	~5.6mb	43.4mb	4K, Ultra HD TV

The bold HD in the comments are absolute optional, but a nice to have.

What i have so far:
(actualy had a better code last night, but since i failed with the bold thing, i just kept the above solution.)
Code:
	$GREP -v "#" "$PRESETS" | $AWK '{ 
		bitrate=$3+$4
		#pixels =${2/x*/} * ${2/*x/}   # When enabling this, i get no output at all -- seems substitution works diffrently for colums.
		print "\t* $BOLD"$1"$RESET\t"$2"\t"pixels"\t"$3"\t"$4"\t"bitrate"\t"$5"\t"$6
	}'

Which looks like:
Code:
	  Label	Resolution 	Pixels	Vidbit	Audbit	Bitrate	1min	Comment
	* ${BOLD}scrn$RESET	resolve		1024	192	1216	?	This
	* ${BOLD}qvga$RESET	320x240		240	128	368	2.8mb	Quarter
	* ${BOLD}hvga$RESET	480x320		320	128	448	3.3mb	Half
	* ${BOLD}nhd$RESET	640x360		512	192	704	5.2mb	Ninth
	* ${BOLD}vga$RESET	640x480		640	192	832	6.1mb	VGA
	* ${BOLD}a-vga$RESET	640x480		512	192	704	5.2mb	VGA,
	* ${BOLD}wvga$RESET	768x480		768	192	960	7.6mb	Wide
	* ${BOLD}dvd$RESET	720x480		720	256	976	7.2mb	DVD
	* ${BOLD}wdvd$RESET	720x576		800	256	1056	7.7mb	DVD-wide
	* ${BOLD}fwvga$RESET	854x480		768	256	1024	7.5mb	DVD-wide
	* ${BOLD}hd$RESET	1280x720		1024	384	1408	10.1mb	HD
	* ${BOLD}a-hd$RESET	1280x720		768	256	1024	8.5mb	HD,
	* ${BOLD}fhd$RESET	1920x1080		1792	384	2176	16.7mb	Full
	* ${BOLD}a-fhd$RESET	1920x1080		1280	256	1536	12.4mb	Full
	* ${BOLD}qhd$RESET	2560x1440		3072	448	3520	26.9mb	Quad
	* ${BOLD}uhd$RESET	3840x2160		5120	512	5632	43.4mb	4K,

First question, can i make bold text with awk print?
Second question, is about the math and substitution, i've seen that gsub be used, but i fail to understand - how.

Using an example from another thread:
Code:
'{x=substr($0,N+1); gsub(".","*",x); print substr($0,1,N) x}' N=3

I get the feeling, somehow awk reads from right to left, rather then left to right, like bash.
Anyway, as last N gets set to 3, so substr can subtract it on print, but what is that 1 and $0 (colum 0, does that mean the whole line?) doing there?
Also on the first x=substr, again column 0... and N gets increased to 4??
Smilie

Third question, how do i get the rest of the comments displayed?

Thank you

Last edited by sea; 02-12-2015 at 01:36 PM..
# 2  
Old 02-12-2015
awk is not shell. It does not have shell variables or shell substitutions. If you want shell variables in awk, you have to put them there. If you want substitutions, you'll have to use awk's.

$ means column in awk. That's why $1 gets you column one. Variables are just strings, like VAR=1. You can use an expression as a column number too, like print $(VAR+1) for column 2.

So, to get your variables, into awk, you will have to put them into awk.

Code:
awk -v VAR1="value1" -v VAR2="value2" ... '{ code }' filenames

Or the more old-fashioned

Code:
awk '{code}' VAR1="value1" VAR2="value2" filename

To get pixels, in awk code, I'd do this:

Code:
split($2, A, "x");
pixels=A[0] * A[1];

Also, doing grep | grep | grep | awk is pointless when it's that easy to just put it all in awk. You can replace this:

Code:
grep -v "^#" filename | awk '{ ... }'

with:

Code:
awk '/^#/ { next } { ... }' filename

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 02-12-2015
Hi, shell variables need to be passed to awk in a different way, for example:
awk -v bold="$BOLD" -v rst="$RESET"
The variable can then be accessed as bold and rst

I would try something like this (not tested):
Code:
$AWK '{ 
		bitrate=$3+$4
		# pixels =${2/x*/} * ${2/*x/}   # When enabling this, i get no output at all -- seems substitution works diffrently for colums. This will not work, parameter expansion is a shell feature, this is awk
		print "\t* " bold $1 rst, $2, pixels,$3,$4,bitrate,$5,$6
	}' OFS='\t'


Last edited by Scrutinizer; 02-12-2015 at 02:38 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 02-12-2015
To get this right:
Split takes column 2, splits the string at the x's and saves its output in an array(like) variable for later usage.
Sadly, pixels get calculated to 0 Smilie but the bold text is working, thank you. Smilie

I'm now stuck at further parsing of bitrates, i'd like to put a 'kb' at its end as long bitrates do not exceed 1mb, then 'mb' should be appended.
Code:
	$AWK '/^#/ { next } ;
	{ 
		bitrate=$3+$4;
		switch (length(bitrates)) {
			case >=4:
				ext=mb
			default:
				ext=kb
		}
		
		split($2, A, "x");
		pixels=A[0] * A[1];
		print "\t* "BOLD$1RESET"\t"$2"\t"pixels"\t"$3"\t"$4"\t"bitrate""ext"\t"$5"\t"$6;
	}' BOLD="\033[1m" RESET="\033[0m" UNDERLINE="\033[4m" "$PRESETS"

But this doesnt show any output at all Smilie

Thank you in advance

EDIT:
So with Scrutinizer's suggestion, and commented out the switch block,
Code:
$AWK '/^#/ { next } ;
	{ 
		bitrate=$3+$4;
		#switch (length(bitrates)) {
		#	case >=4:
		#		ext=mb
		#	default:
		#		ext=kb
		#}
		
		split($2, A, "x");
		pixels=A[0] * A[1];
		print "\t* "BOLD$1RESET,$2 "   ",pixels,$3,$4,bitrate ext,$5,$6;
	}' BOLD="\033[1m" RESET="\033[0m" UNDERLINE="\033[4m" OFS="\t" "$PRESETS"

i get this:
Code:
	  Label	Resolution 	Pixels	Vidbit	Audbit	Bitrate	1min	Comment
	* scrn	resolve   	0	1024	192	1216	?	This
	* qvga	320x240   	0	240	128	368	2.8mb	Quarter
	* hvga	480x320   	0	320	128	448	3.3mb	Half
	* nhd	640x360   	0	512	192	704	5.2mb	Ninth
	* vga	640x480   	0	640	192	832	6.1mb	VGA
	* a-vga	640x480   	0	512	192	704	5.2mb	VGA,
	* wvga	768x480   	0	768	192	960	7.6mb	Wide
	* dvd	720x480   	0	720	256	976	7.2mb	DVD
	* wdvd	720x576   	0	800	256	1056	7.7mb	DVD-wide
	* fwvga	854x480   	0	768	256	1024	7.5mb	DVD-wide
	* hd	1280x720   	0	1024	384	1408	10.1mb	HD
	* a-hd	1280x720   	0	768	256	1024	8.5mb	HD,
	* fhd	1920x1080   	0	1792	384	2176	16.7mb	Full
	* a-fhd	1920x1080   	0	1280	256	1536	12.4mb	Full
	* qhd	2560x1440   	0	3072	448	3520	26.9mb	Quad
	* uhd	3840x2160   	0	5120	512	5632	43.4mb	4K,

Regarding the comments, do i need to have to blindly append columns after $6, so all the words of any possible comments become shown?

Thank you

Last edited by sea; 02-12-2015 at 02:16 PM..
# 5  
Old 02-12-2015
I don't think you can do 'case >= 4'.

Also, ext=mb sets ext to blank unless mb happens to be set to something. I think you meant ext="mb"

Try: ext="kb" ; if(value >= 4) ext="mb";

Also, print "BOLD$1RESET" will print the literal words BOLD and RESET and the literal text $1. awk really is a different language than shell, it doesn't substitute inside quotes -- in fact it doesn't substitute, at all, ever. If you want the variables, do print BOLD $1 RESET

Lastly, and most importantly, you forgot to give awk a filename.
This User Gave Thanks to Corona688 For This Post:
# 6  
Old 02-12-2015
Sadly, setting / comparing with value seems to be an issue for my approach:
Code:
	$AWK '/^#/ { next } ;
	{ 
		bitrate=$3+$4;
		len=( length(bitrates) ); print len
		len = length(bitrates)  ; print len
		ext="kb"; if(len >= 4) ext="mb"; 
		if(len >= 4) bitrates=(substr(bitrates,3));

		split($2, A, "x");
		pixels=A[0] * A[1];
		
		print "\t* "BOLD$1RESET,$2 "   ",pixels,$3,$4,bitrate ext,$5,$6" "$7" "$8" "$9" "$10" "$11" "$12" "$13" "$14" "$15;
		
	}' BOLD="\033[1m" RESET="\033[0m" UNDERLINE="\033[4m" OFS="\t" "$PRESETS"

  1. While i do see now 'kb' for all the bitrates, it neither changes to mb when bitrate is 5120, nor does it cut 3 numbers from the string.
  2. Is there a better way o display the whole comment, rather than " "$7" "$8" "$9" "$10" "$11" "$12" "$13" "$14" "$15 to preseve words?
  3. pixels are still empty, and would require the same 'handling' as the bitrates.

len is always 0, so that is why the check doesnt work, but how to fix that please?

Thank you
# 7  
Old 02-12-2015
Garbage in, garbage out. What are you passing into length?

I see bitrate used in some places, bitrates in others.
This User Gave Thanks to Corona688 For This Post:
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