Skip to content

Instantly share code, notes, and snippets.

@Jacajack
Last active August 19, 2016 02:29
Show Gist options
  • Save Jacajack/17f43bea99d1b52cfe1b1288740d607d to your computer and use it in GitHub Desktop.
Save Jacajack/17f43bea99d1b52cfe1b1288740d607d to your computer and use it in GitHub Desktop.
Display formatted product list, and manage prices
#!/usr/bin/awk -f
#Sample list
#Milk <(2)[1][2.00]>
#Butter <(10)[2][1.00]>
#Basic init
BEGIN {
total = 0;
tokencnt = 0;
mul = 1;
colors = 1;
details = 0;
tax = 0;
}
function ceil( val )
{
return ( val == int( val ) ) ? val : int( val ) + 1;
}
#Ignore commented out lines
match( $0, /\#(.*)/ ) {
next;
}
#Look for MULTIPLIER token
match( $0, /<MULTIPLIER=(.*?)>/, values ) {
mul = values[1];
if ( colors ) printf( "\x1b[32m" );
printf( "Following content requested quantity will be multiplied by %d...", mul );
if ( colors ) printf( "\x1b[0m\n" );
}
#Look for COLORS token
match( $0, /<COLORS=(.*?)>/, values ) {
colors = values[1]
}
#Look for TAX token
match( $0, /<TAX=(.*?)>/, values ) {
tax = values[1]
}
#Look for DETAILS token
match( $0, /<DETAILS=(.*?)>/, values ) {
details = values[1]
}
#For each line containing token
match( $0, /(.*?)\t*<\((.*?)\)\[(.*?)\]\[(.*?)\]>(.*?)/, values ) {
tokencnt++;
token = substr( $0, RSTART, RLENGTH );
#Get description
name = values[1];
gsub( /\t/, "", name );
#Get count
cnt = values[2];
#Get real count
rcnt = mul * cnt;
#Get lot quantity
lot = values[3];
#Get price
cost = values[4];
#Calculate lot quantity
lotcnt = ceil( rcnt / lot );
#Calculate total position cost
cost *= lotcnt * lot;
total += cost;
#Print out
if ( colors ) printf( "\x1b[0m" );
printf( "%4d. %-62s \t", tokencnt, name );
if ( colors )printf( "\x1b[33m" );
printf( "%8.2f", cost );
if ( colors )printf( "\x1b[0m" );
if ( details ) printf( " %d * %d = %d\n", lotcnt, lot, lotcnt * lot );
else printf( "\n" );
}
END {
#Print out total cost
if ( colors ) printf( "\x1b[0m" );
for ( i = 0; i < 80; i++ ) printf( "-" );
printf( "\n" );
if ( tax != 0 )
{
printf( "Tax: " );
if ( colors ) printf( "\x1b[33m" );
printf( "%-66s %+8.2f\n", "", total * tax / 100 );
total += total * tax / 100;
}
if ( colors ) printf( "\x1b[0m" );
printf( "Total: " );
if ( colors ) printf( "\x1b[31m" );
printf( "%-64s %8.2f\n", "", total );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment