Unix Shell - Which tool for this report?

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > August 2007 > Which tool for this report?





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Which tool for this report?
Michael DeBusk

2007-08-26, 1:20 am

After some messing about with grep, awk, and sed, I've been able to
reduce a rather lengthy and cluttered report to the following (snipped
for brevity, as there are 196 lines in the current output and the final
output will have almost 1500):

Complete 10445
Complete 10732
Complete 11468
Complete 11304
Complete 10233
Complete 11144
Complete 11574
Incomplete 11304
Complete 11304
Complete 10732
Incomplete 10233
Complete 10507
Complete 10732
Incomplete 10233
Complete 11468
Complete 11468
Complete 10507
Incomplete 10732

The last thing for me to do is get it to look like this:

Code Complete Incomplete
10445 1 0
10732 5 1
11468 5 0
10233 4 2

....and so on.

What would be the best tool to use for this task?

Thank you.

--
Registered Linux User #450983 * Ubuntu Counter Project number #10548

The "mypacks.net" address from which this message was sent is
legitimate and not spam-trapped. It is, however, disposable.
Alan Curry

2007-08-26, 7:17 am

In article <13d26ej6osbj35a@corp.supernews.com>,
Michael DeBusk <chinos6398@mypacks.net> wrote:
>After some messing about with grep, awk, and sed, I've been able to
>reduce a rather lengthy and cluttered report to the following (snipped
>for brevity, as there are 196 lines in the current output and the final
>output will have almost 1500):


I'm snipping it even more...

>Complete 11468
>Complete 11468
>Complete 10507
>Incomplete 10732
>
>The last thing for me to do is get it to look like this:
>
>Code Complete Incomplete
>10445 1 0
>10732 5 1
>11468 5 0
>10233 4 2


Here's a PERL script. It could be done roughly in a one-liner, but I
generalized it and commented it to make it understandable. Give it your input
file on stdin, or as a file named on the command line.

#!/usr/bin/perl -W
use strict;

# %x is the matrix of values we'll be outputting.
# %hd is used to build up a complete list of all the column-heading keywords
# (in the example data, these are "Complete" and "Incomplete")
# $maxlen{foo} tracks the maximum length of any item in column foo.
my (%x, %hd, %maxlen);

# The first column heading doesn't come from the input, so it's hardcoded
# here, in only one place for easy modification
my $codeheading='Code';

$maxlen& #123;$codeheading}=length($codeheading);


# The input is read in a single pass, adding each line's contributions
# to %x, %hd, and %maxlen
while(<> ) {
chomp;
my ($hd, $code) = split /\s+/, $_;
my $val=++$x{$code}{$hd};
$hd{$hd}=1;
$maxlen{$hd}=length($hd) if $val==1;
$maxlen{$hd}=length($val) if length($val) > $maxlen{$hd};
$maxlen{$codeheading}=length($code) if length($code) > $maxlen{$codeheading};
}

# %hd is now completely built; its keys are the column headings. This sorts
# them alphabetically so "Complete" will be left of "Incomplete".
my @hd=sort keys %hd;

# Build up a dynamic printf format string in which each column has just
# enough width to hold its widest entry, and there are 2 spaces between
# columns.
my $fmt=join ' ', map { '%'.$maxlen{$_}.'s' } $codeheading, @hd;
$fmt =~ s/%/%-/; # The first column ("Code") is left-justified
$fmt.="\n";
# $fmt now looks something like "%-5s %8s %10s\n"

# Print the header
printf $fmt, $codeheading, @hd;

# Print the matrix of values. You might want the Code column sorted
# numerically instead of lexically, in which case you should use
# sort {$a<=>$b} keys %x
for (sort keys %x) {
printf $fmt, $_, map {$_||0} @{$x{$_}}{@hd};
}
__END__

--
Alan Curry
pacman@world.std.com
John W. Krahn

2007-08-26, 7:17 am

Michael DeBusk wrote:
> After some messing about with grep, awk, and sed, I've been able to
> reduce a rather lengthy and cluttered report to the following (snipped
> for brevity, as there are 196 lines in the current output and the final
> output will have almost 1500):
>
> Complete 10445
> Complete 10732
> Complete 11468
> Complete 11304
> Complete 10233
> Complete 11144
> Complete 11574
> Incomplete 11304
> Complete 11304
> Complete 10732
> Incomplete 10233
> Complete 10507
> Complete 10732
> Incomplete 10233
> Complete 11468
> Complete 11468
> Complete 10507
> Incomplete 10732
>
> The last thing for me to do is get it to look like this:
>
> Code Complete Incomplete
> 10445 1 0
> 10732 5 1
> 11468 5 0
> 10233 4 2


