- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Scripting help
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
01-17-2006 01:19 AM
01-17-2006 01:19 AM
I have a file containing two columns.
Column 1 = text
Column 2 = numbers
I wish to read the file a line at a time and as long as the value of column 1 is the same, add the values of column 2 together.
Col 1 contains hostnames, so as long as the hostname remains the same, add col 2 together.
Im getting a little stuck on the scripting for this.
Thanks
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2006 01:29 AM
01-17-2006 01:29 AM
Re: Scripting help
Please, shed more light. Do you mean we should apend col 2 to 1 after?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2006 01:32 AM
01-17-2006 01:32 AM
Re: Scripting help
it a flat file containing information such as
column 1 column 2
server1 10
server1 15
server1 20
server1 40
server2 19
server2 19
In this example, I would like to read the file line by line and as long as column 1 is equal to server1 add the numbers from column 2 making a total of 85 in this example. I could then write server1 and the total figure to another file for example.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2006 01:40 AM
01-17-2006 01:40 AM
Re: Scripting help
no doubt the perl experts are going to produce a one-liner!
However:
#!/usr/bin/sh
oldhost=""
while read host number
do
if [ "$host" = "$oldhost" ]
then
counted=`expr $counted + $number`
else
echo $oldhost $counted
oldhost=$host
counted=$number
fi
done < a.lis
Should work as long as there is one additional empty line at the end of the a.lis input file.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2006 01:42 AM
01-17-2006 01:42 AM
Re: Scripting help
# perl -nle'sub z{print"$p:$s";$s=0}($h,$n)=(/^\s*(\S+)\s+(\d+)/);$h eq$p or z;$p=$h;$s+=$n}END{z' file
Demo:
lt09:/home/merijn 101 > cat >xx.txt
a 1
a 4
a 1
b 2
b 2
c 7
d 8
d 0
lt09:/home/merijn 102 > perl -nle'sub z{print"$p:$s";$s=0}($h,$n)=(/^\s*(\S+)\s+(\d+)/);$h eq$p or z;$p=$h;$s+=$n}END{z' xx.txt
:
a:6
:0
b:4
c:7
d:8
lt09:/home/merijn 103 >
Enjoy, Have FUN! H.Merijn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2006 01:45 AM
01-17-2006 01:45 AM
Re: Scripting help
# perl -nalF -e '$prev=$F[0] if !defined($prev);if ($F[0] eq $prev) {$i++} else {pr
int "$prev=$i";$i=1};$prev=$F[0];END{print "$prev=$i"}' file
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2006 01:48 AM
01-17-2006 01:48 AM
Solution# cat testscript
#!/usr/bin/sh
ITER=1
NUMTOT=0
while read HOST NUM
do
if [[ ${HOSTB} = ${HOST} || ${ITER} = 1 ]] ; then
((NUMTOT=$NUMTOT+$NUM))
HOSTB=$HOST
ITER=2
else
echo "The total for ${HOSTB} is ${NUMTOT}"
NUMTOT=0
((NUMTOT=$NUMTOT+$NUM))
HOSTB=$HOST
fi
done < testfile
echo "The total for ${HOSTB} is ${NUMTOT}"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2006 01:57 AM
01-17-2006 01:57 AM
Re: Scripting help
#!/opt/perl/bin/perl -w
my %hash = ();
my $file_in = "/tmp/file_in";
my $file_out = "/tmp/file_out";
open (IN, "<$file_in" ) or die "cannot open file $file_in";
open (OUT, ">$file_out") or die "cannot open file $file_out";
while (my $line =
chomp($line);
my ($host, $num) = split(/\s+/,$line);
print "$host\n";
$hash{$host}[0] += $num;
}
foreach my $key (sort keys %hash){
print OUT "$key $hash{$key}[0]\n";
}
Regards,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2006 01:59 AM
01-17-2006 01:59 AM
Re: Scripting help
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2006 02:00 AM
01-17-2006 02:00 AM
Re: Scripting help
Have tested some and they look fine.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2006 02:01 AM
01-17-2006 02:01 AM
Re: Scripting help
obviously
/tmp/file_in is:
server1 10
server1 15
server1 20
server1 40
server2 19
server2 19
/tmp/file_out is:
server1 85
server2 38
Regards,