Print all the fields of record using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print all the fields of record using awk
# 1  
Old 06-01-2011
Print all the fields of record using awk

Hi,

i want to generate print statement using awk.
i have 20+ and 30+ fields in each line

Now its priting only first eight fields print statement as output not all.

my record is as shown below filename
HTML Code:
TFP_DNS,saidata.US_TFP_DNS_1,IMSI,APN,IMEI,EVENT_AMOUNT,CAUSE_PROC,CHARGING_INFO,RAG1,RAG2,RAG3,RAG4,RAG5,RAG6,RAG7,RAG8,RAG9,RAG10,RAG11
TFP_DNS,saidata.PS_TFP_DNS_1_HOUR,IMSI,APN,IMEI,EVENT_AMOUNT,CAUSE_PROC,CHARGING_INFO,RAG1,RAG2,RAG3,RAG4,RAG5,RAG6,RAG7,RAG8,RAG9,RAG10
TFP_DNS,saidata.PS_TFP_DNS_1_DAY,IMSI,APN,IMEI,EVENT_AMOUNT,CAUSE_PROC,CHARGING_INFO,RAG1,RAG2,RAG3,RAG4,RAG5,RAG6,RAG7,RAG8,RAG9,
TFP_DNS,saidata.PS_TFP_DNS_1_WEEK,IMSI,APN,IMEI,EVENT_AMOUNT,CAUSE_PROC,CHARGING_INFO,RAG1,RAG2,RAG3,RAG4,RAG5,RAG6,RAG7,RAG8,
TFP_DNS,saidata.PS_TFP_DNS_1_MONTH,IMSI,APN,IMEI,EVENT_AMOUNT,CAUSE_PROC,CHARGING_INFO,RAG1,RAG2,RAG3,RAG4,RAG5,RAG6,RAG7,RAG8,RAG9,RAG10,RAG11,RAG12,RAG13,RAG14  
Code:
 
my awk calling in script is 
awk -F"," -v var=$sql_dir -f do.agg.out fiiename.csv 
 
and awk file do.agg.out looks like 
 
/US_/ { 
 print "spool /opt/nsn/robot/spool/Actual_RAW_JOB.txt; " > ""var"/RAW-Data-out.sql"
  for( i = 2; i <=NF; i++ )  
  {
           print " select count(*), "$i" FROM "$1" where "$i" in (select distinct "$i" from "$1") group by "$i" order by "$i" asc; \n"  >> ""var"/RAW-Data-out.sql"
  }
          print " select count(*) from "$1";\n" >>  ""var"/RAW-Data-out.sql"
   print " spool off; \n exit; " >> ""var"/RAW-Data-out.sql" 
    }

its working fine but its displaying only 8 records (print statement) in for loop instead of number of fields ??

let me know anything wrong i am doing ?

Thanks,
Raghavendra
# 2  
Old 06-01-2011
I don't see any error in the for loop, can you try print the value of NF before the for loop.
# 3  
Old 06-01-2011
OUPUT is looks like this

HTML Code:
spool /opt/nsn/robot/spool/Actual_RAW_JOB.txt;
 select count(*), IMSI FROM saidata.US_TFP_DNS_1 where IMSI in (select distinct IMSI from saidata.US_TFP_DNS_1) group by IMSI order by IMSI asc;
 select count(*), APN FROM saidata.US_TFP_DNS_1 where APN in (select distinct APN from saidata.US_TFP_DNS_1) group by APN order by APN asc;
 select count(*), IMEI FROM saidata.US_TFP_DNS_1 where IMEI in (select distinct IMEI from saidata.US_TFP_DNS_1) group by IMEI order by IMEI asc;
 select count(*), EVENT_AMOUNT FROM saidata.US_TFP_DNS_1 where EVENT_AMOUNT in (select distinct EVENT_AMOUNT from saidata.US_TFP_DNS_1) group by EVENT_AMOUNT order by EVENT_AMOUNT asc;
 select count(*), CAUSE_PROC FROM saidata.US_TFP_DNS_1 where CAUSE_PROC in (select distinct CAUSE_PROC from saidata.US_TFP_DNS_1) group by CAUSE_PROC order by CAUSE_PROC asc;
 select count(*), CHARGING_INFO FROM saidata.US_TFP_DNS_1 where CHARGING_INFO in (select distinct CHARGING_INFO from saidata.US_TFP_DNS_1) group by CHARGING_INFO order by CHARGING_INFO asc;
 select count(*), RAG1 FROM saidata.US_TFP_DNS_1 where RAG1 in (select distinct RAG1 from saidata.US_TFP_DNS_1) group by RAG1 order by RAG1 asc;
 select count(*), RAG2 FROM saidata.US_TFP_DNS_1 where RAG2 in (select distinct RAG2 from saidata.US_TFP_DNS_1) group by RAG2 order by RAG2 asc;
 select count(*) from saidata.US_TFP_DNS_1;
 spool off;
 exit;