$ echo "Complete 10445
Complete 10732
Complete 11468
Complete 11304
Complete 10233
Complete 11144
Complete 11574
Incomplete 11304
Complete 11304
Complete 10732
Incomplete 10233
Complete 10507
Complete 10732
Incomplete 10233
Complete 11468
Complete 11468
Complete 10507
Incomplete 10732" | \
perl -ne'
$x{ $2 }{ $1 }++ if /^([IC])(?:nc|)omplete\s+(\d+)/
}{
print "Code Complete Incomplete\n";
printf "%-6s%9d%12d\n", $_, @{ $x{ $_ } }{ "C", "I" } for keys %x
'
Code Complete Incomplete
10233 1 2
10445 1 0
10507 2 0
11574 1 0
11304 2 1
10732 3 1
11144 1 0
11468 3 0




John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
William James

2007-08-26, 7:17 am

On Aug 26, 1:12 am, Michael DeBusk <chinos6...@mypacks.net> wrote:
> After some messing about with grep, awk, and sed, I've been able to
> reduce a rather lengthy and cluttered report to the following (snipped
> for brevity, as there are 196 lines in the current output and the final
> output will have almost 1500):
>
> Complete 10445
> Complete 10732
> Complete 11468
> Complete 11304
> Complete 10233
> Complete 11144
> Complete 11574
> Incomplete 11304
> Complete 11304
> Complete 10732
> Incomplete 10233
> Complete 10507
> Complete 10732
> Incomplete 10233
> Complete 11468
> Complete 11468
> Complete 10507
> Incomplete 10732
>
> The last thing for me to do is get it to look like this:
>
> Code Complete Incomplete
> 10445 1 0
> 10732 5 1
> 11468 5 0
> 10233 4 2
>
> ...and so on.
>
> What would be the best tool to use for this task?
>
> Thank you.



#!ruby
hash = Hash.new{ 0 }
ARGF.each{|line| hash[ line.split ] += 1 }
format = "%-6s%9s%12s\n"
printf format, 'Code', 'Complete', 'Incomplete'
hash.keys.map{|a| a.last}.uniq.sort.each{|code|
printf format, code, hash[['Complete',code]],
hash[['Incomplete',code]]
}

=====
Output:

Code Complete Incomplete
10233 1 2
10445 1 0
10507 2 0
10732 3 1
11144 1 0
11304 2 1
11468 3 0
11574 1 0

Cyrus Kriticos

2007-08-26, 7:17 am

Michael DeBusk wrote:
> [...]
>
> The last thing for me to do is get it to look like this:
>
> Code Complete Incomplete
> 10445 1 0
> 10732 5 1
> 11468 5 0
> 10233 4 2


A solution with bash:

--- cut here ---
#!/bin/bash

FILE="complete.dat"

read_file ()
{
while read word number; do
case $word in
Complete)
let c[$number]=c[$number]+1
if [ -z "${i[number]}" ]; then i[$number]=0; fi
;;
Incomplete)
let i[$number]=i[$number]+1
if [ -z "${c[number]}" ]; then c[$number]=0; fi
;;
esac
done < "$FILE"
}

dump_arrays ()
{
echo "Code Complete Incomplete"
for i in ${!c[@]}; do # yeah
printf "%5d %9s %11s\n" $i ${c[$i]} ${i[$i]}
done
}

read_file
dump_arrays
--- cut here ---

--
Best | "Was bekommt man/frau, wenn man/frau Software kauft?
regards | Nichts außer einem Haufen Nullen und Einsen."
Cyrus | -- aus d. Lizenzvereinbarung von Spybot Search&Destroy
Cyrus Kriticos

2007-08-26, 7:17 am

Cyrus Kriticos wrote:
> Michael DeBusk wrote:
>
> A solution with bash:
> [...]


few changes:
- changed printf format and removed one if
- replaced variables $word and $i

--- cut here ---
#!/bin/bash

FILE="complete.dat"

read_file ()
{
while read code number; do
case $code in
Complete)
let c[$number]=c[$number]+1
;;
Incomplete)
let i[$number]=i[$number]+1
if [ -z "${c[number]}" ]; then c[$number]=0; fi
;;
esac
done < "$FILE"
}

