Home > Software design >  Read a CSV file inside awk script
Read a CSV file inside awk script

Time:12-30

I want to use an AWK script without typing in the terminal the CSV file and instead, call that file from inside my code.

Current Input terminal:

./script.awk file.csv

Desired Input Terminal:

./script.awk

On the other hand, here is the script I have done so far:

#!/usr/bin/awk -f

BEGIN{print"Filtered Elements:"}
BEGIN{FS=","}

{ if ($8~/.*5.*/ && $2~/.*Sh.*/ && ($3~/.*i.*/ || $4~/.*s.*/)) { print } }
{ if ($3~/.*ra.*/ && $7~/.*18.*/ && $13~/.*r.*/) { print } }
{ if ($5~/.*7.*/ && $2~/.*l.*/ && ($4~/.*Fi.*/ || $12~/20.*/)) { print } }
} **file.csv**

I aslo tried to do this:

#!/usr/bin/awk -f
BEGIN{print"Filtered Elements:"}
BEGIN{FS=","}
BEGIN{
   while (getline < file.csv > 0) {
{ if ($8~/.*5.*/ && $2~/.*Sh.*/ && ($3~/.*i.*/ || $4~/.*s.*/)) { print } }
{ if ($3~/.*ra.*/ && $7~/.*18.*/ && $13~/.*r.*/) { print } }
{ if ($5~/.*7.*/ && $2~/.*l.*/ && ($4~/.*Fi.*/ || $12~/20.*/)) { print } }
   } 

But either ways an error occurred. Thank you in advance!

CodePudding user response:

An awk script isn't a command you call, it's a set of instructions interpreted by awk where awk IS a command you call. What you're trying to do apparently is write a Unix command that's implemented as a shell script which includes a call to awk, e.g.:

#!/usr/bin/env bash

awk '
    { print "foo", $0 }
' 'file.csv'

Store that in a file named stuff (not stuff.awk or stuff.sh or anything else with a suffix), and then call it as ./stuff or just stuff if the current directory is in your PATH.

Though you technically can use a shebang to call awk directly, don't do it - see https://stackoverflow.com/a/61002754/1745001.

CodePudding user response:

Your second example is a correct getline loop, except that the file path should be quoted to be treated as a string (and not a variable): while (getline < "file.csv" > 0) #....

Alternatively, you can set the script arguments (including input files and variables) by manipulating ARGV and ARGC in a BEGIN block:

BEGIN {
    ARGV[1] = "file.csv"
    ARGC = 2
}

{
    # commands here process file.csv as normal
}

Running this as ./script is the same as if you set the argument with the shell (like ./script file.csv).

  • Related