He can simply ignore me if he likes.
# Create a hash whose elements have a default value
# of 0.
BEGIN { $h=Hash.new(0) }
# If the line just read, $_, doesn't begin with
# "JOE", skip it.
next if $_ !~ /^JOE/
# Each iteration of scan produces an array like this:
# [n1,n2]; n1 is the number...
ruby -n sums.rb data
BEGIN { $h=Hash.new(0) }
next if $_ !~ /^JOE/
$_.scan(/(\d+):(\d+)/){|x| $h[x[0].to_i]+=x[1].to_i}
END { puts $h.sort.map{|x| x.join(':')}.join(',') }
while ($i++ <= 5)
It's $i++ not ++$i.
This means that the value of $i is fetched before
$i is incremented. But $i will be incremented before
the print statement. So the equivalent is
$i = 0;
while ($i <= 5)
{ $i++;
print $i;
}
and the output will be
123456
b{1,3} will match "b" or "bb" or...
244000,106685045,"$7,605.40",Dell Australia Pty Ltd,"Hi, Mom!"
244000,106685045,"$1,777,605.40",Dell Australia Pty Ltd,
244000,106685045,"$9,111,777,605.40",Dell Australia Pty Ltd,
becomes
244000,106685045,"$7605.40",Dell Australia Pty Ltd,"Hi Mom!"
244000,106685045,"$1777605.40",Dell...
gsub(/,/, "", currency)
And here's an improved csv parser in Awk. The only thing it doesn't handle is records that contain linefeeds.
{
parse_csv( $0, rec )
printf "["
sep = ""
for (i=1;i in rec; i++)
{ printf "%s<%s>", sep, rec[i]
sep = ", "
}
print "]"
}
function...
Let's say you run the program with
awk -f prog.awk myfile1 myfile2
ARGV is now
["awk", "myfile1", "myfile2"]
BEGIN { file2 = ARGV[2]
file2 now is "myfile2"
ARGC--
ARGV is now, in effect,
["awk", "myfile1"]
while ( getline
Read line from "myfile1" and put it in $0.
+...
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.