dump_arrays ()
{
echo "Code Complete Incomplete"
for j in ${!c[@]}; do # yeah
printf "%5d %9d %11d\n" $j ${c[$j]} ${i[$j]}
done
}

read_file
dump_arrays
--- cut here ---

--
Best | "Was bekommt man/frau, wenn man/frau Software kauft?
regards | Nichts außer einem Haufen Nullen und Einsen."
Cyrus | -- aus d. Lizenzvereinbarung von Spybot Search&Destroy
loki harfagr

2007-08-26, 7:17 am

On Sun, 26 Aug 2007 06:12:35 +0000, Michael DeBusk wrote:

> After some messing about with grep, awk, and sed, I've been able to
> reduce a rather lengthy and cluttered report to the following (snipped
> for brevity, as there are 196 lines in the current output and the final
> output will have almost 1500):
>
> Complete 10445
> Complete 10732
> Complete 11468
> Complete 11304
> Complete 10233
> Complete 11144
> Complete 11574
> Incomplete 11304
> Complete 11304
> Complete 10732
> Incomplete 10233
> Complete 10507
> Complete 10732
> Incomplete 10233
> Complete 11468
> Complete 11468
> Complete 10507
> Incomplete 10732
>
> The last thing for me to do is get it to look like this:
>
> Code Complete Incomplete
> 10445 1 0
> 10732 5 1
> 11468 5 0
> 10233 4 2
>
> ...and so on.
>
> What would be the best tool to use for this task?
>
> Thank you.



As you say you made the first part in awk why not
keeping it for the rest?
Here's a funny way to try it (use a mantissa bigger than 10K
if you grow up in number of lines):

awk '
$1=="Complete"{ v[$2]+=10000 }
$1=="Incomplete"{v[$2]++}
END{ printf("%-10s%15s%15s\n","Code","Complete","Incomplete");
for(i in v){
printf("%-10s%15d%15d\n",i,int(v[i]/10000),v[i]%10000)
}
}' yourfile
Cyrus Kriticos

2007-08-26, 7:17 am

Cyrus Kriticos wrote:
> Michael DeBusk wrote:
>
> A solution with bash:
> [...]


few changes:
- changed printf format and removed one if
- replaced variables $word and $i

--- cut here ---
#!/bin/bash

FILE="complete.dat"

read_file ()
{
while read code number; do
case $code in
Complete)
let c[$number]=c[$number]+1
;;
Incomplete)
let i[$number]=i[$number]+1
if [ -z "${c[number]}" ]; then c[$number]=0; fi
;;
esac
done < "$FILE"
}

dump_arrays ()
{
echo "Code Complete Incomplete"
for j in ${!c[@]}; do # yeah
printf "%-5d %9d %11d\n" $j ${c[$j]} ${i[$j]}
done
}

read_file
dump_arrays
--- cut here ---

--
Best | "Was bekommt man/frau, wenn man/frau Software kauft?
regards | Nichts außer einem Haufen Nullen und Einsen."
Cyrus | -- aus d. Lizenzvereinbarung von Spybot Search&Destroy
Cyrus Kriticos

2007-08-26, 7:17 am

Cyrus Kriticos wrote:
> Michael DeBusk wrote:
>
> A solution with bash:
> [...]


few changes:
- changed wrong printf format and removed one if
- replaced variables $number and $i

Greetings to Ed Morton ;)

--- cut here ---
#!/bin/bash

FILE="complete.dat"

read_file ()
{
while read word code; do
case $word in
Complete)
let c[$code]=c[$code]+1
;;
Incomplete)
let i[$code]=i[$code]+1
if [ -z "${c[code]}" ]; then c[$code]=0; fi
;;
esac
done < "$FILE"
}

dump_arrays ()
{
echo "Code Complete Incomplete"
for j in ${!c[@]}; do # yeah
printf "%-5d %9d %11d\n" $j ${c[$j]} ${i[$j]}
done
}

read_file
dump_arrays
--- cut here ---

--
Best | "Was bekommt man/frau, wenn man/frau Software kauft?
regards | Nichts außer einem Haufen Nullen und Einsen."
Cyrus | -- aus d. Lizenzvereinbarung von Spybot Search&Destroy
Ed Morton

2007-08-26, 1:20 pm

