- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: awk problem...any hints??
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
07-03-2006 01:17 AM
07-03-2006 01:17 AM
I have fileA:
value1 1
value2 3
value3 4
value1 9
value2 5
value5 5
value1 2
value4 1
...
..
...
..
I wanted file like this:
fileB:
value1 12 3
value2 3 2
value3 4 4
value4 1 1
value5 5 1
where I do not have douplicates anymore and second column is sum of values for each value and 3. column is number each same record shows in file
for example in fileA 3 times value1 records is shown..
vale2 is 2 times shown in fileA and 2 solumn in fileB meand that total values for value1 is 12
any hints?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-03-2006 01:33 AM
07-03-2006 01:33 AM
Re: awk problem...any hints??
untested
Enjoy, Have FUN! H.Merijn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-03-2006 01:35 AM
07-03-2006 01:35 AM
Re: awk problem...any hints??
I am newbie here..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-03-2006 01:59 AM
07-03-2006 01:59 AM
Solutionlt09:/home/merijn 106 > perl -nle'/^(\S+)\s+(\S+)/&&push@{$v{$1}},$2;}END{print"$_:@{$v{$_}}"for sort keys%v' fileA
value1:1 9 2
value2:3 5
value3:4
value4:1
value5:5
lt09:/home/merijn 107 >
My awk foo lets me down on this, but lemme try to explain what I did
> perl -nle
-n process for every line, but do not print
-l add a newline to every printed line
-e the `script' expression
> /^(\S+)\s+(\S+)/
match the line as
^ start of line
(\S+) non-whitespace (as many as matched. The parens capture the result in $1 (e.g. value1)
\s+ whitespace
(\S+) non-whitespace (e.g. 12)
> && push @{$v{$1}}, $2;
if it matches, push the second arg $2 onto a list grouped in hash %v (which is just a short arbitrary name) indexed with the value from $1
> } END {
That is a bit mean. You should know about how perl invokes with -n and -p to use this, but effectively, the next block is only invoked after all input is processed
> print "$_:@{$v{$_}}" for sort keys%v
the keys of the hash are the values found, which we iterate over with for keys %v
Those keys are processed by sort and then used in the print
the print prints the key (the value from fileA), and the list of numebrs collected in the process after that. The calling -l option will add a newline
Hopes that might motivate you to use perl more often
Enjoy, Have FUN! H.Merijn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-03-2006 04:31 AM
07-03-2006 04:31 AM
Re: awk problem...any hints??
Here's an awk script I wrote that totals a day's worth of sessions from Oracle's listener.log (after it's run through 'sed'). It it very similar to what you're looking for, and should be relatively easy to modify:
# more acc.awk
BEGIN {
FS=","
hr=-1
d=-1
ptr=0
}
{
if (hr==-1 || hr==$2)
{
if (nb[$3]==null)
{
na[ptr]=$3
ptr++
}
nb[$3]++
}
else
{
printf("********* %d:00 to %d:59 on %s *********\n",hr,hr,d)
for (i=0; i
printf("%30s\t\t%d\n",na[i],nb[na[i]])
}
for (i in na)
delete na[i]
for (i in nb)
delete nb[i]
ptr=0
}
d=$1
hr=$2
}
END {
printf("********* %d:00 to %d:59 on %s *********\n",hr,hr,$1)
for (i=0; i
printf("%30s\t\t%d\n",na[i],nb[na[i]])
}
}
It gives output of the form:
********* 6:00 to 6:59 on 03-JUL *********
user2 7
oracle 14
user1 10
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-03-2006 05:24 AM
07-03-2006 05:24 AM
Re: awk problem...any hints??
# awk '{x[$1]++;z[$1]+=$2} END{for(i in x) print i,z[i],x[i]}' fileA > fileB
~cheers
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-03-2006 07:14 PM
07-03-2006 07:14 PM
Re: awk problem...any hints??
cheers,