- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- command / script help please ..
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2001 09:01 AM
10-03-2001 09:01 AM
the fields are
CUSTOMER,DATE TIME FINISHED,DOLLAR AMOUNT
customer_1,2001-10-02 05:49:40,1.98,
customer_1,2001-10-02 05:49:31,1.65,
customer_1,2001-10-02 05:49:28,23.19,
customer_2,2001-10-02 05:48:01,3.12,
customer_2,2001-10-02 05:48:00,13.95,
customer_3,2001-10-02 05:38:48,227.52,
customer_3,2001-10-02 05:38:36,213.25,
customer_4,2001-10-02 05:44:17,22.99,
customer_4,2001-10-02 05:44:44,2.69,
And the the final output I want it to look like this for each customer:
CUSTOMER,DATE,TOTAL AMOUNT
customer_1,2001-10-02,26.76
what I am having problems with is getting the sun for each customer.
Any ideas?
Richard
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2001 09:06 AM
10-03-2001 09:06 AM
Re: command / script help please ..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2001 09:08 AM
10-03-2001 09:08 AM
Re: command / script help please ..
I dont even know where to start to get the sum.
Richard
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2001 09:10 AM
10-03-2001 09:10 AM
Re: command / script help please ..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2001 09:12 AM
10-03-2001 09:12 AM
Re: command / script help please ..
There are more then 4 customer all with differnt but uniqe names.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2001 09:14 AM
10-03-2001 09:14 AM
Re: command / script help please ..
awk, depending upon the data file size, would work just fine for this.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2001 09:19 AM
10-03-2001 09:19 AM
Re: command / script help please ..
richard
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2001 09:48 AM
10-03-2001 09:48 AM
Re: command / script help please ..
You can use ksh to do that :
Read you file with :
while read line
do
# SOME COMMANDS
done < file.name
Now commands :
customer=`echo $line | cut -f1 -d',' | cut -f2 -d'_'`
amount=`echo $line | cut -f3 -d','`
And for sum you can use "bc" :
total=`echo "$total + $amount" | bc`
HTH
Herv?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2001 09:50 AM
10-03-2001 09:50 AM
Solutionawk ' BEGIN { FS=","; OFS=","; dollars = 0; cust = ""; dateof = ""; firsttime =
1 }
{
if ($1 == cust && $2 == dateof ) {
dollars += $4
} else {
if ( firsttime == 0 ) {
print cust, dateof, dollars
}
firsttime = 0
cust = $1
dateof = $2
dollars = $4
}
}
END { print cust, dateof, dollars }'
Note, that there is a "sed" that replaces the space between the date and time to a comma.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2001 09:53 AM
10-03-2001 09:53 AM
Re: command / script help please ..
I don't know the full answer but
cat test | awk -F, '{s+=$3} END {print s}'
this will print total of 3rd field.
Sachin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2001 09:58 AM
10-03-2001 09:58 AM
Re: command / script help please ..
syntax error The source line is 2.
The error context is
BEGIN { FS=","; OFS=","; dollars = 0; cust = ""; dateof = ""; f
irsttime = >>>
<<<
awk: The statement cannot be correctly parsed.
The source line is 2.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2001 10:00 AM
10-03-2001 10:00 AM
Re: command / script help please ..
awk ' {
if ( NF < 2 ) next;
if ( NR == 1 ) {header=$0;next;}
cus=$1;
split($2,array,",");
amount=array[2];
bigA[id]=bigA[id] + amount;
} END {
print header "\n";
for ( i in bigA ) printf("%s,%f\n",i,bigA[i]);
}'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2001 10:04 AM
10-03-2001 10:04 AM
Re: command / script help please ..
Take the script, below, and call it "my.sh". Then, do this:
# /.my.sh ./myinput #...where "myinput" is your data file to process:
#!/usr/bin/sh
awk -F, '{if (TOG==0) {TOG=1;PREV=$1}};
{if ($1==PREV)
{T=T+$3}
else
{print T;PREV=$1;T=$3}}
END {print T}' $1
exit 0
#.end.
...based on your file, the output will be:
26.82
17.07
440.77
25.68
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2001 10:04 AM
10-03-2001 10:04 AM
Re: command / script help please ..
a little typo there. cus = id
change cus=$1 to id=$1
or
bigA[id] becomes bigA[cus]
looks like you need a bit of adjusting for your header also. but i'm sure you can deal with that.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2001 10:05 AM
10-03-2001 10:05 AM
Re: command / script help please ..
Sorry, you need to paste the script into a file and execute it, otherwise you will need to use the "\" for line continuation.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2001 10:12 AM
10-03-2001 10:12 AM
Re: command / script help please ..
I really meant to show (too) the name associated with the total, like this:
customer_1 26.82
customer_2 17.07
customer_3 440.77
customer_4 25.68
So, use this:
#!/usr/bin/sh
awk -F, '{if (TOG==0) {TOG=1;PREV=$1}};
{if ($1==PREV)
{T=T+$3}
else
{print PREV,T;PREV=$1;T=$3}}
END {print PREV,T}' $1
exit 0
#.end.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2001 10:15 AM
10-03-2001 10:15 AM
Re: command / script help please ..
James you read my mind ..
I was fixing to ask you about that.
Now harry and James both of yours work great.
But I would like to know what your script is doing. I know it is doing the job great but as far as the inside of the script can you explain what the functions are doing?
Richard
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2001 10:24 AM
10-03-2001 10:24 AM
Re: command / script help please ..
Try this
-----------
for i in `cat report.txt |grep -v "DATE" |awk -F"," '{print $1}' |sort -u`
{
echo "\nCustomer : " $i
for dollar in `cat report.txt |grep $i |awk -F"," '{sum+=$3} END {print sum}' `
{
echo "Sum of Dollars : $dollar "
}
----
Thanks.
Prashant.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2001 10:25 AM
10-03-2001 10:25 AM
Re: command / script help please ..
You go "back-to-basics". If it's the first time (based on TOG equal to 0) then preserve the first field (awk's $1) in the variable called PREV and set TOG to 1.
Then, test to see if the first field of the input record is the same as PREV. If true, add the value of the third field ($3) to a counter called "T". Otherwise, if false, then the name ($1) changed, so print the name and the (T)otal summation and set the (T)otal to the current value ($3 in the record being processed).
At the "END", simply flush the name field (PREV) and the current (T)otal.
Remember that outside of the tick marks surrounding 'awk', the "$1" is the file passed to the script -- as the first positional parameter.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2001 10:33 AM
10-03-2001 10:33 AM
Re: command / script help please ..
Oh, I forgot to mention in my explanation of how the script works, that the '-F,' set the interfield separator for 'awk' to the comma character. It was clear from your sample data that that was the field delimiter you chose.
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-04-2001 12:00 AM
10-04-2001 12:00 AM
Re: command / script help please ..
This was uses associative arrays, making it relatively short (& you get the date):
sed 's/ [^,]*//' yourfile |
awk -F, '{array[$1","$2]+=$3}
END{for ( a in array ) print a","array[a]}' |
sort
customer_1,2001-10-02,26.82
customer_2,2001-10-02,17.07
customer_3,2001-10-02,440.77
customer_4,2001-10-02,25.68
The sed is used to simply strip out the time.
Rgds, Robin.