سؤال

For some reason, uniq seem to be recognizing the newline character when comparing different lines, and recognizing the files newline differently than \n (or at least that's what it seems like to me).

What I want it to do is take a file like this:

hello world hello hello
meh
hello
hello

and get output like:

5 hello
1 world
1 meh

But my code:

x=`sed 's/ /\n/g' $1 | uniq -c | sed 's/      //g'`
echo "$x"

is outputting this:

1 hello
1 world
2 hello
1 meh
2 hello
هل كانت مفيدة؟

المحلول

uniq expects sorted input.

$ sed 's/ /\n/g' f | sort | uniq -c | sed 's/      //g'
5 hello
1 meh
1 world
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top