Michael DeBusk wrote:
> After some messing about with grep, awk, and sed, I've been able to
> reduce a rather lengthy and cluttered report to the following (snipped
> for brevity, as there are 196 lines in the current output and the final
> output will have almost 1500):
>
> Complete 10445
> Complete 10732
> Complete 11468
> Complete 11304
> Complete 10233
> Complete 11144
> Complete 11574
> Incomplete 11304
> Complete 11304
> Complete 10732
> Incomplete 10233
> Complete 10507
> Complete 10732
> Incomplete 10233
> Complete 11468
> Complete 11468
> Complete 10507
> Incomplete 10732
>
> The last thing for me to do is get it to look like this:
>
> Code Complete Incomplete
> 10445 1 0
> 10732 5 1
> 11468 5 0
> 10233 4 2
>
> ...and so on.
>
> What would be the best tool to use for this task?
>
> Thank you.
>


awk '{codes[$2]} /^I/{inc[$2]++;next} {cmp[$2]++}
END { print "Code\tComplete\tIncomplete"
for (c in codes) printf "%s\t%d\t%d\n",c,cmp[c],inc[c]
}' file
Michael DeBusk

2007-08-27, 1:20 am

On 26 Aug 2007 10:09:56 GMT, loki harfagr <loki@DarkDesign.free.fr> wrote:

> As you say you made the first part in awk why not keeping it for the
> rest?


Because what I know about awk could fit in a thimble and still leave
plenty of room for your finger. I do find it intriguing, though, and
it may be the first Linux-y language I learn... when I get the time.

--
Registered Linux User #450983 * Ubuntu Counter Project number #10548

The "mypacks.net" address from which this message was sent is
legitimate and not spam-trapped. It is, however, disposable.
Michael DeBusk

2007-08-27, 1:20 am

On Sun, 26 Aug 2007 10:44:03 -0500, Ed Morton <morton@lsupcaemnt.com> wrote:

> awk '{codes[$2]} /^I/{inc[$2]++;next} {cmp[$2]++}
> END { print "Code\tComplete\tIncomplete"
> for (c in codes) printf "%s\t%d\t%d\n",c,cmp[c],inc[c]
> }' file


This is quite impressive. I re-cast it as a one-liner and appended it to
the mess of grep, sed, and awk I'd already thrown together, and it
worked beautifully. Thank you kindly.

Thanks to everyone else who answered, too. You gave me much to think
about in my quest to learn these things.

Must investigate awk more deeply...

--
Registered Linux User #450983 * Ubuntu Counter Project number #10548

The "mypacks.net" address from which this message was sent is
legitimate and not spam-trapped. It is, however, disposable.
hymie!

2007-08-27, 7:23 am

In our last episode, the evil Dr. Lacto had captured our hero,
Michael DeBusk <chinos6398@mypacks.net>, who said:
>After some messing about with grep, awk, and sed, I've been able to
>reduce a rather lengthy and cluttered report to the following (snipped
>for brevity, as there are 196 lines in the current output and the final
>output will have almost 1500):
>
>Complete 10445
>Complete 10732
>Complete 11468
>Incomplete 10233


[...]

>The last thing for me to do is get it to look like this:
>
>Code Complete Incomplete
>10445 1 0
>10732 5 1
>11468 5 0
>10233 4 2
>
>What would be the best tool to use for this task?


Not only is PERL probably the best tool for this task, you can probably
go back a step, re-write your original sed/grep/awk into perl, and save
yourself some time and effort.

--hymie! http://lactose.homelinux.net/~hymie hymie@lactose.homelinux.net
------------------------ Without caffeine for 300 days ------------------------
Glenn Jackman

2007-08-27, 1:25 pm

At 2007-08-27 01:24AM, "Michael DeBusk" wrote:
> On Sun, 26 Aug 2007 10:44:03 -0500, Ed Morton <morton@lsupcaemnt.com> wrote:
>
>
> This is quite impressive. I re-cast it as a one-liner and appended it to
> the mess of grep, sed, and awk I'd already thrown together, and it
> worked beautifully. Thank you kindly.
>
> Thanks to everyone else who answered, too. You gave me much to think
> about in my quest to learn these things.
>
> Must investigate awk more deeply...


I'd encourage you to continue. I have no doubt that you can replace the
entire "mess of grep, sed, and awk" with a single awk script.


--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry
Michael DeBusk

2007-08-29, 7:17 am

On Mon, 27 Aug 2007 07:00:22 -0500, hymie!
<hymie_@_lactose.homelinux.net> wrote:

> Not only is PERL probably the best tool for this task, you can
> probably go back a step, re-write your original sed/grep/awk into
> perl, and save yourself some time and effort.


Not counting the time it would take to develop a reasonable mastery of
perl, I imagine you're correct.

