Sponsored Content
Full Discussion: Grep and Sort
Homework and Emergencies Homework & Coursework Questions Grep and Sort Post 302787809 by hanson44 on Saturday 30th of March 2013 09:23:02 PM
Old 03-30-2013
You got it. Smilie
Code:
$ who | sort

You probably already know the best way to learn is to log on and play around on the computer. You're right if only two people are on (you and the admin) that you can't really test "who" very well. In that case, you can create a little test file, and do some experiments, such as:
Code:
$ echo Joe > test.txt
$ echo Bill >> test.txt
$ echo Suzy >> test.txt
$ echo Alice >> test.txt
$ cat test.txt
Joe
Bill
Suzy
Alice
$ sort test.txt
Alice
Bill
Joe
Suzy
$ cat test.txt | sort
Alice
Bill
Joe
Suzy
$ sort < test.txt
Alice
Bill
Joe
Suzy

I used "echo" to make the test.txt file. If you know how to use a Unix editor, of course use the editor instead.

With the test file, you can try out different sort options, as you are already doing with -r option.

Quote:
I am not really sure why this order of who and sort works.
Is it because you need to run who first so the sort takes
the input from the other command (who)?
Yes, that is exactly right! "who" produces the output, the "raw materials". The output from "who" enters the pipe. On the other side of the pipe, "sort" reads its input. The output from "sort" is what you finally see.

"who" does not expect any input, so it would not make sense to put "who" on the right-hand side of the pipe. "sort" HAS to have input, or have a file specified for it to operate upon. In the examples above, I showed three simple ways sort can get input (sort file, | sort, and sort < file).

Quote:
P.S. I am doing a small grading program for this same lab as well.
I will post my results later tonight. Didn't post here because I had
not yet tried anything. Should I open a new thread or post here?
You should open a new thread.
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Sort/Grep Question

Hello all, I have a test file that has the format: ..... O 3.694950 -.895050 1.480000 O 5.485050 .895050 1.480000 Ti -4.590000 4.590000 2.960000 Ti -2.295000 ... (5 Replies)
Discussion started by: aarondesk
5 Replies

2. Shell Programming and Scripting

how to grep sort userids

hello folks i have a file that have data like /test/aa/123 /test/aa/xyz /test/bb/xyz /test/bb/123 in above lines i just wants to grep "aa" and "bb". Thanks, Bash (4 Replies)
Discussion started by: learnbash
4 Replies

3. Shell Programming and Scripting

history awk grep sort

can someone help me in the awk part...little confuse on that part. The problem is this: what input each utility gets and what it does with data and what output is provides to the next utility) history | awk '{a++}END{for(i in a){print a " " i}}' | sort -rn | grep '^' Thanks (4 Replies)
Discussion started by: Learnerabc
4 Replies

4. Shell Programming and Scripting

Grep empty space and sort

Hi Expert, Kindly request for your expertise in this matter. I have below output: 12.125.124.173,xx1.common.com 12.125.124.174,xx2.common.com 12.125.124.175,xx3.common.com 12.125.124.176, 12.125.124.177, 12.125.124.178, 12.125.124.179,xx4.common.com 12.125.124.180,xx5.common.com... (8 Replies)
Discussion started by: regmaster
8 Replies

5. Programming

[ask]SQL command act like sort and grep

for example, I have a text file in random content inside, maybe something like this. 234234 54654 123134 467456 24234234 7867867 23424 568567if I run this command cat "filename.txt" | sort -n | grep "^467456$" -A 1 -B 1the result is 234234 467456 568567is it possible to do this command... (2 Replies)
Discussion started by: 14th
2 Replies

6. Shell Programming and Scripting

grep from 3 lines and sort

Pseudo name=hdiskpower54 Symmetrix ID=000190101757 Logical device ID=0601 state=alive; policy=SymmOpt; priority=0; queued-IOs=0 ============================================================================== ---------------- Host --------------- - Stor - -- I/O Path - -- Stats --- ### HW... (7 Replies)
Discussion started by: Daniel Gate
7 Replies

7. Shell Programming and Scripting

How to sort grep result based on timestamp?

Hi, Trying to sort grep result based on timestamp of the filename. I have the following result and want to sort them on timestampgrep -i 'ERROR' *log*2013* s_m_xxx_xxx_xxx_xxx_xxxx.log.20130906092431:TRANSF_1_1_1> DBG_21216 Finished transformations for Source Qualifier . Total errors ... (5 Replies)
Discussion started by: bobbygsk
5 Replies

8. Homework & Coursework Questions

awk questions using sort and grep

1. The problem statement, all variables and given/known data: So i'll probably get told off for this but I have a few problems and rather than clog up the whole forum I'll post them here. Please bare in mind I am a complete novice when it comes to all this and so if you help please treat me like a... (4 Replies)
Discussion started by: jamesb18
4 Replies

9. Homework & Coursework Questions

awk with Grep and Sort

