Count blank fields in every line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Count blank fields in every line
# 1  
Old 07-11-2013
Count blank fields in every line

Hello All,

I am trying a one liner for finding the number of null columns in every line of my flat file.

The format of my flat file is like this

Code:
a|b|c|d||||e|f|g|
a|b|c|d||||e|f|g|

I want to count the number of fields delimited by "|" which are blank.
In above case the count should be 3

The command tried by me is
Code:
cat filename | awk -F"|" '{print NF}'| head -1`

The is just giving me the count of all fields.

Please help me doing this
# 2  
Old 07-11-2013
try..

Code:
 
awk -F"|" '{for(i=0;i<=NF;i++){if(!$i){C++}};print C;C=0}' filename

# 3  
Old 07-11-2013
Thanks Vidyadhar,

I tried you solution and it works like a charm Smilie

I tried getting the count for first line only

Code:
cat filename | awk -F"|" '{for(i=0;i<=NF;i++){if(!$i){C++}};print C;C=0}' | head -1

This works exactly as it should be.

Now, How do I get the count for a specific line number in a file.
Is there anyway we can do this.

Thanks
# 4  
Old 07-11-2013
yes you can.. say you want to check it for line number 20

Code:
 
awk -F"|" 'NR==20{for(i=0;i<=NF;i++){if(!$i){C++}};print (C-1);C=0}' filename

I guess you might wanna make it C-1
# 5  
Old 07-11-2013
Hello Vidyadhar,

I tried this one, This one is giving me count 1 less then the the previous command posted by you.
Previous command was working fine and showing me right number of blank fields

Also can you please let me know, what C-1 indicates here
# 6  
Old 07-11-2013
never mind please use it ac C and not C-1 Smilie
# 7  
Old 07-11-2013
Thanks Vidyadhar
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

In a file, replace blank line by the last line not blank above

Dear All, In a CSV file, say that a given column has been extracted. In that column, information is missing (i.e. blank lines appear). I would like to replace the blank lines by the last valid line (not blank) previously read. For example, consider the extract below: 123 234 543 111... (7 Replies)
Discussion started by: bagvian
7 Replies

2. Shell Programming and Scripting

Print . in blank fields to prevent fields from shifting

The below code works great, kindly provided by @Don Cragun, the lines in bold print the current output. Since some of the fields printed can be blank some of the fields are shifted. I can not seem too add . to the blank fields like in the desired output. Basically, if there is nothing in the field... (10 Replies)
Discussion started by: cmccabe
10 Replies

3. Shell Programming and Scripting

Count the pipes "|" in line and delete line if count greter then number.

Hello, I have been working on Awk/sed one liner which counts the number of occurrences of '|' in pipe separated lines of file and delete the line from files if count exceeds "17". i.e need to get records having exact 17 pipe separated fields(no more or less) currently i have below : awk... (1 Reply)
Discussion started by: ketanraut
1 Replies

4. Shell Programming and Scripting

Can ksh read records with blank fields

I have a tab delimited file with some fields potentially containing no data. In ksh 'read' though treats multiple tabs as a single delimiter. Is there any way to change that behavior so I could have blank data too? I.e. When encountering 2 tabs it would take it as a null field? Or do I have to... (3 Replies)
Discussion started by: benalt
3 Replies

5. Shell Programming and Scripting

How to search for blank fields in a text file from a certain position?

Sample txt file : OK00001111112| OK00003443434|skjdaskldj OK32812983918|asidisoado OK00000000001| ZM02910291029|sldkjaslkjdasldjk what would be the shell script to figure out the blank space (if any) after the pipe sign? (4 Replies)
Discussion started by: chatwithsaurav
4 Replies

6. 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

7. Shell Programming and Scripting

shell to find the count fields of each line

hi, i've many unload files with delimiter '|'. I'm trying to load them to the specific tables from those unl's. The problem here is, some unl's are corrupted. To be exact, some files doesnt seem to have the exact number of fields as in the table. So im trying to identify the corrupted... (6 Replies)
Discussion started by: dvah
6 Replies

8. Shell Programming and Scripting

Replace two blank line with a single blank line

Hi Guys, I have a file in which each set of records are separated by two blank line. I want to replace it with a single blank line. Can you guys help me out? Regards, Magesh (9 Replies)
Discussion started by: mac4rfree
9 Replies

9. Shell Programming and Scripting

remove blank spaces from fields

Hi Friends, I have large volume of data file as shown below. Beganing or end of each filed, there are some blank spaces. How do I remove those spaces? AAA AAA1 | BBB BB1 BB2 |CC CCCC DDDD DD | EEEEEEE EEEEEEEE | FFF FFFFFF FFFF GG GGGGGG |HH HH ... (3 Replies)
Discussion started by: ppat7046
3 Replies

10. Shell Programming and Scripting

how to include field separator if there are blank fields?

Hi, I have the following data in the format as shown (note: there are more than 1 blank spaces between each field and the spaces are not uniform, meaning there can be one blank space between field1 and field2 and 3 spaces between field3 and field4, in this example, # are the spaces in between... (19 Replies)
Discussion started by: ReV
19 Replies
Login or Register to Ask a Question