--
Registered Linux User #450983 * Ubuntu Counter Project number #10548

The "mypacks.net" address from which this message was sent is
legitimate and not spam-trapped. It is, however, disposable.
Michael DeBusk

2007-08-29, 7:17 am

On 27 Aug 2007 13:30:36 GMT, Glenn Jackman <glennj@ncf.ca> wrote:

>
> I'd encourage you to continue. I have no doubt that you can replace
> the entire "mess of grep, sed, and awk" with a single awk script.


I have no doubt as well. Would you mind sharing with me how one can use
awk to get a line with a particular word at position one, skip a line,
and get the next? (That's how the original report looks.) I'm not able
to figure it out from the man page and am just starting to make any
sense at all of simple regular expressions.

Thanks much.

--
Registered Linux User #450983 * Ubuntu Counter Project number #10548

The "mypacks.net" address from which this message was sent is
legitimate and not spam-trapped. It is, however, disposable.
Maxwell Lol

2007-08-29, 7:17 am

Michael DeBusk <chinos6398@mypacks.net> writes:

>Would you mind sharing with me how one can use
> awk to get a line with a particular word at position one, skip a line,
> and get the next? (That's how the original report looks.) I'm not able
> to figure it out from the man page and am just starting to make any
> sense at all of simple regular expressions.


Think of awk as a mini-C interpretor.

#!/usr/bin/awk -f
BEGIN {found=0}
{
# printf("%d: %s\n",found,$0);
if (found==0) {
if ($1 ~ /START/) {
found++; # Found a START word
}
} else if (found == 1) {
found++; # Found the line after START
} else if (found == 2) {
print $0; # Print the second line after START
found=0; # reset
}
}
Ed Morton

2007-08-29, 1:20 pm

Maxwell Lol wrote:
> Michael DeBusk <chinos6398@mypacks.net> writes:
>
>
>
>
> Think of awk as a mini-C interpretor.


No, don't. Thinking of awk as C is how you miss many of awks benefits.

> #!/usr/bin/awk -f
> BEGIN {found=0}
> {
> # printf("%d: %s\n",found,$0);
> if (found==0) {
> if ($1 ~ /START/) {
> found++; # Found a START word
> }
> } else if (found == 1) {
> found++; # Found the line after START
> } else if (found == 2) {
> print $0; # Print the second line after START
> found=0; # reset
> }
> }


Here's how you'd really do it:

awk 'c&&!--c;$1~/START/{c=2}' file

Notice that with this idiomatic awk solution, as well as being more
concise than the idiomatic C solution above it, it's more easily
extensible to skipping any number of records after the pattern is found
just by changing "2" to whatever number you like.

Regards,

Ed.
Michael DeBusk

2007-08-30, 7:20 am

On Wed, 29 Aug 2007 09:12:58 -0500, Ed Morton <morton@lsupcaemnt.com> wrote:

> Here's how you'd really do it:
>
> awk 'c&&!--c;$1~/START/{c=2}' file


This is extremely cool. I almost understand it. I'm not yet making sense
of the part between the first single quote and the semicolon. What does
"c" represent? "Man awk" refers to "c" as a regexp matching any
non-metacharacter; is that it? If so, what are the "&&" (logical and?)
and the "!--" (not decrement?) doing?

Incidentally, the above gives me the line after the line after the line
it matches, but it doesn't give me the matching line. I realize I was
unclear, and I'm sorry for the ambiguity.

--
Registered Linux User #450983 * Ubuntu Counter Project number #10548

The "mypacks.net" address from which this message was sent is
legitimate and not spam-trapped. It is, however, disposable.
mik3l3374@gmail.com

2007-08-30, 7:20 am

On Aug 26, 2:12 pm, Michael DeBusk <chinos6...@mypacks.net> wrote:
> After some messing about with grep, awk, and sed, I've been able to
> reduce a rather lengthy and cluttered report to the following (snipped
> for brevity, as there are 196 lines in the current output and the final
> output will have almost 1500):
>
> Complete 10445
> Complete 10732
> Complete 11468
> Complete 11304
> Complete 10233
> Complete 11144
> Complete 11574
> Incomplete 11304
> Complete 11304
> Complete 10732
> Incomplete 10233
> Complete 10507
> Complete 10732
> Incomplete 10233
> Complete 11468
> Complete 11468
> Complete 10507
> Incomplete 10732
>
> The last thing for me to do is get it to look like this:
>
> Code Complete Incomplete
> 10445 1 0
> 10732 5 1
> 11468 5 0
> 10233 4 2
>
> ...and so on.
>
> What would be the best tool to use for this task?
>
> Thank you.
>
> --
> Registered Linux User #450983 * Ubuntu Counter Project number #10548
>
> The "mypacks.net" address from which this message was sent is
> legitimate and not spam-trapped. It is, however, disposable.


if you have Python and able to use it as an alternative

#!/usr/bin/python
d={}
for line in open("file"):
.....status,code=line.split()
.....d.setdefault(code,[])
.....d[code].append(status)
print "Code\tComplete\tIncomplete"
for i,j in d.iteritems():
.....print "%s\t%d\t%d" % (i,
j.count("Complete"),j.count("Incomplete"))

Ed Morton

2007-08-30, 7:20 am

Michael DeBusk wrote:

> On Wed, 29 Aug 2007 09:12:58 -0500, Ed Morton <morton@lsupcaemnt.com> wrote:
>
>
>
>
> This is extremely cool. I almost understand it. I'm not yet making sense
> of the part between the first single quote and the semicolon. What does
> "c" represent? "Man awk" refers to "c" as a regexp matching any
> non-metacharacter; is that it? If so, what are the "&&" (logical and?)
> and the "!--" (not decrement?) doing?


"c" means "counter". It's just a variable. This:

$1~/START/{c=2}

means "when START appears in the first field, set the counter c to 2". This:

c&&!--c;

means "if c is non-zero, decrement c and if the result is zero (i.e. c
was equal to 1) then invoke the default action of printing the current
record".

So, when the script starts "c" is zero so that condition will not be
true. The first time "START" appears in the first field, "c" will get
set to "2". On the next record, "c" will be 2 so the "c is non-zero" is
true but after decrementing c to 1 it's still not zero so the second
part of the condition is false, so the whole condition is false and so
the default action of printing the current record will not be invoked.
On the subsequent record, c is now 1 so "c is non-zero" and the result
of decrementing c IS zero, so that record will be printed.

Keep in mind that an awk script is composed of:

<condition> { <action> }

segments and either the condition or the action can be left unspecified
in which case a default is applied. The default condition is "true"
while the default action is "print $0".

> Incidentally, the above gives me the line after the line after the line
> it matches, but it doesn't give me the matching line. I realize I was
> unclear, and I'm sorry for the ambiguity.
>


No problem, just add a print:

awk 'c&&!--c;$1~/START/{print;c=2}' file

Regards,

Ed.
Michael DeBusk

2007-08-30, 1:20 pm

On Thu, 30 Aug 2007 01:21:59 -0700, mik3l3374@gmail.com
<mik3l3374@gmail.com> wrote:

> if you have Python and able to use it as an alternative


I do, and I want to learn it eventually (that makes it sound farther
away in time than I really intend), but I'm finding awk rather
fascinating right now... especially the way Ed is using it.

--
Registered Linux User #450983 * Ubuntu Counter Project number #10548

The "mypacks.net" address from which this message was sent is
legitimate and not spam-trapped. It is, however, disposable.
Michael DeBusk

2007-08-30, 1:20 pm

On Thu, 30 Aug 2007 07:01:27 -0500, Ed Morton <morton@lsupcaemnt.com> wrote:

> c&&!--c;
>
> means "if c is non-zero, decrement c and if the result is zero (i.e.
> c was equal to 1) then invoke the default action of printing the
> current record".


So, in awk, when a variable is not otherwise set, it's zero?

> So, when the script starts "c" is zero so that condition will not be
> true. The first time "START" appears in the first field, "c" will get
> set to "2". On the next record, "c" will be 2 so the "c is non-zero"


Thank you for this explanation. It makes sense now.

> No problem, just add a print:
>
> awk 'c&&!--c;$1~/START/{print;c=2}' file


Heh... I thought so, and I tried this, but I forgot the semicolon.

> Regards,
>
> Ed.


I deeply appreciate your guidance. Thanks again.

--
Registered Linux User #450983 * Ubuntu Counter Project number #10548

The "mypacks.net" address from which this message was sent is
legitimate and not spam-trapped. It is, however, disposable.
Ed Morton

2007-08-30, 7:21 pm

Michael DeBusk wrote:
> On Thu, 30 Aug 2007 07:01:27 -0500, Ed Morton <morton@lsupcaemnt.com> wrote:
>
>
>
>
> So, in awk, when a variable is not otherwise set, it's zero?


It's zero if it's used as a number, the null string if it's used as a
string.

Ed.
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com