Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count number of column in a pipe delimited file

I have a pipe | delimited file.

File:

106232145|"medicare"|"medicare,medicaid"|789

I would like to count the number of fields in each line. I tried the below code

Code:

awk -F '|' '{print NF-1}'

This returns me the result as 5 instead of 4. This is because the awk takes "medicare|medicaid" as two different fields instead of one field

like image 940
Maulzey Avatar asked Jul 09 '13 21:07

Maulzey


People also ask

How to count the number of pipes in a text file?

In the query editor M language, you could try one of these options to count the pipes: Text.Length (Text.Select ( [Column], {"|"})) List.Count (Text.Split ( [Column], "|")) If you're doing this in DAX, you can use the old Excel trick of replacing the pipes with an empty string and counting how much shorter your text is.

How do I find the number of columns in a file?

If your file is big but you are certain that the number of columns remains the same for each row (and you have no heading) use: head -n 1 FILE | awk '{print NF}' to find the number of columns, where FILE is your file name. To find the number of lines 'wc -l FILE' will work.

How to count the columns of the first line in Bash?

take the first line, change tabs (or you can use ',' instead of ' ' for commas), count the number of lines. A very simple way to count the columns of the first line in pure bash (no awk, perl, or other languages): This will work if your data are formatted appropriately. Following code will do the job and will allow you to specify field delimiter.

How to count the number of lines in a file?

Simple row count is $ (wc -l "$file"). Use $ (wc -lL "$file") to show both the number of lines and the number of characters in the longest line. True. Silly of me to assume this was obvious: wc -l file |cut -f 1. @Tim Sylvester: You know UUOC is about the wasted process, right? I'm tempted to pass it back to you for that cut ;)


1 Answers

awk -F\| '{print NF}'

gives correct result.

like image 187
unxnut Avatar answered Sep 23 '22 13:09

unxnut