Count The Number Of Delimiters using awk or better

What to know the way to count the number of delimiters in each record by ignoring the escape delimiters.

 

Sample Data:
12345678|ABN\|XYZ MED CHEM PTY. LTD.|C||100.00|22|AB"C\|Corp|"XYZ|CDEF"|

I'm using awk -F'|' '{ print NF-1 }' command to find the number of delimiters. this command considering escape character delimiters(\|) also into consideration and returning number of delimiters as 11. 

Excepted Output: 9 

Please help me in this.

 echo '12345678|ABN\|XYZ MED CHEM PTY. LTD.|C||100.00|22|AB"C\|Corp|"XYZ|CDEF"|' | sed -e 's/\\|/ /g' | awk -F\| '{print NF}'

Note: expected output is 10, it doesn't seem you have counted the delimeters correctly.

why do you subract 1?

Hi, for fun, in bash:

$ echo $STRING 
12345678|ABN\|XYZ MED CHEM PTY. LTD.|C||100.00|22|AB"C\|Corp|"XYZ|CDEF"|
$ X=${STRING//\\|/} && X=${X//[^|]/} && echo ${#X}
9

Regards.

1 Like

Thanks for the quick response. can you please provide the same in awk command.

With awk:

$ echo $STRING | awk -F'|' '{$NF=$NF"\\";X=NF;for(i=1;i<=NF;++i){ $i ~ /\\$/ && --X};print X}'
9

Regards.

1 Like

Thanks for helping me out in it.

How can I extract the data also in the below specified expected format using awk

Sample Data: 12345678|ABN\|XYZ MED CHEM PTY. LTD.|C||100.00|22|AB"C\|Corp|"XYZ|CDEF"|

Expected Output: 

12345678
ABN\|XYZ MED CHEM PTY. LTD.
C

100.00
22
AB"C\|Corp
"XYZ
CDEF"

Try

awk -F"|" '{gsub (/\\\|/,"\001");$1=$1;gsub ("\001", "\\|")}1' OFS="\n" file4
12345678
ABN\|XYZ MED CHEM PTY. LTD.
C

100.00
22
AB"C\|Corp
"XYZ
CDEF"


1 Like

Thanks for your quick suggestions. How to shuffle the columns as well

Sample Data:
Sample Data: 12345678|ABN\|XYZ MED CHEM PTY. LTD.|C||100.00|22|AB"C\|Corp|"XYZ|CDEF"|

Expected Output Data: 12345678|C|ABN\|XYZ MED CHEM PTY. LTD.|22|AB"C\|Corp||100.00|"XYZ|CDEF"|