Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a .awk file?

Tags:

awk

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 
like image 657
Sharad Avatar asked Apr 03 '12 10:04

Sharad


People also ask

What is a .awk file?

Script created for awk, a text processing program that is included with Unix and Linux operating system distributions; contains instructions for matching, replacing, and printing text from an input string or file; used for producing formatted text and reports.

How do I run an awk script in Linux?

awk ScriptsTell the shell which executable to use to run the script. Prepare awk to use the FS field separator variable to read input text with fields separated by colons ( : ). Use the OFS output field separator to tell awk to use colons ( : ) to separate fields in the output. Set a counter to 0 (zero).

How do I run an awk script in Windows?

You can download and run the setup file. This should install your AWK in " C:\Program Files (x86)\GnuWin32 ". You can run the awk or gawk command from the bin folder or add the folder ``C:\Program Files (x86)\GnuWin32\bin to your PATH`. Show activity on this post.


1 Answers

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.

like image 58
lhf Avatar answered Sep 21 '22 17:09

lhf