33

I am converting a CSV file into a table format, and I wrote an AWK script and saved it as my.awk. Here is the my script:

#AWK for test
awk -F , '
    BEGIN {
        aa = 0;
    }
    {
        hdng = "fname,lname,salary,city";
        l1 = length($1);
        l13 = length($13);
        if ((l1 > 2) &&  (l13 == 0)) {
            fname = substr($1, 2, 1);
            l1 = length($3) - 4;
            lname = substr($3, l1, 4);
            processor = substr($1, 2);
            #printf("%s,%s,%s,%s\n", fname, lname, salary, $0);
        }

        if ($0 ~ ",,,,")
            aa++
        else if ($0 ~ ",fname")
            printf("%s\n", hdng);
        else if ((l1 > 2) && (l13 == 0)) {
            a++;
        }
        else {
            perf = $11;
            if (perf ~/^[0-9\.\" ]+$/)
                type = "num"
            else
                type = "char";
            if (type == "num")
                printf("Mr%s,%s,%s,%s,,N,N,,\n", $0,fname,lname, city);
        }
    }
    END {
    } ' < life.csv > life_out.csv*

How can I run this script on a Unix server? I tried to run this my.awk file by using this command:

awk -f my.awk life.csv
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sharad
  • 3,562
  • 6
  • 37
  • 59

3 Answers3

44

The file you give is a shell script, not an awk program. So, try sh my.awk.

If you want to use awk -f my.awk life.csv > life_out.cs, then remove awk -F , ' and the last line from the file and add FS="," in BEGIN.

lhf
  • 70,581
  • 9
  • 108
  • 149
  • can you pipe output from something like `tail -f` into an `awk` shell script? So I could tail a log file then do some processing with awk then output it to the screen? – mal Feb 09 '18 at 06:27
  • Actually it looks like stoggy answer below is what I'm looking for. – mal Feb 09 '18 at 06:29
34

If you put #!/bin/awk -f on the first line of your AWK script it is easier. Plus editors like Vim and ... will recognize the file as an AWK script and you can colorize. :)

#!/bin/awk -f
BEGIN {}  # Begin section
{}        # Loop section
END{}     # End section

Change the file to be executable by running:

chmod ugo+x ./awk-script

and you can then call your AWK script like this:

`$ echo "something" | ./awk-script`
Calaf
  • 10,113
  • 15
  • 57
  • 120
stoggy
  • 441
  • 4
  • 2
  • You can simply say `chmod +x ...` instead of `chmod ugo+x ...` – Csaba Toth Sep 23 '20 at 22:30
  • @Calaf: Your edit would be clearer if you placed the `chmod` instruction **before** the `echo "something" | ./awk-script` command. –  Jan 23 '21 at 08:03
  • @Seamus Good point. Done. (I resisted the temptation to yield to my indignation at Emacs being referred to with "dots" by adding specific instructions that make Emacs's superiority plenty evident.) – Calaf Jan 25 '21 at 19:29
18

Put the part from BEGIN....END{} inside a file and name it like my.awk.

And then execute it like below:

awk -f my.awk life.csv >output.txt

Also I see a field separator as ,. You can add that in the begin block of the .awk file as FS=","

ooransoy
  • 785
  • 5
  • 10
  • 25
Vijay
  • 65,327
  • 90
  • 227
  • 319