4

the script was working. I added some comments and renamed it then submitted it. today my instructor told me it doesnt work and give me the error of awk 1 unexpected character '.' the script is supposed to read a name in command line and return the student information for the name back. right now I checked it and surprisingly it gives me the error. I should run it by the command like this:

scriptName -v name="aname" -f filename

what is this problem and which part of my code make it?

#!/usr/bin/awk
BEGIN{
tmp=name;
nameIsValid;
if (name && tolower(name) eq ~/^[a-z]+$/ )
{

    inputName=tolower(name)
    nameIsValid++;
}
else
{
    print "you have not entered the student name"
    printf "Enter the student's name: "
    getline inputName < "-"
    tmp=inputName;
    if (tolower(inputName) eq ~/^[a-z]+$/)
    {
        tmpName=inputName
        nameIsValid++
    }
    else
    {
    print "Enter a valid name!"
    exit
    }
}
inputName=tolower(inputName)
FS=":"
   }
   {
if($1=="Student Number")
{
    split ($0,header,FS)    
}
if ($1 ~/^[0-9]+$/ && length($1)==8)
{
    split($2,names," ")
    if (tolower(names[1]) == inputName || tolower(names[2])==inputName )
    {
        counter++
            for (i=1;i<=NF;i++)
            {
                printf"%s:%s ",header[i], $i

            }

            printf "\n"

    }
}
   }
   END{
   if (counter == 0 && nameIsValid)
   {
 printf "There is no record for the %-10s\n" , tmp 
   }
   }
femchi
  • 1,185
  • 8
  • 20
  • 37

4 Answers4

3

Here are the steps to fix the script:

  1. Get rid of all those spurious NULL statements (trailing semi-colons at the end of lines).
  2. Get rid of the unset variable eq (it is NOT an equality operator!) from all of your comparions.
  3. Cleanup the indenting.
  4. Get rid of that first non-functional nameIsValid; statement.
  5. Change printf "\n" to the simpler print "".
  6. Get rid of the useless ,FS arg to split().
  7. Change name && tolower(name) ~ /^[a-z]+$/ to just the second part of that condition since if that matches then of course name is populated.
  8. Get rid of all of those tolower()s and use character classes instead of explicit a-z ranges.
  9. Get rid of the tmp variable.
  10. Simplify your BEGIN logic.
  11. Get rid of the unnecessary nameIsValid variable completely.
  12. Make the awk body a bit more awk-like

And here's the result (untested since no sample input/output posted):

BEGIN {
    if (name !~ /^[[:alpha:]]+$/ ) {
        print "you have not entered the student name"
        printf "Enter the student's name: "
        getline name < "-"
    }

    if (name ~ /^[[:alpha:]]+$/) {
        inputName=tolower(name)
        FS=":"
    }
    else {
        print "Enter a valid name!"
        exit
    }
}

$1=="Student Number" { split ($0,header) }

$1 ~ /^[[:digit:]]+$/ && length($1)==8 {

        split(tolower($2),names," ")

        if (names[1]==inputName || names[2]==inputName ) {
            counter++
            for (i=1;i<=NF;i++) {
                printf "%s:%s ",header[i], $i
            }
            print ""
        }
    }
}

END {
    if (counter == 0 && inputName) {
        printf "There is no record for the %-10s\n" , name
    }
}
Gaurang Tandon
  • 6,504
  • 11
  • 47
  • 84
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
2

I changed the shebang line to:

#!/usr/bin/awk -f

and then in command line didnt use -f. It is working now

femchi
  • 1,185
  • 8
  • 20
  • 37
  • 1
    Make sure you never run this script on Solaris where /usr/bin/awk is old, broken awk or you'll get cryptic syntax errors and/or unexpected output. – Ed Morton Oct 24 '12 at 21:37
0

Run the script in the following way:

awk -f script_name.awk input_file.txt

This seems to suppress the warnings and errors.

Ishrak
  • 509
  • 1
  • 9
  • 17
0

In my case, the problem was resetting the IFS variable to be IFS="," as suggested in this answer for splitting string into an array. So I resetted the IFS variable and got my code to work.

IFS=', '
read -r -a array <<< "$string"
IFS=' ' # reset IFS
Gaurang Tandon
  • 6,504
  • 11
  • 47
  • 84