LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Count occurrence of character in field and print in a new field (https://www.linuxquestions.org/questions/linux-newbie-8/count-occurrence-of-character-in-field-and-print-in-a-new-field-934525/)

Trd300 03-15-2012 12:44 AM

Count occurrence of character in field and print in a new field
 
%%%%%

grail 03-15-2012 03:10 AM

Something like:
Code:

awk 'BEGIN{OFS="|"}{if(NR == 1)$6 = "Field6";else $6 = 0}NR > 1 && $4 ~ /x/{$6 = split($4,_,"x") - 1}1' file

Trd300 03-15-2012 04:36 AM

Thanks grail !

I see what you mean. It is a bit like using the character I am looking for as FS and count the number of field.

Like:
Code:

awk 'BEGIN {FS="x"} {print $0, NF-1}' file
Except that the split option restricts the move to the field only.
I didn't know that the split option returns the number of array by default.

Thanks again grail !

Trd300 03-20-2012 03:51 AM

awk: sort variable value and assign a name accordingly
 
error

grail 03-20-2012 04:32 AM

error???? Would you like to provide more information?

Trd300 03-21-2012 07:57 PM

No no, it works !

I didn't write on the good thread, my mistake !


All times are GMT -5. The time now is 08:46 AM.