script doubt

script doubt

am 22.01.2008 20:45:41 von apogeusistemas

Hi:


Can You tell why runnig this script I cannot
get output only in one line ?

# cat xu
month=`head -2 x.lis | awk '{print $6}' | cut -d'/' -f1`
year=`head -2 x.lis | awk '{print $6}' | cut -d'/' -f3`

echo "month: $month - year: $year"

# head x.lis

SunOS dvfsmlum001 5.8 Generic_117350-12 sun4u 12/02/07

00:00:01 %usr %sys %wio %idle
00:05:00 20 5 10 65
00:10:00 31 8 8 53
00:15:00 21 6 5 68
00:20:00 15 5 4 77
00:25:00 15 3 1 81
00:30:00 13 2 1 84
# ./xu
month:
12 - year:
07


Thank You in advance...

Re: script doubt

am 22.01.2008 21:07:22 von Cyrus Kriticos

apogeusistemas@gmail.com wrote:
> Hi:
>
>
> Can You tell why runnig this script I cannot
> get output only in one line ?
>
> # cat xu
> month=`head -2 x.lis | awk '{print $6}' | cut -d'/' -f1`
> year=`head -2 x.lis | awk '{print $6}' | cut -d'/' -f3`


head -2 x.lis | tail -1 | awk ...


> echo "month: $month - year: $year"
>
> # head x.lis
>
> SunOS dvfsmlum001 5.8 Generic_117350-12 sun4u 12/02/07
>
> 00:00:01 %usr %sys %wio %idle
> 00:05:00 20 5 10 65
> 00:10:00 31 8 8 53
> 00:15:00 21 6 5 68
> 00:20:00 15 5 4 77
> 00:25:00 15 3 1 81
> 00:30:00 13 2 1 84
> # ./xu
> month:
> 12 - year:
> 07

--
Best regards | Be nice to America or they'll bring democracy to
Cyrus | your country.

Re: script doubt

am 22.01.2008 21:15:07 von Janis Papanagnou

apogeusistemas@gmail.com wrote:
> Hi:
>
>
> Can You tell why runnig this script I cannot
> get output only in one line ?

Can't tell; your script works for me.

>
> # cat xu
> month=`head -2 x.lis | awk '{print $6}' | cut -d'/' -f1`
> year=`head -2 x.lis | awk '{print $6}' | cut -d'/' -f3`

You may want to reduce the number of processes and switch to a different
approach...

eval $(awk 'NR==2{split($6,a,"/");print "month="a[1]" day="a[3];exit}' x.lis)

