0

I'm creating a shellscript that will print out various filetypes we have in our directory. It pretty much works, however, for some odd reason when I try to use uniq on my output, it doesn't work. This is my input (and value of $FILE_TYPE)

POSIX shell script, ASCII text executable
ASCII text
Bourne-Again shell script, ASCII text executable
UTF-8 Unicode text, with overstriking
Bourne-Again shell script, ASCII text executable

However when I use

FILE_TYPE_COUNT=`echo "$FILE_TYPE" | sort | uniq -c`

this is the result it prints

  1 POSIX shell script, ASCII text executable
  1 ASCII text
  1 Bourne-Again shell script, ASCII text executable
  1 UTF-8 Unicode text, with overstriking
  1 Bourne-Again shell script, ASCII text executable

Obviously it should be

  1 POSIX shell script, ASCII text executable
  1 ASCII text
  2 Bourne-Again shell script, ASCII text executable
  1 UTF-8 Unicode text, with overstriking

Any idea what I'm doing wrong?

closed as unclear what you're asking by ctrl-alt-delor, RalfFriedl, sam, msp9011, grochmal Dec 28 '18 at 12:40

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Note shell variables by convention are lowercase. – ctrl-alt-delor Dec 27 '18 at 0:19
  • 1
    There is missing code. How can we help, if you don't show a minimum, but sufficient code, to show the problem. – ctrl-alt-delor Dec 27 '18 at 0:24
5

You're not sorting the file before filtering it. From the manpage:

Note: uniq does not detect repeated lines unless they are adjacent. You may want to sort the input first, or use sort -u without uniq. Also, comparisons honor the rules specified by LC_COLLATE.

You also need to process all the lines you want to count in one go. Currently, you're processing file types one at a time, so uniq -c correctly tells you you have one of each — it only ever sees one file type at a time.

file * | sort | uniq -c

would be more appropriate (presumably with a more specific glob, or even a list of files to process).

  • Thanks somehow I overlooked it, however since that is STDOUT of a certain echo, sort -u doesnt work on it. Any idea how could I sort it out? basically it's FILE_TYPE=file -b "$MY_FILE" in a for loop echo'd out, so FILE_TYPE_COUNT=echo "$FILE_TYPE" | sort -u sadly doesnt work on it – Rawrplus Mar 19 '17 at 18:58
  • 2
    @Rawrplus You need to pass the whole output to a call to sort, rather than pass each line individually to a separate.call to sort. If you don't understand how to do that, ask a new question with your actual code (the full script, not just one line). – Gilles Mar 19 '17 at 22:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.