I have a .TXT file in UNIX with 1 million records in it which is a Pipe delimited, i would like to count the number of occurences of '|' in the 2nd record. Can someone please help me out.
Thanks in advance.
I have a .TXT file in UNIX with 1 million records in it which is a Pipe delimited, i would like to count the number of occurences of '|' in the 2nd record. Can someone please help me out.
Thanks in advance.
$ awk -F'|' 'NR==2{print NF-1; exit}' file
split the record with the defined field separator, the count of separators is one less than the number of fields
or
$ awk 'NR==2{print gsub("\\|","|")}' file
replace all occurrences of | and print the count. Since | is a special regex character you need to escape it (and escape the escape char as well).