(Note that whereever you find head's, tail's, cut's, and grep's, in a large
pipeline it's likely that a single awk program does your task.)

Janis

>
> echo "month: $month - year: $year"
>
> # head x.lis
>
> SunOS dvfsmlum001 5.8 Generic_117350-12 sun4u 12/02/07
>
> 00:00:01 %usr %sys %wio %idle
> 00:05:00 20 5 10 65
> 00:10:00 31 8 8 53
> 00:15:00 21 6 5 68
> 00:20:00 15 5 4 77
> 00:25:00 15 3 1 81
> 00:30:00 13 2 1 84
> # ./xu
> month:
> 12 - year:
> 07
>
>
> Thank You in advance...

Re: script doubt

am 22.01.2008 21:16:32 von apogeusistemas

On 22 jan, 18:15, Janis Papanagnou
wrote:
> apogeusiste...@gmail.com wrote:
> > Hi:
>
> > Can You tell why runnig this script I cannot
> > get output only in one line ?
>
> Can't tell; your script works for me.
>
>
>
> > # cat xu
> > month=3D`head -2 x.lis | awk '{print $6}' | cut -d'/' -f1`
> > year=3D`head -2 x.lis | awk '{print $6}' | cut -d'/' -f3`
>
> You may want to reduce the number of processes and switch to a different
> approach...
>
> eval $(awk 'NR==2{split($6,a,"/");print "month=3D"a[1]" day=3D"a[3];ex=
it}' x.lis)
>
> (Note that whereever you find head's, tail's, cut's, and grep's, in a larg=
e
> pipeline it's likely that a single awk program does your task.)
>
> Janis
>
>
>
>
>
> > echo "month: $month - year: $year"
>
> > # head x.lis
>
> > SunOS dvfsmlum001 5.8 Generic_117350-12 sun4u =A0 =A012/02/07
>
> > 00:00:01 =A0 =A0%usr =A0 =A0%sys =A0 =A0%wio =A0 %idle
> > 00:05:00 =A0 =A0 =A020 =A0 =A0 =A0 5 =A0 =A0 =A010 =A0 =A0 =A065
> > 00:10:00 =A0 =A0 =A031 =A0 =A0 =A0 8 =A0 =A0 =A0 8 =A0 =A0 =A053
> > 00:15:00 =A0 =A0 =A021 =A0 =A0 =A0 6 =A0 =A0 =A0 5 =A0 =A0 =A068
> > 00:20:00 =A0 =A0 =A015 =A0 =A0 =A0 5 =A0 =A0 =A0 4 =A0 =A0 =A077
> > 00:25:00 =A0 =A0 =A015 =A0 =A0 =A0 3 =A0 =A0 =A0 1 =A0 =A0 =A081
> > 00:30:00 =A0 =A0 =A013 =A0 =A0 =A0 2 =A0 =A0 =A0 1 =A0 =A0 =A084
> > # ./xu
> > month:
> > 12 - year:
> > 07
>
> > Thank You in advance...- Ocultar texto entre aspas -
>
> - Mostrar texto entre aspas -

Thanks again Cyrus and Janis ! ! !

Re: script doubt

am 22.01.2008 21:24:06 von Glenn Jackman

At 2008-01-22 02:45PM, "apogeusistemas@gmail.com" wrote:
> Hi:
>
>
> Can You tell why runnig this script I cannot
> get output only in one line ?
>
> # cat xu
> month=`head -2 x.lis | awk '{print $6}' | cut -d'/' -f1`
> year=`head -2 x.lis | awk '{print $6}' | cut -d'/' -f3`
>
> echo "month: $month - year: $year"
>
> # head x.lis
>
> SunOS dvfsmlum001 5.8 Generic_117350-12 sun4u 12/02/07
[...]
> # ./xu
> month:
> 12 - year:
> 07

Just look what $( head -2 x.lis | awk '{print $6}' | cut -d'/' -f1 )
gives you (hint: a leading blank line)

For an all-awk solution, I'd suggest:
awk 'NR==2 {
split($6,date,"/");
printf "month: %s - year: %s\n", date[1], date[3]
}' x.lis

To get the month and year variables into your shell:
eval $(awk -v v1=month -v v2=year '
NR==2 {
split($6,date,"/");
printf "%s=%s %s=%s\n", v1, date[1], v2, date[3]
}' x.lis )
printf "month: %s - year: %s\n" "$month" "$year"

but the eval makes me nervous. Please, someome tell me why that is not
a good solution (assuming it's not).

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

Re: script doubt

am 22.01.2008 21:50:43 von Ed Morton

On 1/22/2008 1:45 PM, apogeusistemas@gmail.com wrote:
> Hi:
>
>
> Can You tell why runnig this script I cannot
> get output only in one line ?
>
> # cat xu
> month=`head -2 x.lis | awk '{print $6}' | cut -d'/' -f1`
> year=`head -2 x.lis | awk '{print $6}' | cut -d'/' -f3`
>
> echo "month: $month - year: $year"
>
> # head x.lis
>
> SunOS dvfsmlum001 5.8 Generic_117350-12 sun4u 12/02/07
>
> 00:00:01 %usr %sys %wio %idle
> 00:05:00 20 5 10 65
> 00:10:00 31 8 8 53
> 00:15:00 21 6 5 68
> 00:20:00 15 5 4 77
> 00:25:00 15 3 1 81
> 00:30:00 13 2 1 84
> # ./xu
> month:
> 12 - year:
> 07
>
>
> Thank You in advance...

You got some good advice on setting month and year shell variables after parsing
your input file with awk, but I wonder if you really need them. There's a good
chance that whatever you're doing with them later in your script, you'd be
better off just sticking with awk. So, try just this:

awk 'NR==2 {
split($6,a,"/")
printf "month: %s - year: %s\n,"a[1],a[3]
exit
}' x.lis

then tell us what else, if anything, you want to do with those month and year
values if you'd like help.

Ed.

Re: script doubt

am 22.01.2008 22:22:35 von someone

Janis Papanagnou wrote:
> apogeusistemas@gmail.com wrote:
>>
>> # cat xu
>> month=`head -2 x.lis | awk '{print $6}' | cut -d'/' -f1`
>> year=`head -2 x.lis | awk '{print $6}' | cut -d'/' -f3`
>
> You may want to reduce the number of processes and switch to a different
> approach...
>
> eval $(awk 'NR==2{split($6,a,"/");print "month="a[1]" day="a[3];exit}'
> x.lis)
>
> (Note that whereever you find head's, tail's, cut's, and grep's, in a large
> pipeline it's likely that a single awk program does your task.)

Or a single Perl program:

$ echo "

SunOS dvfsmlum001 5.8 Generic_117350-12 sun4u 12/02/07

00:00:01 %usr %sys %wio %idle
00:05:00 20 5 10 65
00:10:00 31 8 8 53
00:15:00 21 6 5 68
00:20:00 15 5 4 77
00:25:00 15 3 1 81
00:30:00 13 2 1 84
" | perl -lne'm!(\d+)/\d+/(\d+)! && print "month: $1 - year: $2" and
close ARGV'
month: 12 - year: 07




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

Re: script doubt

am 24.01.2008 08:54:03 von William James

apogeusiste...@gmail.com wrote:
> Hi:
>
>
> Can You tell why runnig this script I cannot
> get output only in one line ?
>
> # cat xu
> month=`head -2 x.lis | awk '{print $6}' | cut -d'/' -f1`
> year=`head -2 x.lis | awk '{print $6}' | cut -d'/' -f3`
>
> echo "month: $month - year: $year"
>
> # head x.lis
>
> SunOS dvfsmlum001 5.8 Generic_117350-12 sun4u 12/02/07
>
> 00:00:01 %usr %sys %wio %idle
> 00:05:00 20 5 10 65
> 00:10:00 31 8 8 53
> 00:15:00 21 6 5 68
> 00:20:00 15 5 4 77
> 00:25:00 15 3 1 81
> 00:30:00 13 2 1 84
> # ./xu
> month:
> 12 - year:
> 07

awk -F' +|/../' 'NF{printf "month: %s - year: %s\n", $2,$3; exit}'
x.lis