using awk in a for loop (getting ksh variable)

Hi,

I've tried searching the forums for a case similar to mine but was unsuccessful. This is my first time to use awk so any help would be really appreciated :slight_smile:

I have one file containing data for with the first value in each row being a State Name. I would need to create a separate file for each state which is why i thought that awk could do the job (the file is comma delimited). I would also want the state being processed to be the filename.

The file state.csv contains sample data such as:
NJ XXX XXX XXX
NJ XXX XXX XXX
VA XXX XXX XXX
VA XXX XXX XXX
VA XXX XXX XXX
KS XXX XXX XXX

Here is what I've tried:

for st in NJ VA CO DE KS PA RI UT VT WY;
        do
        awk -v srch="$st" '/^srch/ { print $0}' state.csv >> ${st}_data.csv
        done

Doing that gives me the error:
awk: syntax error near line 1
awk: bailing out near line 1

I have also tried:

for st in NJ VA CO DE KS PA RI UT VT WY;
        do
        awk '/^"'"$st"'"/ { print $0}' state.csv >> ${st}_data.csv
        done

Doing that gives me no error, but all output created is empty :frowning:

I have also tried:

 
for st in NJ VA CO DE KS PA RI UT VT WY; 
do
        awk -f, -v srch="$st" '$1 ~ srch { print $0 }' state.csv >> ${st}_data.csv
done

but i get the bail out error once again.

Can somebody enlighten me as to the correct syntax for awk?

Thanks!

Use nawk or /usr/xpg4/bin/awk on Solaris!

awk '!_[$1]++ { 
  fn && close(fn)
  fn = $1 "_data.csv" 
  }
{ print > fn }' infile

For coma separated values add -F, at the beginning of the command:

awk -F, '!_[$1]++ { 
  fn && close(fn)
  fn = $1 "_data.csv" 
  }
{ print > fn }' infile