1. The problem statement, all variables and given/known data: Please bare in mind I am a complete novice to this and have very very basic knowledge so please keep any answers as simple as possible and explain in terms I will understand ahha :):) I have a text file of names and test scores... (1 Reply)
Discussion started by: jamesb18
1 Replies

10. Shell Programming and Scripting

Grep -v -f and sort|diff which way is faster

Hi Gurus, I have two big files. I need to compare the different. currently, I am using sort file1 > file1_temp; sort file2 > file2_tmp diff file1_tmp file2_tmp I can use command grep -v -f file1 file2 just wondering which way is fast to compare two big files. Thanks... (4 Replies)
Discussion started by: ken6503
4 Replies
MONGOCOLLECTION.AGGREGATECURSOR(3)					 1					MONGOCOLLECTION.AGGREGATECURSOR(3)

MongoCollection::aggregateCursor - Execute an aggregation pipeline command and retrieve results through a cursor

SYNOPSIS
public MongoCommandCursor MongoCollection::aggregateCursor (array $command, [array $options]) DESCRIPTION
With this method you can execute Aggregation Framework pipelines and retrieve the results through a cursor, instead of getting just one document back as you would with MongoCollection::aggregate. This method returns a MongoCommandCursor object. This cursor object implements the Iterator interface just like the MongoCursor objects that are returned by the MongoCollection::find method. Note The resulting MongoCommandCursor will inherit this collection's read preference. MongoCommandCursor::setReadPreference may be used to change the read preference before iterating on the cursor. PARAMETERS
o $pipeline - The Aggregation Framework pipeline to execute. o $options -Options for the aggregation command. Valid options include: o "allowDiskUse"Allow aggregation stages to write to temporary files o "cursor" It is possible to configure how many initial documents the server should return with the first result set. The default initial batch size is 101. You can change it by adding the batchSize option: <?php $collection->aggregateCursor( $pipeline, [ "cursor" => [ "batchSize" => 4 ] ] ); MongoCommandCursor::batchSize method on the returned MongoCommandCursor object. o "explain" Return information on the processing of the pipeline. This option may cause the command to return a result docu- ment that is unsuitable for constructing a MongoCommandCursor. If you need to use this option, you should consider using MongoCollection::aggregate. o "maxTimeMS"Specifies a cumulative time limit in milliseconds for processing the operation (does not include idle time). If the operation is not completed within the timeout period, a MongoExecutionTimeoutException will be thrown. RETURN VALUES
Returns a MongoCommandCursor object. Because this implements the Iterator interface you can iterate over each of the results as returned by the command query. The MongoCommandCursor also implements the MongoCursorInterface interface which adds the MongoCommandCursor::batch- Size, MongoCommandCursor::dead, MongoCommandCursor::info methods. EXAMPLES
Example #1 MongoCollection.aggregateCursor(3) example Finding all of the distinct values for a key. <?php $m = new MongoClient; $db = $m->test; $people = $db->people; $people->drop(); $people->insert(array("name" => "Joe", "points" => 4)); $people->insert(array("name" => "Molly", "points" => 43)); $people->insert(array("name" => "Sally", "points" => 22)); $people->insert(array("name" => "Joe", "points" => 22)); $people->insert(array("name" => "Molly", "points" => 87)); $ages = $people->aggregateCursor( [ [ '$group' => [ '_id' => '$name', 'points' => [ '$sum' => '$points' ] ] ], [ '$sort' => [ 'points' => -1 ] ], ] ); foreach ($ages as $person) { echo "{$person['_id']}: {$person['points']} "; } ?> The above example will output something similar to: Joe: 26 Sally: 22 Example #2 MongoCollection.aggregateCursor(3) example with different initial batch size Finding all of the distinct values for a key. <?php $m = new MongoClient; $db = $m->test; $people = $db->people; $people->drop(); /* Insert some sample data */ $people->insert(array("name" => "Joe", "points" => 4)); $people->insert(array("name" => "Molly", "points" => 43)); $people->insert(array("name" => "Sally", "points" => 22)); $people->insert(array("name" => "Joe", "points" => 22)); $people->insert(array("name" => "Molly", "points" => 87)); /* Run the command cursor */ $ages = $people->aggregateCursor( [ [ '$group' => [ '_id' => '$name', 'points' => [ '$sum' => '$points' ] ] ], [ '$sort' => [ 'points' => -1 ] ], ], [ "cursor" => [ "batchSize" => 4 ] ] ); foreach ($ages as $person) { echo "{$person['_id']}: {$person['points']} "; } ?> The above example will output something similar to: Joe: 26 Sally: 22 SEE ALSO
MongoDB::command, MongoCommandCursor, MongoCommandCursor::batchSize, MongoCollection::aggregate, The MongoDB aggregation framework. PHP Documentation Group MONGOCOLLECTION.AGGREGATECURSOR(3)
All times are GMT -4. The time now is 08:42 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy