-----Original Message-----
From: jon(a)jgcomp.com
Sent: Mon, 09 May 2016 18:45:30 -0400
To: wingators(a)inbox.com
Subject: Re: using awk for selective printing, and adding a new line
On Mon, May 09, 2016 at 02:26:07PM -0800, Antonio Olivares wrote:
>
>
>> It would be much easier to leave it as one field per line.
>>
>>
>> awk '
>> BEGIN { f[1] = 4; f[2] = -14
>> f[3] = f[4] = f[5] = f[6] = f[7] = f[8] = f[9] = f[10] = 3
>> }
>> { printf ("%*s%c", f[NR%10], $0, NR%10 ? " " :
"\n") }
>> ' $1
>>
>> --
>
> Dear Jon,
>
> I like your solution, but I get different output:
>
> $ awk ' BEGIN { f[1] = 4; f[2] = -14 f[3] = f4 = f[5] = f[6] = f[7] =
> f[8] = f[9] = f[10] = 3 } { printf ("%*s%c",f[NR%10], $0, NR%10 ? "
" :
> "\n") }' tablagr2.dat
Notice my BEGIN line is broken into 2 lines. If you want to join
them you must put a semicolon after the "-14".
> awk: syntax error at source line 1
> context is
> BEGIN { f[1] = 4; f[2] = -14 f[3] >>> = <<< f4 = f[5] =
f[6]
> = f[7] = f[8] = f[9] = f[10] = 3 } { printf ("%*s%c",f[NR%10], $0, NR%10
> ? " " : "\n") }
> awk: illegal statement at source line 1
>
> $ awk ' BEGIN { f[1] = 4; f[2] = -14; f[3] = f4 = f[5] = f[6] = f[7] =
> f[8] = f[9] = f[10] = 3 } { printf ("%*s%c",f[NR%10], $0, NR%10 ? "
" :
> "\n") }' tablagr2.dat
Ok, you did that.
>
> Pos. Equipo JJ JG JE JP GF GC DIF
> PTS 1Monterrey 17 12 1 4 38 23
> 15 37 2Pachuca 17 8 6 3 31 16
> 15 30 3 Leon 17 9 3 5 29
> 19 10 30 4 America 17 8 5 4
> 34 22 12 29 5 Chivas 17 7 7
> 3 26 16 10 28 6 Morelia 17 8
> 4 5 25 24 1 28 7 Santos 17
> 8 3 6 22 20 2 27 8 Tigres
> 17 6 6 5 29 19 10 24 9
> CruzAzul 17 5 7 5 25 24 1 22
> 10 Pumas 17 5 7 5 23 24 -1
> 22 11 Toluca 17 5 7 5 20 21
> -1 22 12 Puebla 17 5 7 5 21
> 26 -5 22 13 ClubQueretaro 17 5 4
> 8 21 26 -5 19 14 ClubTijuana 17
> 3 9 5 17 26 -9 18 15 Atlas
> 17 3 5 9 18 26 -8 14 16
> Dorados 17 4 2 11 18 32 -14 14
> 17 Veracruz 17 2 8 7 18 34
> -16 14 18 ChiapasFC 17 3 3 11
> 16 33 -17 12
> $
>
> $ awk ' BEGIN { f[1] = 4; f[2] = -14; f[3] = f4 = f[5] 9] = f[10] = 3 }
> { printf ("%*s%c",f[NR%10], $0, NR%10 ? " " : "\n")
}'
> tablageneral20160509.dat
>
>
> to get the nice output I have to do it in two steps,
> 1) copy the contents of the website to a file, then
> 2) run/pipe the command to
> awk '{printf("%s ",$0)}' filename > newfilename
>
My program should be run using the data from 1),
i.e.
Pos.
Equipo
JJ
JG
...
Cruz Azul
17
...
>
> The names are in two, Cruz Azul, combined into Cruz Azul, Chiapas FC ->
> ChiapasFC, Club Tijuana, Club Queretaro, I have to combine them into 1
> so the data can format nicely. If the commands could be combined to do
> it in one shot, it would be nice, but the names will be a problem.
You don't have to squish them.
>
> The data is from :
>
http://www.mediotiempo.com/tabla_general.php?id_liga=1&id_torneo=591
>
> I select copy the data and paste it into a file, then I have to remove
> the "\n" and then take the 10 items per line. It looks easy, but it
> takes some time.
>
I just cut and pasted that data into a file and ran my program on it.
There were no column headings, but you can add that in the BEGIN
statement
if you want.
My program formatted the data fine for me. The only problem I see is
the website has a little icon before each team name. That might have
to be removed.
1 Monterrey 17 12 1 4 38 23 15 37
2 Pachuca 17 8 6 3 31 16 15 30
3 León 17 9 3 5 29 19 10 30
4 América 17 8 5 4 34 22 12 29
5 Chivas 17 7 7 3 26 16 10 28
6 Morelia 17 8 4 5 25 24 1 28
7 Santos 17 8 3 6 22 20 2 27
8 Tigres 17 6 6 5 29 19 10 24
9 Cruz Azul 17 5 7 5 25 24 1 22
10 Pumas 17 5 7 5 23 24 -1 22
11 Toluca 17 5 7 5 20 21 -1 22
12 Puebla 17 5 7 5 21 26 -5 22
13 Club Querétaro 17 5 4 8 21 26 -5 19
14 Club Tijuana 17 3 9 5 17 26 -9 18
15 Atlas 17 3 5 9 18 26 -8 14
16 Dorados 17 4 2 11 18 32 -14 14
17 Veracruz 17 2 8 7 18 34 -16 14
18 Chiapas FC 17 3 3 11 16 33 -17 12
Jon
--
Jon H. LaBadie jon(a)jgcomp.com
11226 South Shore Rd. (703) 787-0688 (H)
Reston, VA 20190 (703) 935-6720 (C)
I do not get the same output. I have the file
formatabla.sh as follows:
#!/bin/sh
# Check for arguments
if [ $# -eq 0 ]; then
echo "Usage: $(basename $0) filename"
exit 1
fi
if [ ! -f $1 ]; then
echo "File "$1" doesn't exist!"
exit 0
fi
awk '
BEGIN { f[1] = 4; f[2] = -14
f[3] = f[4] = f[5] = f[6] = f[7] = f[8] = f[9] = f[10] = 3
}
{ printf ("%*s%c", f[NR%10], $0, NR%10 ? " " : "\n") }
' $1
$ sh formatablagrl.sh tablageneral0510.dat
1 Monterrey 17 12 1
4 38 23 15 37 2
Pachuca 17 8 6 3
31 16 15 30 3
León 17 9 3 5
29 19 10 30 4 América
17 8 5 4 34
22 12 29 5 Chivas
17 7 7 3 26
16 10 28 6 Morelia 17
8 4 5 25 24
1 28 7 Santos 17
8 3 6 22 20
2 27 8 Tigres 17 6
6 5 29 19 10
24 9 Cruz Azul 17 5
7 5 25 24 1
22 10 Pumas 17 5 7
5 23 24 -1 22
11 Toluca 17 5 7
5 20 21 -1 22 12
Puebla 17 5 7 5
21 26 -5 22 13
Club Querétaro 17 5 4 8
21 26 -5 19 14 Club Tijuana
17 3 9 5 17
26 -9 18 15 Atlas
17 3 5 9 18
26 -8 14 16 Dorados 17
4 2 11 18 32
-14 14 17 Veracruz 17
2 8 7 18 34
-16 14 18 Chiapas FC 17 3
3 11 16 33 -17
12 $
Is the program exactly the same or it is a bit different?
i just copied and pasted and ran the program. Or do I need to pipe it two times?
Best Regards,
Antonio
____________________________________________________________
FREE ONLINE PHOTOSHARING - Share your photos online with your friends and family!
Visit