Linux - Newbie This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
02-02-2017, 01:51 PM
|
#1
|
LQ Newbie
Registered: Feb 2017
Posts: 6
Rep: 
|
Scripting
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
|
|
|
02-02-2017, 02:01 PM
|
#2
|
LQ Guru
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573
|
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).
|
|
|
02-02-2017, 02:12 PM
|
#3
|
LQ Guru
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 27,824
|
Quote:
Originally Posted by Ziggy2017
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.
|
|
|
02-02-2017, 02:18 PM
|
#4
|
Senior Member
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278
|
Assuming a file called 'input' contains this:
Code:
orange
apple
grape
orange
grape
grape
apple
Run sort on the file and output to a file called 'output' like this:
Code:
sort input -o output
Gives a sorted list like this:
Code:
cat output
apple
apple
grape
grape
grape
orange
orange
Is there something that is confusing about the output or the process?
|
|
|
02-02-2017, 02:20 PM
|
#5
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,756
|
Quote:
Originally Posted by Ziggy2017
I ran the man sort command, but didn't understand the results
|
There are a lot of advanced options there. Can you post some data and what you want to do with it?
You can sort different columns independently. Take the following data:
Code:
cat <<EOF > data
141.251.0.0/16
141.25.0.0/16
51.116.0.0/16
191.234.4.0/24
157.58.90.0/24
104.40.0.0/13
EOF
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.
|
|
1 members found this post helpful.
|
02-02-2017, 02:22 PM
|
#6
|
LQ Newbie
Registered: Feb 2017
Posts: 6
Original Poster
Rep: 
|
Confusion
What's confusing is the statement; put all lines containing the same word on adjacent lines
|
|
|
02-02-2017, 02:30 PM
|
#7
|
LQ Guru
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
|
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:
https://www.chegg.com/homework-help/...s-st-q16692997
|
|
|
02-02-2017, 02:40 PM
|
#8
|
LQ Newbie
Registered: Feb 2017
Posts: 6
Original Poster
Rep: 
|
$$$
chegg wants money. Won't pay. I'll find the answer some other way
|
|
|
02-02-2017, 02:42 PM
|
#9
|
LQ Guru
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
|
Quote:
Originally Posted by Ziggy2017
chegg wants money. Won't pay. I'll find the answer some other way
|
I didn't expect you to pay. I expected you to have a look at the hint in the question and see if it made any sense to you.
|
|
1 members found this post helpful.
|
02-02-2017, 02:50 PM
|
#10
|
Senior Member
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278
|
Quote:
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:
Sorted:
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.
|
|
1 members found this post helpful.
|
02-02-2017, 02:52 PM
|
#11
|
Moderator
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,968
|
Quote:
Originally Posted by Ziggy2017
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.
|
|
|
|
02-02-2017, 03:00 PM
|
#12
|
LQ Newbie
Registered: Feb 2017
Posts: 6
Original Poster
Rep: 
|
OK, I'll look at the hint when I get home from work tonight. Thanks
|
|
|
02-02-2017, 03:04 PM
|
#13
|
LQ Guru
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
|
Quote:
Originally Posted by Ziggy2017
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.
|
|
|
All times are GMT -5. The time now is 07:21 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|