0

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.

1
  • 2
    Please edit your question to add (a part of) your input and your expected output. Commented Feb 7, 2019 at 21:41

1 Answer 1

1
$ 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).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.