file manipulation using nawk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting file manipulation using nawk
# 8  
Old 10-16-2012
Quote:
Originally Posted by pamu
try this..

Code:
nawk 'FNR==NR{a[$1,$2,$4]=$6;next}{if(a[$1,$2,$4] != ""){s=(a[$1,$2,$4]-$6);$6=0;print $0", Total "s" times gapped in past 1 hr."}}' OFS="\t" file1 file2

Thanks Pamu, it worked, but to double check, i just modified the first 2 lines of file f1 manually like below
Code:
f1:
TSCparser1      1irons1         1 EMEA_01     -- 5 gaps (7647450 missing messages), 0 seq no resets
TSCparser12     1irons1         1 SPAIN_01    -- 3 gaps (43242430 missing messages), 0 seq no resets

And running the awk.
Code:
nawk 'FNR==NR{a[$1,$2,$4]=$6;next}{if(a[$1,$2,$4] != ""){s=(a[$1,$2,$4]-$6);$6=0;print $0", Total "s" times gapped in past 1 hr."}}' OFS="   " f1 f2

got the below output where in which, total gaps are correctly showing as "5"
but the modification done on "missing messages are being shown as 0" and 6th col is also "0" where i set it to non-zero value

Code:
san:/tmp> nawk 'FNR==NR{a[$1,$2,$4]=$6;next}{if(a[$1,$2,$4] != ""){s=(a[$1,$2,$4]-$6);$6=0;print $0", Total "s" times gapped in past 1 hr."}}' OFS="   " f1 f2
TSCparser1   1irons1   1   EMEA_01   --   0   gaps   (0   missing   messages),   0   seq   no   resets, Total 5 times gapped in past 1 hr.
TSCparser12   1irons1   1   SPAIN_01   --   0   gaps   (0   missing   messages),   0   seq   no   resets, Total 3 times gapped in past 1 hr.

i think, $0 is being taken from file f2 instead of f1?
# 9  
Old 10-16-2012
Quote:
Originally Posted by sdosanjh
i think, $0 is being taken from file f2 instead of f1?
Yes it is..

If you want to take lines from f1 then just small change..Smilie

Code:
nawk 'FNR==NR{a[$1,$2,$4]=$6;next}{if(a[$1,$2,$4] != ""){s=($6-a[$1,$2,$4]);$6=0;print $0", Total "s" times gapped in past 1 hr."}}' OFS="   " file2 file1

This User Gave Thanks to pamu For This Post:
# 10  
Old 10-16-2012
Quote:
Originally Posted by pamu
Yes it is..

If you want to take lines from f1 then just small change..Smilie

Code:
nawk 'FNR==NR{a[$1,$2,$4]=$6;next}{if(a[$1,$2,$4] != ""){s=($6-a[$1,$2,$4]);$6=0;print $0", Total "s" times gapped in past 1 hr."}}' OFS="   " file2 file1

Thanks Pamu, we are little far from destination now Smilie

Code:
san:/tmp> head -2 f1
TSCparser1      1irons1         1 EMEA_01     -- 5 gaps (7647450 missing messages), 0 seq no resets
TSCparser12     1irons1         1 SPAIN_01    -- 3 gaps (43242430 missing messages), 0 seq no resets

After nawk, 6th col is showing as "0" gaps, while in f1, it is non-zero

Code:
san:/tmp> nawk 'FNR==NR{a[$1,$2,$4]=$6;next}{if(a[$1,$2,$4] != ""){s=($6-a[$1,$2,$4]);$6=0;print $0", Total "s" times gapped in past 1 hr."}}' OFS="   " f2 f1
TSCparser1   1irons1   1   EMEA_01   --   0   gaps   (7647450   missing   messages),   0   seq   no   resets, Total 5 times gapped in past 1 hr.
TSCparser12   1irons1   1   SPAIN_01   --   0   gaps   (43242430   missing   messages),   0   seq   no   resets, Total 3 times gapped in past 1 hr.


Last edited by sdosanjh; 10-16-2012 at 09:41 AM..
# 11  
Old 10-16-2012
As per your previous output i have set this like this..

try this..

Code:
nawk 'FNR==NR{a[$1,$2,$4]=$6;next}{if(a[$1,$2,$4] != ""){s=($6-a[$1,$2,$4]);print $0", Total "s" times gapped in past 1 hr."}}' OFS="   " file2 file1

This User Gave Thanks to pamu For This Post:
# 12  
Old 10-16-2012
Quote:
Originally Posted by sdosanjh
Thanks Pamu, we are little far from destination now Smilie

Code:
san:/tmp> head -2 f1
TSCparser1      1irons1         1 EMEA_01     -- 5 gaps (7647450 missing messages), 0 seq no resets
TSCparser12     1irons1         1 SPAIN_01    -- 3 gaps (43242430 missing messages), 0 seq no resets

After nawk, 6th col is showing as "0" gaps, while in f1, it is non-zero

Code:
san:/tmp> nawk 'FNR==NR{a[$1,$2,$4]=$6;next}{if(a[$1,$2,$4] != ""){s=($6-a[$1,$2,$4]);$6=0;print $0", Total "s" times gapped in past 1 hr."}}' OFS="   " f2 f1
TSCparser1   1irons1   1   EMEA_01   --   0   gaps   (7647450   missing   messages),   0   seq   no   resets, Total 5 times gapped in past 1 hr.
TSCparser12   1irons1   1   SPAIN_01   --   0   gaps   (43242430   missing   messages),   0   seq   no   resets, Total 3 times gapped in past 1 hr.

