Count string at header.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Count string at header.
# 1  
Old 04-20-2007
Count string at header.

Hi there, it may quite easy but i've tried my best to editing this awk script but didn't get to solution.
Scripts:
Code:
BEGIN { Header = "1 ";
        FileType = "NBI";
        #Count = Count + 1;
				printf("%-2s%-3s%03s%-14s%-84s\r\n", Header, FileType, Count, FileSeq, SysTime, " ");
}
{       Count2 = Count2 + 1;
        split($0,a,FS);
        
        Header = a[1];
        gsub(" ", "",Header);
        AcctNo = a[2];
        gsub(" ", "",AcctNo);
        LastBillDt = a[3];
        gsub(" ", "",LastBillDt);
        DueDt = a[4];
        gsub(" ", "",DueDt);
        PymtAmt = a[5];
        gsub(" ", "",PymtAmt);
        Myr = a[6];
        BillPd = a[7];
        AcctStat = a[8];
        KodHsl = a[9];
        SorceSys = a[10];                       
        printf("%-2s%-14s%-8s%-8s%-13s%-3s%-2s%-1s%-3s%-4s\r\n", Header, AcctNo, LastBillDt, DueDt, 
				PymtAmt, Myr, BillPd, AcctStat,KodHsl,SorceSys);
}
END {
   printf("%-2s%08d%-68s\r\n", Trailer, Count, " ");
}

i want to view the header just like this:
Code:
1 NBI001 200704030000999154634NBI00120070403154634.txt                                                                                  
2 A890060392003 2007022820060528+        .00MYR28 0140002                       
2 A890060392502 2007022820060528+        .00MYR28 0140002                       
2 B502997650806 2007022820060528+        .00MYR28A0140002

*the bold one is where the count should appear.


if i run the scripts, it goes like this:
Code:
1 NBI001 20070403       154634NBI00120070403154634.txt                                                                                  
2 A890060392003 2007022820060528+        .00MYR28 0140002                       
2 A890060392502 2007022820060528+        .00MYR28 0140002                       
2 B502997650806 2007022820060528+        .00MYR28A0140002
TR 0000999

but, if it at the end of the file, it view the total count, that makes me dull.
Anyone have ideas, i'am new to AWK and quite weak in programming.
# 2  
Old 04-20-2007
Code:
BEGIN { Header = "1 ";
        FileType = "NBI";
        #Count = Count + 1;				
}
{       Count2 = Count2 + 1;
        split($0,a,FS);
        
        Header = a[1];
        gsub(" ", "",Header);
        AcctNo = a[2];
        gsub(" ", "",AcctNo);
        LastBillDt = a[3];
        gsub(" ", "",LastBillDt);
        DueDt = a[4];
        gsub(" ", "",DueDt);
        PymtAmt = a[5];
        gsub(" ", "",PymtAmt);
        Myr = a[6];
        BillPd = a[7];
        AcctStat = a[8];
        KodHsl = a[9];
        SorceSys = a[10];                       
        str = str sprintf("%-2s%-14s%-8s%-8s%-13s%-3s%-2s%-1s%-3s%-4s\r\n", Header, AcctNo, LastBillDt, DueDt, PymtAmt, Myr, BillPd, AcctStat,KodHsl,SorceSys) ;
}
END {
   sub("\n$","",str)
   printf("%-2s%-3s%03s%-14s%-84s\r\n", "1", "NBI", Count, FileSeq, SysTime, " ");
   print str;
}

# 3  
Old 04-20-2007
Thanks anbu23.
it works very well and i get into awk clear fron now on.
thank alot pal.
# 4  
Old 04-20-2007
on the otherhand,

why this data comu out like this:
Code:
2B5027939545082007031920060617+.00MYR19A0140002                                  
2B5027939550082007031920060617-64.59MYR190140002                           
2B5027939573102007031920060617-943.06MYR190140002                           
2A9201931083052007031920060617+.00MYR19K0140002

instant of this:
Code:
2A500222577302 2007031920060617+        .00MYR19A0140002                        
2B502793954508 2007031920060617+        .00MYR19A0140002                        
2B502793955008 2007031920060617-      64.59MYR19 0140002                        
2B502793957310 2007031920060617-     943.06MYR19 0140002                        
2A920193108305 2007031920060617+        .00MYR19K0140002

is there something wrong in this code:
Code:
 Count2 = Count2 + 1;
        split($0,a,FS);
        
        Header = a[1];
        gsub(" ", "",Header);
        AcctNo = a[2];
        gsub(" ", "",AcctNo);
        LastBillDt = a[3];
        gsub(" ", "",LastBillDt);
        DueDt = a[4];
        gsub(" ", "",DueDt);
        PymtAmt = a[5];
        gsub(" ", "",PymtAmt);
        Myr = a[6];
        BillPd = a[7];
        AcctStat = a[8];
        KodHsl = a[9];
        SorceSys = a[10];                       
        str = str sprintf("%-2s%-14s%-8s%-8s%-13s%-3s%-2s%-1s%-3s%-4s\r\n", Header, AcctNo, LastBillDt, DueDt, PymtAmt, Myr, BillPd, AcctStat,KodHsl,SorceSys) ;

it seem that this script remove the white spaces(in the bold area) which have to be remained in the original place.
Smilie sorry to ask again, i've tried but become worse than the original one.

