Print records which do not have expected number of fields in a comma delimited file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print records which do not have expected number of fields in a comma delimited file
# 1  
Old 01-31-2012
Print records which do not have expected number of fields in a comma delimited file

Hi,

I have a comma (,) delimited file, in which few fields are enclosed with in double quotes " ". I have to print the records in the file which donot have expected number of field with the line number.

Code:
 
File1
====
name,desgnation,doj,project #header#
asath,se,15-06-2010,asc,"india,mumbai"
rif,sse,12-05-2010,asc
si,tl,01-12-2009,asc
mthr,"ase,trans",15-09-2010
sdu,ase,15-09-2010,bench
 
Here lets say me expected number of field is 4. And in the above input, record 2 has 5 fields and record 5 as 2 fields. I have to print the two records with line number.
 
Expected output
 
File1_fldmismatchrcrds
==================
2 asath,se,15-06-2010,asc,"india,mumbai"
5 mthr,"ase,trans",15-09-2010

I tried the below code

Code:
 awk -F "," '{gsub(/"[^"]*"/,x);if (NF != '4'){print NR,$0}}' File1

This gives me the output as
Code:
2 asath,se,15-06-2010,asc,
5 mthr,,15-09-2010



Can someone please help on this issue?
Smilie
# 2  
Old 01-31-2012
awk -F "," '/"/{print NR,$0}' file
This will help you.

Output-:
2 asath,se,15-06-2010,asc,"india,mumbai"
5 mthr,"ase,trans",15-09-2010
# 3  
Old 01-31-2012
Hi Could you please explain how this works?
This returns me the rows in which there are double quoted (" ") but what I require is I have to print any record (with / without double quotes enclosed data) in it if the number of field is less than expected number of field.
The above code prints the records with double quote enclosed data alone. Irrepective of the number of fields.
# 4  
Old 01-31-2012
Try preserving the record first:
Code:
awk -F, '{p=$0;gsub(/"[^"]*"/,x)} NF!=4{print NR,p}' file

# 5  
Old 01-31-2012
Code:
/pattern/ {statements}

is a pattern match and the function or series of statments to invoke if the pattern matches
ie., a boolean result (true or false)

Code:
# expected number is 4
# this prints the record number if there are fewer than 5 fields
# and if there is a quote somewhere on the line
awk -F ','   ' /"/ && NF!=4 {print NR,$0}' file

I do not get exactly what the criteria for printing are supposed to be. So this is just giving you what it seems you want....
# 6  
Old 01-31-2012
Thanks. It worked!! Smilie
Code:
awk -F, '{p=$0;gsub(/"[^"]*"/,x)} NF!=4{print NR,p}' file


Last edited by machomaddy; 01-31-2012 at 09:25 AM..
# 7  
Old 01-31-2012
Hi machomaddy
Are you not getting the output what you want. If not please show me the output you want
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Need help on an old post - How to convert a comma delimited string to records or lines of text?

Hi, Apologies in advance to the moderator if I am posting this the wrong way. I've searched and found the solution to an old post but as it is a very old post, I don't see an option to update it with additional question. The question I have is in relation to the following post: How to... (6 Replies)
Discussion started by: newbie_01
6 Replies

2. UNIX for Dummies Questions & Answers

Make all records with the same number of fields (awk)

Hi, input: AA|BB|CC DD|EE FF what I am trying to get: AA|BB|CC DD|EE| FF|| I tried to create first an UDF for printing repeats, but I think I have an issue with my END section or my array: function repeat(str, n, rep, i) { for(i=1 ;i<n;i++) rep=rep str return rep } ... (6 Replies)
Discussion started by: beca123456
6 Replies

3. UNIX for Dummies Questions & Answers

How to convert a comma delimited string to records or lines of text?

Hi, I am not sure if I've posted this question before. Anyway, I previously asked about converting lines of text into a comma delimited string. Now I am needing to do the other way around ... :( :o Can anyone advise how is this possible? Example as below: Converting records/lines to... (2 Replies)
Discussion started by: newbie_01
2 Replies

4. UNIX for Dummies Questions & Answers

appending a command to print a file in a comma delimited format

Hi everyone, i have a file that I had grep'd from something else lets call it file1.txt which consists variable files and lines due to different scenarios/inputs 1782 9182 fe35 ac67 how can I print this in this manner? 1782,9182,fe35,ac67 also if i had piped the new output... (2 Replies)
Discussion started by: prodigy06
2 Replies

5. Shell Programming and Scripting

Count number of column in a comma delimited file

I have a comma (,) delimited file. 106232145,"medicare","medicare,medicaid",789 I would like to count the number of fields in each line. I tried the below code awk -F ',' '{print NF-1}' This returns me the result as 5 instead of 4. This is because the awk takes... (9 Replies)
Discussion started by: machomaddy
9 Replies

6. UNIX for Advanced & Expert Users

Problem while counting number of fields in TAB delimited file

I'm facing a strange problem, please help me out. Here we go. I want to count number of fields in particular file. filename and delimiter character will be passed through parameter. On command prompt if i type following i get 27 as output (which is correct) cat customer.dat | head -1 | awk... (12 Replies)
Discussion started by: vikanna
12 Replies

7. UNIX for Dummies Questions & Answers

AWK ??-print for fields within records in a file

Hello all, Would appreciate if someone can help me out on the following requirement. INPUT FILE: -------------------------- TPS REPORT abc def ghi jkl mon pqr stu vrs lll END OF TPS REPORT TPS REPORT field1 field2 field3 field4 field5 field6 (8 Replies)
Discussion started by: hyennah
8 Replies

8. UNIX for Dummies Questions & Answers

Sort the fields in a comma delimited file

Hi, I have a comma delimited file. I want to sort the fields alphabetically and again store them in a comma delimited file. For example, My file looks like this. abc,aaa,xyz,xxx,def pqr,ggg,eee,iii,qqq zyx,lmo,pqr,abc,fff and I want my output to look like this, all fields sorted... (3 Replies)
Discussion started by: swethapatil
3 Replies

9. Shell Programming and Scripting

Parse apart strings of comma separated data with varying number of fields

I have a situation where I am reading a text file line-by-line. Those lines of data contain comma separated fields of data. However, each line can vary in the number of fields it can contain. What I need to do is parse apart each line and write each field of data found (left to right) into a file.... (7 Replies)
Discussion started by: 2reperry
7 Replies

10. Shell Programming and Scripting

Converting Tab delimited file to Comma delimited file in Unix

Hi, Can anyone let me know on how to convert a Tab delimited file to Comma delimited file in Unix Thanks!! (22 Replies)
Discussion started by: charan81
22 Replies
Login or Register to Ask a Question