Question

I have a file as below

"a"|"a|b|c|d"|"as|ds"

I want to count total number of "|" to get the exact column count.

Was it helpful?

Solution 3

awk '{print gsub(/\"\|\"/,"&",$0)}' input.txt

Prints number of substitutions of "|" i.e count of it, so

awk '{print gsub(/\"\|\"/,"&",$0) +1 }' input.txt

Will be number of columns.

"&" means substitute the matched string with itself, i.e no change.


EDIT

Or use

awk -F'["][|]["]' '{print NF}' input.txt

Which prints number of columns and this

awk -F'["][|]["]' '{print NF-1}' input.txt

is the number of "|"

OTHER TIPS

awk -F'"[|]"' '{print NF}'

will return the number of columns (NF is a built in variable returning the number of fields)

edit: surround the pipe with [] to escape it and it works correctly.

If you have gnu awk version 4+, you can use FPAT to specify how the data looks like, and then count.

echo '"a"|"a|b|c|d"|"as|ds"' | awk '{print NF}' FPAT='"([a-z]+[|]*)+"'
3

To see the column:

echo '"a"|"a|b|c|d"|"as|ds"' | awk '{print "1="$1,"2="$2,"3="$3}' FPAT='"([a-z]+[|]*)+"'
1="a" 2="a|b|c|d" 3="as|ds"

But since FS works, I would have used awk -F'"[|]"' '{print NF}'

You can try the following perl script:

#! /usr/bin/perl

use warnings;
use strict;
use Text::CSV;


my $csv = Text::CSV->new({ sep_char => '|' });

while (<>) {
   $csv->parse($_);
   my @fields = $csv->fields();
   print scalar @fields,"\n";
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top