> -----Original Message----- > From: jon@xxxxxxxxxx > Sent: Mon, 09 May 2016 14:49:50 -0400 > To: wingators@xxxxxxxxx > Subject: Re: using awk for selective printing, and adding a new line > > On Mon, May 09, 2016 at 08:24:54AM -0800, Antonio Olivares wrote: >> Dear folks, >> >> I have found numerous guides using awk to format stats. I can get stats >> from a website, but when I paste them they get pasted one per line, I >> can get them to one line using awk '{printf("%s ",$0)}' and the filename >> here, but what I want to do is to get the first 11 records and print out >> a new line "\n", then get the next 11 record and get new line "\n" >> >> I get the data from mediotiempo.com, >> >> data: >> >> Pos. Equipo JJ JG JE JP GF GC >> DIF PTS 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 >> >> to >> >> Pos Equipo JJ JG JE JP GF GC DIF PTS >> 1 Monterrey 16 12 1 3 37 21 16 37 >> 2 America 16 8 4 4 33 21 12 28 >> 3 Pachuca 16 7 6 3 29 15 14 27 >> 4 Leon 16 8 3 5 28 19 9 27 >> 5 Santos 16 8 3 5 22 19 3 27 >> 6 Chivas 16 6 7 3 25 16 9 25 >> 7 Morelia 16 7 4 5 23 23 0 25 >> 8 CruzAzul 16 5 7 4 25 21 4 22 >> 9 Tigres 16 5 6 5 26 19 7 21 >> 10 Pumas 16 5 6 5 22 23 -1 21 >> 11 Toluca 16 4 7 5 18 20 -2 19 >> 12 ClubQueretaro 16 5 4 7 20 23 -3 19 >> 13 Puebla 16 4 7 5 18 25 -7 19 >> 14 ClubTijuana 16 3 8 5 17 26 -9 17 >> 15 Dorados 16 4 2 10 18 31 -13 14 >> 16 Veracruz 16 2 8 6 17 32 -15 14 >> 17 Atlas 16 3 4 9 18 26 -8 13 >> 18 ChiapasFC 16 3 3 10 15 31 -16 12 >> >> using a sed command. >> >> I have parts of commands 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 ' >> >> NR == 1 { >> printf "%2s %-14s %3s %3s %3s %3s %3s %3s %3s %3s \n", >> "Pos", "Equipo", "JJ", "JG", "JE", "JP", "GF", "GC" , >> "DIF", "PTS" >> } >> >> NR >= 2 { >> printf "%2d %-14s %3d %3d %3d %3d %3d %3d %3d %3d \n", >> $1, $2, $3, $4, $5, $6, $7, $8, $9, $10 '\n' >> }' $1 >> >> but it just prints the first line and that is it because it treats the >> data as a single line. If that is possible otherwise I will have to do >> it manually. >> >> Best regards, >> >> >> Antonio >> > > 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 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 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 Pos. Equipo JJ JG JE JP GF GC DIF PTS 1 Monterrey 17 12 1 4 38 23 15 37 2 Pachuca 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 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 awk ' NR == 1 { printf "%2s %-14s %3s %3s %3s %3s %3s %3s %3s %3s \n", "Pos", "Equipo", "JJ", "JG", "JE", "JP", "GF", "GC" , "DIF", "PTS" } NR >= 2 { printf "%2d %-14s %3d %3d %3d %3d %3d %3d %3d %3d \n", $1, $2, $3, $4, $5, $6, $7, $8, $9, $10 '\n' }' $1 and then use this one: awk '{ for (i = 1; i <= NF; i++) if (i % 10) printf "%s\t", $i; else printf "%s\n", $i }' newfilename Maybe I should just combine it into 1? I wonder what I am doing wrong? I actually use the commands to fit the data nicely to a latex table as the following: $ awk '{ for (i = 1; i <= NF; i++) if (i % 10) printf "%s\t", $i; else printf "%s\n", $i }' tablagr2.dat | awk '$1=$1' OFS=' & ' ORS=' \\\\ \\hline \n' Pos. & Equipo & JJ & JG & JE & JP & GF & GC & DIF & PTS \\ \hline 1 & Monterrey & 17 & 12 & 1 & 4 & 38 & 23 & 15 & 37 \\ \hline 2 & Pachuca & 17 & 8 & 6 & 3 & 31 & 16 & 15 & 30 \\ \hline 3 & Leon & 17 & 9 & 3 & 5 & 29 & 19 & 10 & 30 \\ \hline 4 & America & 17 & 8 & 5 & 4 & 34 & 22 & 12 & 29 \\ \hline 5 & Chivas & 17 & 7 & 7 & 3 & 26 & 16 & 10 & 28 \\ \hline 6 & Morelia & 17 & 8 & 4 & 5 & 25 & 24 & 1 & 28 \\ \hline 7 & Santos & 17 & 8 & 3 & 6 & 22 & 20 & 2 & 27 \\ \hline 8 & Tigres & 17 & 6 & 6 & 5 & 29 & 19 & 10 & 24 \\ \hline 9 & CruzAzul & 17 & 5 & 7 & 5 & 25 & 24 & 1 & 22 \\ \hline 10 & Pumas & 17 & 5 & 7 & 5 & 23 & 24 & -1 & 22 \\ \hline 11 & Toluca & 17 & 5 & 7 & 5 & 20 & 21 & -1 & 22 \\ \hline 12 & Puebla & 17 & 5 & 7 & 5 & 21 & 26 & -5 & 22 \\ \hline 13 & ClubQueretaro & 17 & 5 & 4 & 8 & 21 & 26 & -5 & 19 \\ \hline 14 & ClubTijuana & 17 & 3 & 9 & 5 & 17 & 26 & -9 & 18 \\ \hline 15 & Atlas & 17 & 3 & 5 & 9 & 18 & 26 & -8 & 14 \\ \hline 16 & Dorados & 17 & 4 & 2 & 11 & 18 & 32 & -14 & 14 \\ \hline 17 & Veracruz & 17 & 2 & 8 & 7 & 18 & 34 & -16 & 14 \\ \hline 18 & ChiapasFC & 17 & 3 & 3 & 11 & 16 & 33 & -17 & 12 \\ \hline 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. 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. Thanks for your help. Best Regards, Antonio ____________________________________________________________ Can't remember your password? Do you need a strong and secure password? Use Password manager! It stores your passwords & protects your account. Check it out at http://mysecurelogon.com/password-manager -- users mailing list users@xxxxxxxxxxxxxxxxxxxxxxx To unsubscribe or change subscription options: http://lists.fedoraproject.org/admin/lists/users@xxxxxxxxxxxxxxxxxxxxxxx Fedora Code of Conduct: http://fedoraproject.org/code-of-conduct Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines Have a question? Ask away: http://ask.fedoraproject.org