using awk in a for loop (getting ksh variable)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting using awk in a for loop (getting ksh variable)
# 1  
Old 03-04-2009
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 Smilie

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:
Code:
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:
Code:
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 Smilie

I have also tried:
Code:
 
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!
# 2  
Old 03-04-2009
Use nawk or /usr/xpg4/bin/awk on Solaris!
Code:
awk '!_[$1]++ { 
  fn && close(fn)
  fn = $1 "_data.csv" 
  }
{ print > fn }' infile

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

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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Variable and awk inside for loop

Thanks all for taking time out and reading this thread and big Thanks to all who have come forward for rescue. Background: I have a variable "nbrofcols" that has number of columns from a data file. Now, using this count in for loop, I am trying to get the maximum length of each column present... (7 Replies)
Discussion started by: svks1985
7 Replies

2. UNIX for Dummies Questions & Answers

Awk: problem for loop through variable

Hi, input: AAA|1 my script (the function is just an example): gawk 'BEGIN{FS=OFS="|"} function repeat(str, n, rep, i){ for(i=1; i<=n; i++) rep=rep str return rep } { variable_1=repeat($1,$2) variable_2=repeat($1,$2+1) variable_3=repeat($1,$2+3) ... (5 Replies)
Discussion started by: beca123456
5 Replies

3. Shell Programming and Scripting

awk programming -Passing variable to awk for loop

Hi All, I am new to AWK programming. I have the following for loop in my awk program. cat printhtml.awk: BEGIN -------- <some code here> END{ ----------<some code here> for(N=0; N<H; N++) { for(M=5; M<D; M++) print "\t" D ""; } ----- } ... (2 Replies)
Discussion started by: ctrld
2 Replies

4. Shell Programming and Scripting

AWK Variable assignment issue Ksh script

Hi There, I am writing a ksh script which assigns variable values from file "A" and passes that variables to file "B". While passing the parameters an additional "$" sign is being assigned to awk -v option. Could any one help me with this please. #!/bin/ksh head -1... (3 Replies)
Discussion started by: Jeevanm
3 Replies

5. Shell Programming and Scripting

Setting a variable in a while loop (.ksh script)

Hello Everyone, I'm still trying to grasp many concepts in .ksh scripting, one of them being variables inside loops. My problem is the following: * I'm trying to set a variable inside a while read loop to reuse it outside of said loop. My lines are the following :... (13 Replies)
Discussion started by: jimmy75_13
13 Replies

6. Shell Programming and Scripting

for loop in awk script using ksh

Guys, I am new in awk , I face problem while i try to use for loop in awk, I am using ksh, i am trying to set a for loop which runs as man times as the records in a file , the for loop like for(a=1;a<=5;a++) is working in my awk script but the one i need is not working :wall: for example ... (8 Replies)
Discussion started by: djahmed
8 Replies

7. UNIX for Dummies Questions & Answers

Passing KSH variable to AWK with two loops

Hi, I need some help to figure out why an outer for loop KSH variable does not decode in AWK but inner for loop does. Below is my code, If I hard code variable 'SUBSEQ' in AWK it works but if I try to pass the SUBSEQ from KSH, it does not and when I pass the variable 'NAM' from KSH it works: I... (1 Reply)
Discussion started by: chowdhut
1 Replies

8. Shell Programming and Scripting

ksh variable pass to awk

I'm trying to store the response from a nawk command inside of a ksh script. The command is: text=$(nawk -F: '$1 ~ /${imgArray}/ {print $2}' ${etcDir}/captions.txt) From what I can tell, the imgArray variable is not being expanding when it is inside the single quote ('). Is there something I... (4 Replies)
Discussion started by: meman1188
4 Replies

9. Shell Programming and Scripting

bash and ksh: variable lost in loop in bash?

Hi, I use AIX (ksh) and Linux (bash) servers. I'm trying to do scripts to will run in both ksh and bash, and most of the time it works. But this time I don't get it in bash (I'm more familar in ksh). The goal of my script if to read a "config file" (like "ini" file), and make various report.... (2 Replies)
Discussion started by: estienne
2 Replies

10. Shell Programming and Scripting

Passing a variable to awk while in a shell for loop

I am a newbie to awk and c programming, however am not a unix newbie. However, I do need help with a kshell script I am writing. It is almost complete, the last step is killing me. Any help would be greatly appreciated. What I am trying to do is cat a text file that has usernames. Then, using... (2 Replies)
Discussion started by: synergy_texas
2 Replies
Login or Register to Ask a Question