it is generating only 8 statements in forloop but i am expecting as many fileds in the input file
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk sort based on difference of fields and print all fields

Hi I have a file as below <field1> <field2> <field3> ... <field_num1> <field_num2> Trying to sort based on difference of <field_num1> and <field_num2> in desceding order and print all fields. I tried this and it doesn't sort on the difference field .. Appreciate your help. cat... (9 Replies)
Discussion started by: newstart
9 Replies

2. Shell Programming and Scripting

awk - compare 1st 15 fields of record with 20 fields

I'm trying to compare 2 files for differences in a selct number of fields. When differnces are found it will write the whole record of the second file including appending '|C' out to a delta file. Each record will have 20 fields, but only want to do comparison of 1st 15 fields. The 1st field of... (7 Replies)
Discussion started by: sljnk
7 Replies

3. Shell Programming and Scripting

How to print 1st field and last 2 fields together and the rest of the fields after it using awk?

Hi experts, I need to print the first field first then last two fields should come next and then i need to print rest of the fields. Input : a1,abc,jsd,fhf,fkk,b1,b2 a2,acb,dfg,ghj,b3,c4 a3,djf,wdjg,fkg,dff,ggk,d4,d5 Expected output: a1,b1,b2,abc,jsd,fhf,fkk... (6 Replies)
Discussion started by: 100bees
6 Replies

4. UNIX for Dummies Questions & Answers

keeping last record among group of records with common fields (awk)

input: ref.1;rack.1;1 #group1 ref.1;rack.1;2 #group1 ref.1;rack.2;1 #group2 ref.2;rack.3;1 #group3 ref.2;rack.3;2 #group3 ref.2;rack.3;3 #group3 Among records from same group (i.e. with same 1st and 2nd field - separated by ";"), I would need to keep the last record... (5 Replies)
Discussion started by: beca123456
5 Replies

5. Shell Programming and Scripting

AWK print initial record and double

I have an initial record 0.018 I would like a script that would for i=0;i<200;i++ print 0.018*1 0.018*2 0.018*3 0.018*4 ... 0.018*200 using newline. (7 Replies)
Discussion started by: chrisjorg
7 Replies

6. Shell Programming and Scripting

AWK exclude first and last record, sort and print

Hi everyone, I've really searched for a solution to this and this is what I found so far: I need to sort a command output (here represented as a "cat file" command) and from the second down to the second-last line based on the second row and then print ALL the output with the specified section... (7 Replies)
Discussion started by: dentex
7 Replies

7. Shell Programming and Scripting

awk text record - prepend first field to all subsequent fields

Hello everyone, I've suddenly gotten very interested in sed and awk (and enjoying it quite a bit too) because of a large conversion project that we're working on. I'm currently stuck with a very inefficient process for processing text blocks. I'm sure someone here should be able to easily point out... (2 Replies)
Discussion started by: jameswatson3
2 Replies

8. Shell Programming and Scripting

awk - print record with both string1 and string2

How do I use awk to find the records in a file that contains two specific strings? I have tried piping and using awk two times, but I don't know how to do it in one action. (2 Replies)
Discussion started by: locoroco
2 Replies

9. Shell Programming and Scripting

awk /nawk :: print the everything except the first and the last fields

format of file1 "file1.txt" 1 2 3 4 A B C XX YY ZZ AA WWW The output must contain except the first and last column the output must be 2 3 B YY ZZ AA (8 Replies)
Discussion started by: centurion_13
8 Replies

10. Shell Programming and Scripting

awk: record has too many fields

Hi, I'm trying this command - but get this error. Do you guys have any workaround for this? cat tf|sed 's/{//g'|sed 's/,//g'|awk '{for (i=1;i<=NF;i++) {if ($i == "OPTIME") {k = i + 2; print $i,$k}}}' awk: record `2005 Jul 28 17:35:29...' has too many fields record number 15 This is how... (3 Replies)
Discussion started by: chaandana
3 Replies
Login or Register to Ask a Question