with using awk i want to know how many uniq 40th field is in asd.txt file and what are…
- User
with using awk i want to know how many uniq 40th field is in asd.txt file and what are they. field seeprator is |
- Assistant
To find out how many unique values are in the 40th field of the file
asd.txt, where the fields are separated by|, and also list what those unique values are usingawk, you can use the following command:bashawk -F"|" '{ uniq[$40]++ } END { for (val in uniq) print val; print "Count:", length(uniq); }' asd.txtExplanation:
-F"|"sets the field separator to|.uniq[$40]++builds an associative array with the 40th field value as the key.- In the
ENDblock:- The
forloop prints all unique keys. length(uniq)prints the number of unique keys.
- The
Sample output format:
codevalue1 value2 value3 Count: 3
If you want the count first and then the list, you can change the order in the
ENDblock:bash - User
i used below code. Now i need to take only field15 = "A" ones. awk -F'|' 'NF >= 40 {print $40}' asd.txt | sort | uniq -c
- Assistant
You want to count and list unique values in the 40th field only when the 15th field equals "A".
You can modify your
awkcommand like this:bashawk -F'|' 'NF >= 40 && $15 == "A" {print $40}' asd.txt | sort | uniq -cExplanation:
NF >= 40ensures the line has at least 40 fields.$15 == "A"restricts to lines where the 15th field equals "A".{print $40}prints the 40th field from those filtered lines.sort | uniq -csorts and counts occurrences of each unique value.
If you want just the unique values and their count (without sorting by count), this is usually the way. If you want to sort by count decreasing:
bashawk -F'|' 'NF >= 40 && $15 == "A" {print $40}' asd.txt | sort | uniq -c | sort -nrIf you want the number of unique 40th field values that meet the condition:
bash