LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-04-2013, 07:16 PM   #16
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Blog Entries: 2

Rep: Reputation: 244Reputation: 244Reputation: 244

Save this in file a.awk
Code:
/^Class/ {c=$2; if (c>maxclass) {maxclass=c}}
/^[1-9]/ {d=$2; alldates[d]=d; child[d][c]=$4; adult[d][c]=$5}
END{asort(alldates,ordereddates)
show("Adult.txt",adult)
show("Children.txt",child)
}
function show(f,a){
printf("Date      ") >>f
for (c=1; c<=maxclass;c++){printf(" %4d", c) >>f}
print "" >>f
for (d in ordereddates){
    dd=ordereddates[d]
    printf("%s ",dd) >>f
    for (c=1;c<=maxclass;c++) {printf(" %4d", a[dd][c]) >>f}
    print "" >>f
}
}
and run with
Code:
awk -f A.awk  in
and you should afterward have files Adult.txt and Children.txt

Apart from some doubt about whether the dates will always be the right length and sort correctly Ithink that's what you want.

Doing it all in one file in one language is neater and less confusing than mixing shell with awk. Shell uses $ to reference a variable but not to set it. Quotes do different things - it all gets painful on complicated tasks. I've been very happy I took up Perl in the 1990s to escape big jobs in the shell/awk mix.
 
Old 04-04-2013, 08:30 PM   #17
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,670

Rep: Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542
Quote:
Originally Posted by linosaurusroot View Post
Save this in file a.awk ...
I saved it in dbm724.awk.
Your code didn't have a first line starting with hash-bang. Is one needed? I assumed so.

dbm724.awk ...
Code:
#! /bin/awk -f
/^Class/ {c=$2; if (c>maxclass) {maxclass=c}}
/^[1-9]/ {d=$2; alldates[d]=d; child[d][c]=$4; adult[d][c]=$5}
END{asort(alldates,ordereddates)
show("Adult.txt",adult)
show("Children.txt",child)
}
function show(f,a){
printf("Date      ") >>f
for (c=1; c<=maxclass;c++){printf(" %4d", c) >>f}
print "" >>f
for (d in ordereddates){
    dd=ordereddates[d]
    printf("%s ",dd) >>f
    for (c=1;c<=maxclass;c++) {printf(" %4d", a[dd][c]) >>f}
    print "" >>f
}
}
Invocation with error messages ...
Code:
daniel@daniel-desktop:~$ awk -f /home/daniel/Desktop/LQfiles/dbm724.awk /home/daniel/Desktop/LQfiles/dbm724.inp
awk: /home/daniel/Desktop/LQfiles/dbm724.awk:3: /^[1-9]/ {d=$2; alldates[d]=d; child[d][c]=$4; adult[d][c]=$5}
awk: /home/daniel/Desktop/LQfiles/dbm724.awk:3:                                        ^ syntax error
awk: /home/daniel/Desktop/LQfiles/dbm724.awk:3: /^[1-9]/ {d=$2; alldates[d]=d; child[d][c]=$4; adult[d][c]=$5}
awk: /home/daniel/Desktop/LQfiles/dbm724.awk:3:                                                        ^ syntax error
awk: /home/daniel/Desktop/LQfiles/dbm724.awk:15:     for (c=1;c<=maxclass;c++) {printf(" %4d", a[dd][c]) >>f}
awk: /home/daniel/Desktop/LQfiles/dbm724.awk:15:                                                    ^ syntax error
This is my first attempt to run a pure awk program so I may have overlooked something which is obvious to you but not me.

Daniel B. Martin
 
Old 04-04-2013, 08:55 PM   #18
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Blog Entries: 2

Rep: Reputation: 244Reputation: 244Reputation: 244
Your #! alteration should work but follow instructions literally until you know a subject well enough to make variations. I put no #! line because I am not running it by its own filename ("./a.awk") but am running the command awk with arguments.

Your version of awk may differ.
Quote:
$ awk -V
GNU Awk 4.0.0
I assume (from where your syntax error is marked) you haven't got gawk which does multidimension arrays. The foo[x][y] could be changed to foo[x,y] to cope with that.
 
Old 04-04-2013, 09:28 PM   #19
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,670

Rep: Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542
Quote:
Originally Posted by linosaurusroot View Post
Your version of awk may differ.
daniel@daniel-desktop:~$ awk --version
GNU Awk 3.1.6
Quote:
I assume (from where your syntax error is marked) you haven't got gawk which does multidimension arrays.
daniel@daniel-desktop:~$ gawk --version
GNU Awk 3.1.6

Daniel B. Martin
 
Old 04-04-2013, 09:36 PM   #20
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,670

