sort Find how many distinct values are among the certain column

8,183

Solution 1

I would start with cut since you only care about uniqueness in column 2:

cut -d' ' list.txt

results in:

1
2
1
3
6
7
6

Now you want unique values; uniq will do that, but only if it's sorted. If you're going to sort, go ahead and use sort's -u flag:

cut -d' ' -f2 list.txt | sort -u

Results in:

1
2
3
6
7

and now you can use wc to count the number of lines of output:

cut -d' ' -f2 list.txt | sort -u  | wc -l

which gives you:

5

Note that we're relying a specific format for the list.txt file -- no spaces in people's names!

Solution 2

I would go with:

sort -k2,2 -u names | wc -l
5

Where names has this content:

cat names
Joe 3
Jack 1
Ulysses 6
Fox 2
Cassidy 1
Jones 6
Kevin 7
Share:
8,183
Fortuneman
Author by

Fortuneman

Updated on September 18, 2022

Comments

  • Fortuneman
    Fortuneman almost 2 years

    I want to send the alert message to client.

    So, I found that.

    res.send(<'alert("XXX!!!")');

    In this way, I didn't use res.render();

    I want to use together.

    Or

    I want to send alert message using res.render();

    How do I this?

    This is the code.

    			if (err)
    				console.log(err);
    			
    			if (flag === 1) { //함수 이름 중복
    				try {
    					//res.send('<script>alert("함수 이름 중복!")</script>');
    					LoadDB(res);
    				} catch (exception) {}
    			}
    			else {
    				//(4) 데이터베이스 insert 쿼리
    				var query = "insert into automation_script set ?";
    				conn.query(query, set, function (error, result, fields) {
    					if (error) {
    						console.log('Insert_DB error' + error);
    					} else {
    						try {
    							LoadDB(res);
    						} catch (exception) {}
    					}
    				});
    			}
    			//(5) 데이터베이스 해제
    			conn.end();

    Thanks you.

    • Vinod Louis
      Vinod Louis over 7 years
      how about sending your alert as text and at client side use eval() to execute prior to that why such need ?
    • Soubhik Mondal
      Soubhik Mondal over 7 years
      @VinodLouis isn't eval a bad idea?
    • Vinod Louis
      Vinod Louis over 7 years
      @BlazeSahlzen ofcourse it is if adulterated thats why I asked why such need
    • Fortuneman
      Fortuneman over 7 years
      @VinodLouis eval? Oh! I have to research the eval. Thanks you!
    • Prathamesh More
      Prathamesh More about 4 years
  • Fortuneman
    Fortuneman over 7 years
    First, Thanks you Jamie. I will remember your advice. In fact, I'm novice programmer especially javascript and node.js. But, I got the project that I had to make using javascript and node.js. It's hard but interesting. I will research the http and web socket. Thanks again.
  • Jeff Schaller
    Jeff Schaller over 6 years
    Clever combination of the key field with -unique!
  • BennX
    BennX about 4 years
    While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.