awk delimiter count if mismatched?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk delimiter count if mismatched?
# 1  
Old 04-14-2016
awk delimiter count if mismatched?

Hi all,

I have a file where in it has lot of records in it.
I have written below stuff to find the number of fields as shown below

Code:
Code:
`awk -F '|' '{print NF-1}' file.txt| head -1`

how do i proceed if in case any record in particular is having more number of delimiters, if it having??? what is the record content (need to print that particular record)?

for example

I have content of table

Code:
Code:
id|name|desc
1|Raj|null|
2|Nick|NA
0|Digu|fine with it
3|NA|go ahead
0|Digu|fine with it
100|Digu|fine with it
4|Duvi|baby

My o/p should be

Code:
Code:
1|Raj|null|

as it is having extra delimiter

Last edited by nikhil jain; 04-14-2016 at 01:27 PM..
# 2  
Old 04-14-2016
Try:
Code:
awk -F '|' 'NR==1{n=NF}NF>n' file

or if it is either less or more:
Code:
awk -F '|' 'NR==1{n=NF}NF!=n' file

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 04-14-2016
thanks a lot Smilie how can i ignore the escaped delimiter

for ex
Code:
Code:
2|Nick|NA\|

It should ignore this kind as there are chances that "|" is part of the record as well not only delimiter.
# 4  
Old 04-14-2016
Try
Code:
awk -F '|' 'NR==1 {n=NF} NF-gsub(/\\\|/, "&")!=n' file
1|Raj|null|

This User Gave Thanks to RudiC For This Post:
# 5  
Old 04-14-2016
thanks a lot sir Smilie
# 6  
Old 04-14-2016
Hello Nikhil,

In case you need only those lines which are having more number of delimiters following may help you. Taken code from RudiC's suggestion with a minor edit in it as follows.
Code:
awk -F '|' 'NR==1 {n=NF} NF-gsub(/\\\|/, "&")>n'  Input_file

So let's say Input_file is as follows:
Code:
cat Input_file
id|name|desc
1|Raj|null|
2|Nick|NA
0|Digu|fine with it
3|NA|go ahead
0|Digu|fine with it
100|Digu|fine with it
4|Duvi|baby
4|Duvi

Then only line 1|Raj|null| will be printed by above code, hope this helps you too.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 7  
Old 04-15-2016
thanks a lot ravinder Smilie

One more thing, I will validate the number of delimiters based on the very first row of the file that is the header. So can we include that as well in the check with the script u gave.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Comparing two files and writing mismatched rows along with mismatched columns. Pointing out the mism

I got a requirement where I need to compare two files wrt to each columns and write the corresponding difference in another file along with some identification showing mismatched columns. Pointing out the mismatched columns is my main problem statement. For example we have files like: File 1 ... (8 Replies)
Discussion started by: piyush pankaj
8 Replies

2. Shell Programming and Scripting

Split files based on row delimiter count

I have a huge file (around 4-5 GB containing 20 million rows) which has text like: <EOFD>11<EOFD>22<EORD>2<EOFD>2222<EOFD>3333<EORD>3<EOFD>44<EOFD>55<EORD>66<EOFD>888<EOFD>9999<EORD> Actually above is an extracted file from a Sql Server with each field delimited by <EOFD> and each row ends... (8 Replies)
Discussion started by: amvip
8 Replies

3. Shell Programming and Scripting

Linux shell script to insert new lines based on delimiter count

The input file is a .dat file which is delimited by null (^@ in Linux). On a windows PC it looks something like this (numbers are masked with 1). https://i.imgur.com/nta2Gqp.jpg The entire file is in one row but it has multiple records - each record contains 80 fields i.e. there are 81 counts... (9 Replies)
Discussion started by: digitalnirvana
9 Replies

4. Shell Programming and Scripting

Count delimiter(~|*) each row in a file and return 1 or 0

Hi I want to check delimiter in file. Delimiter in my file is ~|* sample of file : ABC~|*edgf~|*T1J333~|*20121130 ABC~|*sdaf~|*T1J333~|*20121130 ABC~|*fsdg~|*T1J333~|*20121130 ABC~|*dfsg~|*T1J333~|*20121130 in this i want to count number delimiter occur is 4 in each row if count is... (21 Replies)
Discussion started by: MOHANP12
21 Replies

5. Shell Programming and Scripting

Skip the delimiter with in double quotes and count the number of delimiters during data extract

Hi All, I'm stuck-up in finding a way to skip the delimiter which come within double quotes using awk or any other better option. can someone please help me out. Below are the details: Delimited: | Sample data: 742433154|"SYN|THESIS MED CHEM PTY.... (2 Replies)
Discussion started by: BrahmaNaiduA
2 Replies

6. Shell Programming and Scripting

Delimiter count

Hi I wrote shell script for count the deleimeters and send it to reject file which are exeeseding number of count. But script is not working properly... Can you please check the below script and modify it as required. ex : 1,abc 2,mno 3,pqr 4,xyz,error As per the requiredment last... (3 Replies)
Discussion started by: victory
3 Replies

7. Shell Programming and Scripting

awk - count character count of fields

Hello All, I got a requirement when I was working with a file. Say the file has unloads of data from a table in the form 1|121|asda|434|thesi|2012|05|24| 1|343|unit|09|best|2012|11|5| I was put into a scenario where I need the field count in all the lines in that file. It was simply... (6 Replies)
Discussion started by: PikK45
6 Replies

8. Shell Programming and Scripting

awk delimiter

I have several hundred files that look like this: ABB110405110000.rawfma8 AAB110405110001.rawhr32 If I use the statement ls | awk '{x=$0;gsub("","",x) I see that all characters other than 0-9 are eliminated but how do I eliminate all characters including numbers after the "." In my mind I... (1 Reply)
Discussion started by: rdburg
1 Replies

9. Shell Programming and Scripting

Count delimiter and replace

Hi, I have below sample data file. I want to count the delimiter no of times repeated and replace with new line characters. The new line characters appear somtimes in between of the records and end of the record: input file: jack;1500;manager;boston\n jim;2000;\n developer;texas\n... (7 Replies)
Discussion started by: dsubha
7 Replies

10. Shell Programming and Scripting

Delimiter count in a string.

Hi , I need to get the delimiter "-" count in a particular string. string= SYS_NAME-123-S5-2008-10-20.LOG the delimit "-" count is 5 . Using sed or awk can I know the count ? I have seen how to get the count for delimiter in a file but not a string :( Thanks, Priya (8 Replies)
Discussion started by: priyam
8 Replies
Login or Register to Ask a Question