Rep: Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542
Quote:
Originally Posted by linosaurusroot View Post
... follow instructions literally until you know a subject well enough to make variations.
You are right, and I attempted to do that. When a slew of errors resulted I attempted to work through it by Googling and that's where I found the hash-bang.

dbm724.awk (without hash-bang)...
Code:
/^Class/ {c=$2; if (c>maxclass) {maxclass=c}}
/^[1-9]/ {d=$2; alldates[d]=d; child[d][c]=$4; adult[d][c]=$5}
END{asort(alldates,ordereddates)
show("Adult.txt",adult)
show("Children.txt",child)
}
function show(f,a){
printf("Date      ") >>f
for (c=1; c<=maxclass;c++){printf(" %4d", c) >>f}
print "" >>f
for (d in ordereddates){
    dd=ordereddates[d]
    printf("%s ",dd) >>f
    for (c=1;c<=maxclass;c++) {printf(" %4d", a[dd][c]) >>f}
    print "" >>f
}
}
Invocation with error messages ...
Code:
daniel@daniel-desktop:~$ awk -f /home/daniel/Desktop/LQfiles/dbm724.awk /home/daniel/Desktop/LQfiles/dbm724.inp
awk: /home/daniel/Desktop/LQfiles/dbm724.awk:2: /^[1-9]/ {d=$2; alldates[d]=d; child[d][c]=$4; adult[d][c]=$5}
awk: /home/daniel/Desktop/LQfiles/dbm724.awk:2:                                        ^ syntax error
awk: /home/daniel/Desktop/LQfiles/dbm724.awk:2: /^[1-9]/ {d=$2; alldates[d]=d; child[d][c]=$4; adult[d][c]=$5}
awk: /home/daniel/Desktop/LQfiles/dbm724.awk:2:                                                        ^ syntax error
awk: /home/daniel/Desktop/LQfiles/dbm724.awk:14:     for (c=1;c<=maxclass;c++) {printf(" %4d", a[dd][c]) >>f}
awk: /home/daniel/Desktop/LQfiles/dbm724.awk:14:                                                    ^ syntax error
Daniel B. Martin
 
Old 04-04-2013, 09:56 PM   #21
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Blog Entries: 2

Rep: Reputation: 244Reputation: 244Reputation: 244
It's also possible your gawk has some restrictive configuration (maybe an Ubuntu default) - but I didn't see any objection to the asort() function.

awk man page
Quote:
-c
--traditional
Run in compatibility mode. In compatibility mode, gawk behaves identically to UNIX awk; none of the GNU-
specific extensions are recognized. See GNU EXTENSIONS, below, for more information.

-P
--posix
This turns on compatibility mode, with the following additional restrictions: ...
 
Old 04-04-2013, 10:12 PM   #22
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,670

Rep: Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542
Quote:
Originally Posted by linosaurusroot View Post
... I didn't see any objection to the asort() function.
I've used asort as part of awk segments in other bash programs without difficulty.

Daniel B. Martin
 
Old 04-05-2013, 12:43 AM   #23
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,619

Rep: Reputation: 2940Reputation: 2940Reputation: 2940Reputation: 2940Reputation: 2940Reputation: 2940Reputation: 2940Reputation: 2940Reputation: 2940Reputation: 2940Reputation: 2940
@Daniel - your errors still point to 3.1.6 not being able to support multi-dimension arrays ([][]), as instructed previously, you will need to change these all as follows:
Code:
child[d][c] = $4

child[d,c] = $4
The negative here is that now to loop over the items you will need to split in SUBSEP and have a way of ordering the split data.
 
Old 04-05-2013, 01:23 AM   #24
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 11,287

Rep: Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393
Quote:
Originally Posted by grail View Post
@Daniel - your errors still point to 3.1.6 not being able to support multi-dimension arrays ([][]), as instructed previously, you will need to change these all as follows:
Code:
child[d][c] = $4

child[d,c] = $4
The negative here is that now to loop over the items you will need to split in SUBSEP and have a way of ordering the split data.
He need to store the possible values in different arrays, and he can make a loop like this:
ix_array1 = ( date1, date2, date3, date4 ...)
ix_array2 = ( class1, class2, class3, .....)
and probably ix_array3 = (child, adult, infant)
for i in loop on ix_array1
do anything on child[ix_array1[i], c]
and
for j in loop on ix_array2
do anything on child[d, ix_array2[j]]
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED]Wierd AWK behavior / AWK not reading first line. Involar Linux - Newbie 9 11-28-2012 10:53 AM
running series of awk commands from an awk file oreka18 Programming 3 05-16-2012 01:13 AM
awk error awk: line 2: missing } near end of file boscop Linux - Networking 2 04-08-2012 10:49 AM
[SOLVED] call awk from bash script behaves differently to awk from CLI = missing newlines titanium_geek Programming 4 05-26-2011 09:06 PM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 04:11 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration