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
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.
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.
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.
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 ;)
awk -F\| '{print NF}'
gives correct result.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With