Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm new to scripting and was asked a question that wants me to sort the text to put all lines containing the same word on adjacent lines. I ran the man sort command, but didn't understand the results
If your input file is "inputfile", you would run "sort inputfile". That will print the sorted results to the screen. If you want to dump the sorted results to a file, you would run "sort inputfile > outputfile". Use the flags described in "man sort" to control how sort does the sorting (reverse, numeric, sort by column, etc).
I'm new to scripting and was asked a question that wants me to sort the text to put all lines containing the same word on adjacent lines. I ran the man sort command, but didn't understand the results
Not sure what you're asking...you know the command, and the man page tells you each option and how to use it.
Read the "Question Guidelines" link in my posting signature. We're happy to help, but show us what you've done/tried on your own, and tell us what you're confused about. We can't explain the man page to you.
You can sort it as IPv4 addresses numerically by using a dot (.) as the field separator. Then each field can be sorted numerically:
Code:
sort -t. -k1,1n -k2,2n -k3,3n -k4,4n data
It's not so clear from the manual page, but it's reference material not a guide or tutorial. But keep checking the manual page. Pieces will make sense over time.
Googling for "put all lines containing the same word on adjacent lines" throws up some interesting homework-related results, including the following which contains a hint for the operation in question:
Put all lines containing the same word on adjacent lines
This line is a line
Adjacent means 'next to'
Words that are the same, are the same
The definition of 'sort' is to systematically arrange in groups by sameness
So, if your lines contain words, and you sort those lines by word, then lines with words that are the same will end up 'grouped' together on adjacent lines.
Unsorted:
apple
orange
grape
apple
Sorted:
apple
apple
grape
orange
You'll notice that when sorting alphabetically, the lines with 'apple' will be adjacent to one another.
Last edited by szboardstretcher; 02-02-2017 at 02:53 PM.
chegg wants money. Won't pay. I'll find the answer some other way
Please read the recommended links from TB0ne or others who have cited advice. Rude comebacks only go so far, but it shows that you're well aware of the capability to search for solutions versus asking for handouts.
Quote:
Originally Posted by TB0ne
Not sure what you're asking...you know the command, and the man page tells you each option and how to use it.
Read the "Question Guidelines" link in my posting signature. We're happy to help, but show us what you've done/tried on your own, and tell us what you're confused about. We can't explain the man page to you.
OK, I'll look at the hint when I get home from work tonight. Thanks
Great. Bear in mind however that it's wrong (the solution is actually simpler than the hint suggests), but it *is* along the right lines and puts the question you asked into context. Pay particular attention to szboardstretcher's post. The solution is in fact quite simple.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.