User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Shell Scripting section within the Software Development category of DaniWeb, a massive community of 456,444 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,604 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Shell Scripting advertiser: Programming Forums
Views: 3738 | Replies: 23 | Solved
Reply
Join Date: Feb 2005
Posts: 92
Reputation: tones1986 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
tones1986 tones1986 is offline Offline
Junior Poster in Training

Re: BASH script - sales per each associate (3 files)

  #21  
Dec 4th, 2007
I am trying something like what you said - but could you please give me more information about the use of the pipe (|). What command (ie the first or 2nd?) gets put into the input of what command (the 1st or 2nd)...i know its basic stuff - but looking online at examples its not opbvious.

This is what i have tried so far... any hints? If you answer the above and help me figure out the options for the sort/awk it would be awesome.

sort -n /tmp/output.$$ | awk -F":" '{print $1 $2 $3}' 

Gives me this:

/home/lx/z109079 : ./newtest.sh
Name: Position: Total Sales:
Dennis MillerCommedian1.98
Fast BuckStock Boy2504.83
George BushPresident1059.67
Hillary ClintonCandidate151.00
John DoeClerk284.74
Susan WorkerManager279.52

script:
#! /bin/bash
awk '/2007/ {print $0}' sales > /tmp/newSales

declare -a names
declare -a totalsales
declare -a position

echo -e "Name:\tPosition:\tTotal Sales:"
ID=1
while [ $ID -le 6 ]
do
  awk /[2][$ID]$/ /tmp/newSales > /tmp/sales.2$ID
  let ID=$ID+1
done

GRANDTOTAL=0
ID=1
while [ $ID -le 6 ]
do
  while read line
  do
    PRODID=`echo $line|cut -d"," -f1`
    QUANT=`echo $line|cut -d"," -f2`
    PRICE=`awk /^$PRODID/ products|awk -F":" '{print $3}'`
    TOTAL=$(echo "scale=2; $PRICE*$QUANT" | bc)
    GRANDTOTAL=$(echo "scale=2; $GRANDTOTAL+$TOTAL" | bc)
done < "/tmp/sales.2$ID"

totalsales[$ID]=$GRANDTOTAL
NAMES[$ID]=`awk /^[2]"$ID"/ associates | awk -F"/" '{print $2}'`
position[$ID]=`awk /^[2]"$ID"/ associates | awk -F"/" '{print $4}'`

echo ${NAMES[$ID]}":"${position[$ID]}":"${totalsales[$ID]} >> /tmp/output.$$

GRANDTOTAL=0
let ID=$ID+1
done

sort -n /tmp/output.$$ | awk -F":" '{print $1 $2 $3}' #/tmp/output.$$

# removes all created files
rm /tmp/sales.2*
rm /tmp/newSales
rm /tmp/output.$$

Im so close thanks to your help... thanks once again, so much apprecaited
Last edited by tones1986 : Dec 4th, 2007 at 5:14 pm. Reason: added the output... and entire script
Reply With Quote  
Join Date: Oct 2007
Posts: 302
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Rep Power: 3
Solved Threads: 37
stilllearning stilllearning is offline Offline
Posting Whiz

Re: BASH script - sales per each associate (3 files)

  #22  
Dec 4th, 2007
Sorting a delimited file

for example if your record is and you want to sort it by the 3rd field,

a:b:c

sort -t':' -k 3 

To sort it in numeric order add a -n to the line

sort -n -t':' -k 3 

Your awk can just be a regular printf statement

awk -F':' '{printf("%3d\t%-20s\t$%10.2f\n", $1, $2, $3)}'
Last edited by stilllearning : Dec 4th, 2007 at 9:39 pm.
Reply With Quote  
Join Date: Feb 2005
Posts: 92
Reputation: tones1986 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
tones1986 tones1986 is offline Offline
Junior Poster in Training

Re: BASH script - sales per each associate (3 files)

  #23  
Dec 4th, 2007
The file output.$$ is this:

John Doe:Clerk:284.74
George Bushresident:1059.67
Susan Worker:Manager:279.52
Fast Bucktock Boy:2504.83
Hillary Clinton:Candidate:151.00
Dennis Miller:Commedian:1.98

********EDIT********
I changed the line around and removed some parts to make it simpler (using the man page for printf()) and came up with this out put:

Dennis Miller Commedian $ 1.98
Fast Buck Stock Boy $ 2504.83
George Bush President $ 1059.67
Hillary Clinton Candidate $ 151.00
John Doe Clerk $ 284.74
Susan Worker Manager $ 279.52

From this line:
awk -F":" '{printf("%s\t%s\t$%10.2f\n", $1, $2, $3)}' /tmp/output.$$ | sort -n -t':' -k 3

Still not sorted quite right - is this something wrong with the 'sort' part of the above line?
Last edited by tones1986 : Dec 4th, 2007 at 10:05 pm. Reason: switched line of code ... edit
Reply With Quote  
Join Date: Oct 2007
Posts: 302
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Rep Power: 3
Solved Threads: 37
stilllearning stilllearning is offline Offline
Posting Whiz

Re: BASH script - sales per each associate (3 files)

  #24  
Dec 4th, 2007
The '|' character is a pipe. Basically what it does is send the output of one command which would otherwise go to stdout, in as the input to the next command.

The final sort and print doesn't need to be in your loop. It should be at the end of everything, because now you have your output and you're only wanting to sort and display it.

Assuming your final results are in a file called output.

cat output
displays those results , and you can use a '|' to pipe that into the sort command which is
sort -n -t':' -k 3 
and then pipe the results of the sort into the awk statement for final formatting
awk -F":" '{printf("%3d\t%-20s\t%10.2f\n", $1, $2, $3)}'
The '$' signs are not needed in your printf unless you want them to appear in your output, before a certain field.

Not sure what's causing your loop, but you may want to put some print statements in there to isolate it.

Basically at the end you would have
cat output | sort ..... | awk ...
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Shell Scripting Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Shell Scripting Forum

All times are GMT -4. The time now is 1:49 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC