Open study

is now brainly

With Brainly you can:

  • Get homework help from millions of students and moderators
  • Learn how to solve problems with step-by-step explanations
  • Share your knowledge and earn points by helping other students
  • Learn anywhere, anytime with the Brainly app!

A community for students.

C / Unix question, How to "sort" a printf?

Computer Science
See more answers at
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.

Join Brainly to access

this expert answer


To see the expert answer you'll need to create a free account at Brainly

Say I run a program, over the course of it's running, its prints a few lines. e.g. File1 40 File2 90 File3 20 How would I go about instead of printing it during its run time, "collect" the printed statements and then print them in sorted order. Should I use the unix command like: sort -k2n,2 -k1,1 myprogram
you can sort it using syntax " sort [options] filename " and using options -b : Ignores leading spaces in each line -d : Uses dictionary sort order. Conisders only spaces and alphanumeric characters in sorting -f : Uses case insensitive sorting. -M : Sorts based on months. Considers only first 3 letters as month. Eg: JAN, FEB -n : Uses numeric sorting -R : Sorts the input file randomly. -r : Reverse order sorting -k : Sorts file based on the data in the specified field positions. -u : Suppresses duplicate lines -t : input field separator
The Unix sort program works on files, not on programs. That's why `sort [options] myprogram` won't work. Instead, you'll need to tell sort to read from stdin (your terminal). You can do that by simply not giving a file name to sort (like `sort [options]`). But how can you use the output of `myprogram` as input for `sort`? That's where a pipe ('|') comes in. The pipe takes to output of the program before it and 'pipes' that output to the input of the program after it. So `./myprogram | sort -k2` will take the ouput from `./myprogram` and will use that output as input for `sort -k2`. That should give you the result you want. Another alternative is to write the ouput of myprogram to a file and run sort on that file. `./myprogram > tmp` will place the output of `./myprogram` in a file called `tmp`. Then you can sort that output with `sort -k2 tmp`. It'll work, but IMHO the first solution is better 'cause you're not constantly creating new files that you need to cleanup later.

Not the answer you are looking for?

Search for more explanations.

Ask your own question

Other answers:

Hi thanks for your responses slotema and harpellet1202. slotema, where exactly would I put that statement? Since I don't want to printf my statements anywhere in my program except after I have sorted my output
I like the first solution better to because like you said, I would have to create new files and delete them later
If you want to sort the output from inside your program, you'll need a different approach. You'll need to write a function that sorts the strings you want to print (there are different sorting algorithms you can use and adapt for that: ). Or try to see if you can get your hands on the source code of the `sort` program.
Just as a very basic idea of what I want to do: #include void printThis(); int main(int argc, char *argv[]){ printf("blah3 30"); printThis(); return 0 ) void printThis(){ printf("blah2 20"); printf("blah1 10"); } So if I run this program, it would print out blah3 30 blah2 20 blah1 10 However what I want to do is instead of printing any of this, take all of what the program should have output, sort it using Unix sort, and THEN print it out blah1 10 blah2 20 blah3 30
Ah, so I wouldn't be able to do this within the program right?
It's possible, but it'd take some effort to get it working properly. Another thing you can do is to write a shell script that runs your program and passes it to sort (like in my first response). You can run the shell script as if it was your program and you'll get the sorted output.
so would everything be encapsulated with a .sh file?
Yes, though if you want to you can leave out the '.sh' extension on a Unix system.
I'll probably have to go the function route
Thanks a lot for your help slotema!
You're welcome. Good luck with the function route and just ask around if you need help with that.

Not the answer you are looking for?

Search for more explanations.

Ask your own question