Last edited by Helmi; 04-20-2007 at 05:16 AM..
# 5  
Old 04-20-2007
Can you show your input?
# 6  
Old 04-20-2007
Actually, after doing query, the output is pp_spool_out file, like below:
Code:
2B502793954508 2007031920060617+        .00MYR19A0140002                        
2B502793955008 2007031920060617-      64.59MYR19 0140002                        
2B502793957310 2007031920060617-     943.06MYR19 0140002                        
2A920193108305 2007031920060617+        .00MYR19K0140002                        
2B502793952001 2007031920060617+        .00MYR19A0140002

the formats e.g columns, are controls by SQL scripts.

then, in a shell script, which i run after spooling the query, i used awk like below
just to add header, totalrecords and dates.
Code:
awk -f invoice2.awk -v SysDate=${sysdate} -v SysTime=${systime} -v FileSeq=${seqno} ${f_pp_spool_out} > ${f_pp_out}${seqno}${fsystime}.dat

the bold is the above awk script.
i come to cross my mind another way which is how to modify the invoice2.awk just to add header and totalrecords of pp_spool_out rather than write back the whole file, because it will merge all lines.

Thanks,
Helmi
# 7  
Old 04-20-2007
If you just want to add header record to pp_spool_out why do you need awk processing.
Code:
no_of_records=$( wc -l < pp_spool_out )
hdr_rec="1 NBI001 $(date) ${no_of_records} ..." 
sed "1 i\\
$hdr_rec" pp_spool_out > newfile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep a string and count following lines starting with another string

I have a large dataset with following structure; C 0001 Carbon D SAR001 methane D SAR002 ethane D SAR003 propane D SAR004 butane D SAR005 pentane C 0002 Hydrogen C 0003 Nitrogen C 0004 Oxygen D SAR011 ozone D SAR012 super oxide C 0005 Sulphur D SAR013... (3 Replies)
Discussion started by: Syeda Sumayya
3 Replies

2. Shell Programming and Scripting

Compare file1 header count with file2 line count

What I'm trying to accomplish. I receive a Header and Detail file for daily processing. The detail file comes first which holds data, the header is a receipt of the detail file and has the detail files record count. Before processing the detail file I would like to put a wrapper around another... (4 Replies)
Discussion started by: pone2332
4 Replies

3. UNIX for Dummies Questions & Answers

File Row Line Count without Header Footer

Hi There! I am saving the file count of all files in a directory to an output file using: wc -l * > FileCount.txt I get: 114 G4SXORD 3 G4SXORH 0 G4SXORP 117 total But this count includes header and footer. I want to subtract 2 from the count and get ... (7 Replies)
Discussion started by: gagan8877
7 Replies

4. Programming

Error with the string.h header

I get this error when I try to compile following C program #include <stdio.h> #include <strings.h> #define LEN 80 int main(void) { int mess, flag = 1; int first = 0, last = 0; printf("Enter a message: "); gets(mess); if (strlen(mess) > LEN) ... (3 Replies)
Discussion started by: solaris_user
3 Replies

5. Shell Programming and Scripting

Need awk help to print specific columns with as string in a header

awk experts, I have a big file of 4000 columns with header. Would like to print the columns with string value of "Commands" in header. File has "," separator. This file is on ESX host with Bash. Thanks, Arv (21 Replies)
Discussion started by: arv_cds
21 Replies

6. Shell Programming and Scripting

Extract columns where header matches a given string

Hi, I'm having trouble pulling out columns where the headers match a file of key ID's I'm interested in and was looking for some help. file1.txt I Name 34 56 84 350 790 1215 1919 7606 9420 file2.txt I Name 1 1 2 2 3 3 ... 34 34... 56 56... 84 84... 350 350... M 1 A A A A... (20 Replies)
Discussion started by: flotsam
20 Replies

7. Shell Programming and Scripting

Header as is.. trailer count

i have .DAT file FILE1.DAT 1200910270040625 2123456789 J123456 ABC 2123456789 K123456 ABC 2222222222 L123456 DEF 2333333333 M12345 GHI 30000004 My outfile FILE2.TXT should have like this, I need the header value as ie (1200910270040625 ) body rows remove the duplicate rows and the... (2 Replies)
Discussion started by: kshuser
2 Replies

8. Shell Programming and Scripting

Inserting a String in a file header.

Dear all, I have a file created in the name sample.txt in UNIX with header and footer. How to insert a required string (for example "FILE1") in the header part after the file has been created. What kind of command can i use to do the same. Thanks in advance Hari (3 Replies)
Discussion started by: Hari123
3 Replies

9. UNIX for Dummies Questions & Answers

Checking the header and trailer for a given string and if not found, exit out of the

hi, How to check a given file for a string and if it's not found, exit out ofthe script? e.g. a file Test123 is there whose header begins with #bt and trailer begins with #ed. I have to check if the header and trailer matches as above and if not, exit out of the script. How can we do it in... (2 Replies)
Discussion started by: er_ashu
2 Replies

10. Shell Programming and Scripting

Count No of Records in File without counting Header and Trailer Records

I have a flat file and need to count no of records in the file less the header and the trailer record. I would appreciate any and all asistance Thanks Hadi Lalani (2 Replies)
Discussion started by: guiguy
2 Replies
Login or Register to Ask a Question