Counting No. delimiter per line in a file in Unix

I have a 'records.txt' file that contains over 200,000 records.

Each entry is on a separate line and has several fields separated by a separator '|'.

Each line should have 35 fields, but the problem is that one of these lines has <> 35 fields, i.e. <> 35 '|' characters.

Can anyone suggest a way on Unix with which I can define a string. (How to get the number of characters '|' in each line of a file)

+3
source share
3 answers

Try the following:

awk -F '|'  'NF != 35 {print NR, $0} ' your_filefile
+11
source

perl script :

cat records.txt | perl -ne '$t = $_; $t =~ s/[^\|]//g; print unless length($t) == 35;'

, , |, , .

+1

Greg using bash stuff for bash friends :)

while read n; do [ `echo $n | tr -cd '|' | wc -c` != 35 ] && echo $n; done < records.txt
+1
source

All Articles