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
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.
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).
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With