I tried removing $6=0, that sets it to 0 and it worked.

[CODE]san:/tmp> nawk 'FNR==NR{a[$1,$2,$4]=$6;next}{if(a[$1,$2,$4] != ""){s=($6-a[$1,$2,$4]);$6=0;print $0", Total "s" times gapped in past 1 hr."}}' OFS=" " f2 f1

---------- Post updated at 04:43 AM ---------- Previous update was at 04:42 AM ----------

Quote:
Originally Posted by pamu
As per your previous output i have set this like this..

try this..

Code:
nawk 'FNR==NR{a[$1,$2,$4]=$6;next}{if(a[$1,$2,$4] != ""){s=($6-a[$1,$2,$4]);print $0", Total "s" times gapped in past 1 hr."}}' OFS="   " file2 file1

Thank you Pamu, same i tried Smilie removing $6=0 and it worked.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Nawk Problem - nawk out of space in tostring on

Hi.. i am running nawk scripts on solaris system to get records of file1 not in file2 and find duplicate records in a while with the following scripts -compare nawk 'NR==FNR{a++;next;} !a {print"line"FNR $0}' file1 file2duplicate - nawk '{a++}END{for(i in a){if(a-1)print i,a}}' file1in the middle... (12 Replies)
Discussion started by: Abhiraj Singh
12 Replies

2. Shell Programming and Scripting

Populating File data with custom manipulation on file names

Hi, I am confused how to proceed firther please find the problem below: Input Files: DCIA_GEOG_DATA_OCEAN.TXT DCIA_GEOG_DATA_MCRO.TXT DCIA_GEOG_DATA_CVAS.TXT DCIA_GEOG_DATA_MCR.TXT Output File Name: MMA_RFC_GEOG_NAM_DIM_LOD.txt Sample Record(DCIA_GEOG_DATA_OCEAN.TXT):(Layout same for... (4 Replies)
Discussion started by: Arun Mishra
4 Replies

3. Shell Programming and Scripting

Awk to convert a text file to CSV file with some string manipulation

Hi , I have a simple text file with contents as below: 12345678900 971,76 4234560890 22345678900 5971,72 5234560990 32345678900 71,12 6234560190 the new csv-file should be like: Column1;Column2;Column3;Column4;Column5 123456;78900;971,76;423456;0890... (9 Replies)
Discussion started by: FreddyDaKing
9 Replies

4. UNIX for Dummies Questions & Answers

Filtering records from 1 file based on some manipulation doen on second file

Hi, I am looking for an awk script which should help me to meet the following requirement: File1 has records in following format INF: FAILEd RECORD AB1234 INF: FAILEd RECORD PQ1145 INF: FAILEd RECORD AB3215 INF: FAILEd RECORD AB6114 ............................ (2 Replies)
Discussion started by: mintu41
2 Replies

5. Shell Programming and Scripting

Reformat file using nawk

Hi all, I have a file with records that look something like this, "Transaction ID",Date,Email,"Card Type",Amount,"NETBANX Ref","Root Ref","Transaction Type","Merchant Ref",Status,"Interface ID","Interface Name","User ID" nnnnnnnnn,"21 Nov 2011 00:10:47",someone@hotmail.co.uk,"Visa... (2 Replies)
Discussion started by: dazedandconfuse
2 Replies

6. Shell Programming and Scripting

Help nawk parse file

Hello, Hope you are doing fine. I have a file in following format. I only want to process the data inside the section that comes after #DATE,CODE,VALUE #ITEMS WITH CORRECTIONS ....... #DATE,CODE,VALUE 2011-08-02, ID1, 0.30 2011-08-02, ID2, 0.40 2011-08-02, ID3, 0.50 ...... Means... (3 Replies)
Discussion started by: srattani
3 Replies

7. Shell Programming and Scripting

nawk --- can't open file ??

While i am trying to execute nawk in korn shell iam getting this error. nawk: can't open file $directory../../../filename. When the file is in home directory it is executing. But its not able to find file in other directory. Thanks (2 Replies)
Discussion started by: Diddy
2 Replies

8. Shell Programming and Scripting

Add a column to a file with nawk

Hi all, I'm new at forum, I cant find an answer to my problem so , I need a file which has pipe as a file separator and I need to add a column to a file in the third column I write this code but it s not enough , cat allproblems | nawk'\ BEGIN { FS:"|" } {print $3 $4 $5, ????} ' ... (7 Replies)
Discussion started by: circuitman06
7 Replies

9. Shell Programming and Scripting

how to access values of awk/nawk variables outside the awk/nawk block?

i'm new to shell scripting and have a problem please help me in the script i have a nawk block which has a variable count nawk{ . . . count=count+1 print count } now i want to access the value of the count variable outside the awk block,like.. s=`expr count / m` (m is... (5 Replies)
Discussion started by: saniya
5 Replies

10. UNIX for Advanced & Expert Users

nawk - file limits

Hi, I want to search particular pattern and splitting the file in to multiple files. (Splitted files may be more than 150). It got splitted upto 20 files after that, I got some error. nawk: filename.21 makes too many open files. input record number 654, file xxxxxxx Can u guide me to... (1 Reply)
Discussion started by: sharif
1 Replies
Login or Register to Ask a Question