LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 02-02-2017, 01:51 PM   #1
Ziggy2017
LQ Newbie
 
Registered: Feb 2017
Posts: 6

Rep: Reputation: Disabled
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
 
Old 02-02-2017, 02:01 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
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).
 
Old 02-02-2017, 02:12 PM   #3
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,553

Rep: Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946Reputation: 7946
Quote:
Originally Posted by Ziggy2017 View Post
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.
 
Old 02-02-2017, 02:18 PM   #4
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
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?
 
Old 02-02-2017, 02:20 PM   #5
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,258
Blog Entries: 3

Rep: Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713
Quote:
Originally Posted by Ziggy2017 View Post
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.
Old 02-02-2017, 02:22 PM   #6
Ziggy2017
LQ Newbie
 
Registered: Feb 2017
Posts: 6

Original Poster
Rep: Reputation: Disabled
Confusion

What's confusing is the statement; put all lines containing the same word on adjacent lines
 
Old 02-02-2017, 02:30 PM   #7
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
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
 
Old 02-02-2017, 02:40 PM   #8
Ziggy2017
LQ Newbie
 
Registered: Feb 2017
Posts: 6

Original Poster
Rep: Reputation: Disabled
Smile $$$

chegg wants money. Won't pay. I'll find the answer some other way
 
Old 02-02-2017, 02:42 PM   #9
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
Quote:
Originally Posted by Ziggy2017 View Post
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.
Old 02-02-2017, 02:50 PM   #10
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
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:
  • 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.
 
1 members found this post helpful.
Old 02-02-2017, 02:52 PM   #11
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,877
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by Ziggy2017 View Post
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 View Post
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.
 
Old 02-02-2017, 03:00 PM   #12
Ziggy2017
LQ Newbie
 
Registered: Feb 2017
Posts: 6

Original Poster
Rep: Reputation: Disabled
OK, I'll look at the hint when I get home from work tonight. Thanks
 
Old 02-02-2017, 03:04 PM   #13
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
Quote:
Originally Posted by Ziggy2017 View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Shell Scripting Part I: Getting started with bash scripting LXer Syndicated Linux News 0 04-29-2015 08:03 AM
LXer: Scripting the Linux desktop, Part 2: Scripting Nautilus LXer Syndicated Linux News 0 02-17-2011 04:02 AM
Firefox Scripting Add-on (Scripting HTML / Javascript inside Firefox) linuxbeatswindows Programming 1 09-18-2009 10:09 PM
teaching shell scripting: cool scripting examples? fax8 Linux - General 1 04-20-2006 04:29 AM
scripting help Abe_the_Man Linux - General 1 11-03-2004 05:30 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:59 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration