Filtering issues while using nawk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Filtering issues while using nawk
# 1  
Old 11-25-2010
Filtering issues while using nawk

Hi,

I am currently filtering a file that has multiple sets of data. An example of some of the data is as follows;

Code:
Sat Oct  2 07:42:45 2010    01:33:46 R1_CAR_12.34
Sun Oct  3 13:09:53 2010    00:02:34 R2_BUS_56.78
Sun Oct  3 21:11:39 2010    00:43:21 R3_TRAIN_COACH_90.12
Mon Oct  4 06:07:10 2010    00:01:50 R4_TRAIN_CARRAIGE_34.56X
Tues Oct  5 07:10:30 2010    00:12:50 R4_TRAIN_ENGINE_12.5.6X

The command i am using to filter the data is as follows;

Code:
nawk '{n=split($NF,a,"[_.]");print $1,$2,$3,$5,$6,a[n-2],(/[A-Za-z]$/)?substr($0,length):""}' OFS=, $FileName

This command works perfectly until it encounters data sets with this format;

Code:
Tues Oct  5 07:10:30 2010    00:12:50 R4_TRAIN_ENGINE_12.5.6X

Instead of the desired output;

Code:
Tues,Oct,5,2010,00:12:50,ENGINE,X

I am getting the following;

Code:
Tues,Oct,5,2010,00:12:50,12,X

I think the error lies within the split command within the nawk but im not too sure.

Anybody have any ideas on how to fix this error?

Any help would be greatly appreciated.

Thanks in advance
# 2  
Old 11-25-2010
Code:
#  nawk '{n=split($NF,a,"[_.]");print $1,$2,$3,$5,$6,a[3],(/[A-Za-z]$/)?substr($0,length):""}' OFS=, infile
Sat,Oct,2,2010,01:33:46,12,
Sun,Oct,3,2010,00:02:34,56,
Sun,Oct,3,2010,00:43:21,COACH,
Mon,Oct,4,2010,00:01:50,CARRAIGE,X
Tues,Oct,5,2010,00:12:50,ENGINE,X

this works for your sample, but may have other effects.

Issue is that there are differing numbers of fields defined with your split after the word you appear to be looking for...

Code:
i.e.
R4_TRAIN_CARRAIGE_    34   56X
R4_TRAIN_ENGINE_        12  5      6X

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

Need help on filtering

Hi experts, I have a file image.csv as below: COMPUTERNAME,23/07/2013,22/07/2013,21/07/2013,20/07/2013,19/07/2013,18/07/2013,17/07/2013 AED03852180,3,3,3,3,3,3,3 AED03852181,3,3,3,3,3,3,1 AED09020382,3,0,3,0,3,3,3 AED09020383,1,3,3,3,2,1,3 AED09020386,3,3,0,3,3,0,3 ... (4 Replies)
Discussion started by: zaq1xsw2
4 Replies

3. Shell Programming and Scripting

Filtering

Hi I am interested in DNS resolving a set of sites and each time the output is different- $ host www.yahoo.com www.yahoo.com is an alias for fd-fp3.wg1.b.yahoo.com. fd-fp3.wg1.b.yahoo.com is an alias for ds-fp3.wg1.b.yahoo.com. ds-fp3.wg1.b.yahoo.com is an alias for... (1 Reply)
Discussion started by: jamie_123
1 Replies

4. AIX

Need help with filtering

Hi!! I have a bit of a task here and filtering/scripting not my strongest. I have to collect info of approx 1100 hdiskpower.so i have appended all the hdisk into a text file and i need it to run the command lscfg -vl to confirm if the drive is symmetrix. here's what i have so far at... (3 Replies)
Discussion started by: vpundit
3 Replies

5. Shell Programming and Scripting

Filtering Issues Using sed and awk

Hi, I am currently using the sed and awk commands to filter a file that has multiple sets of data in different columns. An example of part of the file i am filtering is as follows; Sat Oct 2 07:42:45 2010 01:33:46 R1_CAR_12.34 Sun Oct 3 13:09:53 2010 00:02:34 R2_BUS_56.78 Sun... (4 Replies)
Discussion started by: crunchie
4 Replies

6. Shell Programming and Scripting

Filtering issues with multiple columns in a single file

Hi, I am new to unix and would greatly appreciate some help. I have a file containing multiple colums containing different sets of data e.g. File 1: John Ireland 27_December_69 Mary England 13_March_55 Mike France 02_June_80 I am currently using the awk... (10 Replies)
Discussion started by: crunchie
10 Replies

7. Shell Programming and Scripting

Nesting - two nawk into one nawk

hi people; this is my two awk code: nawk '/cell+-/{r=(NF==8) ? $4FS$5FS$6 : NF==7 ? $4FS$5 : $4 ;c=split(r,rr);for (i=1;i<=c;i++){if(rr != "111111"){printf($3" %d ""\n",(i+3))}}printf("")}' /home/gc_sw/str.txt > /home/gc_sw/predwn.txt nawk -F'*' '{gsub(/ *$/,"")}$0=$1$($NF-2)'... (2 Replies)
Discussion started by: gc_sw
2 Replies

8. Shell Programming and Scripting

Issues with filtering duplicate records using gawk script

Hi All, I have huge trade file with milions of trades.I need to remove duplicate records (e.g I have following records) 30/10/2009,trdeId1,..,.. 26/10/2009.tradeId1,..,..,, 30/10/2009,tradeId2,.. In the above case i need to filter duplicate recods and I should get following output.... (2 Replies)
Discussion started by: nmumbarkar
2 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 Dummies Questions & Answers

AWK (NAWK) and filtering values

Hi , i try to filter input file : 17/04/2008 06:17:09 17/04/2008 00:00:02 keeping lines with hour > 06 as : 17/04/2008 06:17:09 i tried : CSL=06 nawk -v CSL="${CSL}" -F'' '/^\[/ { if ( $4 -gt $CSL) print $0 } ; /^\>/ { if ( $5 -gt $CSL) print $0 }' input_file.txt... (11 Replies)
Discussion started by: Nicol
11 Replies
Login or Register to Ask a Question