0

I have a file with pipe delimiter and one record has more columns than expected.

For example:

File NPS.txt

1|a|10
2|b|20
3|c|30
4|d|40|old

The last column has more columns than expected and I want to know the line number to understand what the problem is.

I found this command:

awk -F\; '{print NF}' NPS.txt | sort | uniq -c  

With this command I know that one columns has one column added but I do not know which one is.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38

1 Answers1

0

I would use a bash script a) Define a counter variable, starting at 0, b) iterate over each line in your file, adding +1 to the counter at the beginning of each loop, c) split each line into an array based on the "|" delimiter, logging the counter # if the array contains more than 3 elements. you can log to console or write to a file.

It's been awhile since I've scripted in Linux, but these references might help: Intro: https://www.guru99.com/introduction-to-shell-scripting.html For Loops: https://www.cyberciti.biz/faq/bash-for-loop/ Bash String Splitting How do I split a string on a delimiter in Bash? Making Scripts Executable https://www.andrewcbancroft.com/blog/musings/make-bash-script-executable/

There may be a good one-liner out there, but it's not a difficult script to write.

jomiwi
  • 1