LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 08-29-2013, 02:07 PM   #1
linustalman
LQ Guru
 
Registered: Mar 2010
Location: Ireland
Distribution: Debian 12 Bookworm
Posts: 5,726

Rep: Reputation: 479Reputation: 479Reputation: 479Reputation: 479Reputation: 479
Question How to alphabetically sort lines in a html file?


Hi.

Does anyone know how to alphabetically sort lines in a html file? I use KompoZer for editing html files. Can it be done via that app? Else another way?

Thanks.
 
Old 08-29-2013, 02:33 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,666

Rep: Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970
Quote:
Originally Posted by LinusStallman View Post
Hi.
Does anyone know how to alphabetically sort lines in a html file? I use KompoZer for editing html files. Can it be done via that app? Else another way?
Well, that's a fairly open-ended question, the way it's worded. What do you mean? Sorted by first word, all words in the line? Can you provide an example of the input you have, and the output of what you want?
 
Old 08-29-2013, 02:37 PM   #3
linustalman
LQ Guru
 
Registered: Mar 2010
Location: Ireland
Distribution: Debian 12 Bookworm
Posts: 5,726

Original Poster
Rep: Reputation: 479Reputation: 479Reputation: 479Reputation: 479Reputation: 479
Quote:
Originally Posted by TB0ne View Post
Well, that's a fairly open-ended question, the way it's worded. What do you mean? Sorted by first word, all words in the line? Can you provide an example of the input you have, and the output of what you want?
By first word of course.

Example (actual list is much longer and has hyper links):
simonscat.com; youtube.com/simonscat (Simon Tofield)
youtube.com/AsapSCIENCE
dorkly.com; youtube.com/dorkly

Output expected:
dorkly.com; youtube.com/dorkly
simonscat.com; youtube.com/simonscat (Simon Tofield)
youtube.com/AsapSCIENCE
 
Old 08-29-2013, 02:39 PM   #4
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Quote:
Originally Posted by LinusStallman View Post
By first word of course.

Example (actual list is much longer and has hyper links):
simonscat.com; youtube.com/simonscat (Simon Tofield)
youtube.com/AsapSCIENCE
dorkly.com; youtube.com/dorkly

Output expected:
dorkly.com; youtube.com/dorkly
simonscat.com; youtube.com/simonscat (Simon Tofield)
youtube.com/AsapSCIENCE
What does it have to do with an html file?
 
Old 08-29-2013, 02:40 PM   #5
linustalman
LQ Guru
 
Registered: Mar 2010
Location: Ireland
Distribution: Debian 12 Bookworm
Posts: 5,726

Original Poster
Rep: Reputation: 479Reputation: 479Reputation: 479Reputation: 479Reputation: 479
Quote:
Originally Posted by sycamorex View Post
What does it have to do with an html file?
They are formatted inside a html file. If it was just plain lines of text, I could easily sort in Gedit or Medit.

Last edited by linustalman; 08-29-2013 at 02:44 PM.
 
Old 08-29-2013, 02:42 PM   #6
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Quote:
Originally Posted by LinusStallman View Post
They are formatted in a html file.
So can you provide a sample in the context (ideally the whole file, if possible)? Without knowing the context, it's hard to do anything.
 
Old 08-29-2013, 02:53 PM   #7
linustalman
LQ Guru
 
Registered: Mar 2010
Location: Ireland
Distribution: Debian 12 Bookworm
Posts: 5,726

Original Poster
Rep: Reputation: 479Reputation: 479Reputation: 479Reputation: 479Reputation: 479
Quote:
Originally Posted by sycamorex View Post
So can you provide a sample in the context (ideally the whole file, if possible)? Without knowing the context, it's hard to do anything.
Sure. Attached is a selection from a larger list. I've put them in the wrong order to show what I mean. I renamed the file extension from .html to .txt because it would not let me attach it.

You must rename the file to 1.html to view it properly.
Attached Files
File Type: txt 1.txt (767 Bytes, 58 views)

Last edited by linustalman; 08-29-2013 at 02:55 PM.
 
Old 08-29-2013, 02:55 PM   #8
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
I don't really know html

but why not present the list of links in a table and get that table to sort it


http://www.w3.org/html/wg/drafts/htm...table-sortable

Last edited by Firerat; 08-29-2013 at 03:04 PM. Reason: added link
 
Old 08-29-2013, 03:32 PM   #9
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,666

Rep: Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970
Quote:
Originally Posted by LinusStallman View Post
By first word of course.
There is not really an 'of course' here, since what you originally said was "alphabetically sort lines"...which could mean alphabetize the entire line or not.
Quote:
Example (actual list is much longer and has hyper links):
simonscat.com; youtube.com/simonscat (Simon Tofield)
youtube.com/AsapSCIENCE
dorkly.com; youtube.com/dorkly

Output expected:
dorkly.com; youtube.com/dorkly
simonscat.com; youtube.com/simonscat (Simon Tofield)
youtube.com/AsapSCIENCE
Have you looked at the sort command??? You can tell it which word(s) to sort on, and how to order the list. I'd strip out the html tags first with sed, and get everything on it's own line. After that, just run it through sort.

Last edited by TB0ne; 08-29-2013 at 03:34 PM.
 
Old 08-29-2013, 03:42 PM   #10
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196
You should rethink your question: How to alphabetically sort hyperlinks in an html file?

The first problem, in the example file you posted, the first link to youtube.com/AsapSCIENCE is not actually on a separate line and if sorted by lines it would potentially break the structure of the document by moving the entire <html><head>... section away from the start of the document.

Next it is apparent from your other examples that what you really want to sort by is either the portion of the link after the host name, or maybe the linked text itself.

Finally, although your selected examples only include links of the form youtube.com/sometext, in order to work you would surely need to handle urls with query strings like somehost.com/sometext?abc=xyz, and cases where the linked text does not appear in the link target.

Also worth mentioning, in your 1.txt file the links are not contiguous. There are other html elements between some of them such as the styled <br> tag (which is not valid anyway).

My point is not to criticize your html, but to point out that sorting it by line to impose order on the links is not the right problem and probably hopeless anyway.

I would suggest that you first impose a sound structure on the html.

Then you need to decide what field you actually want to sort by, and what cases need to be handled.

My guess is that you really want to sort by linked text, and this might very well be different from the target url. But sorting by linked text inside other html tags and styles and various other attributes is a losing proposition.

So you would be much better served to either script a way of generating the links from sorted text, or some means of managing it via a text editor before inserting it into the html document.

If you must sort the html itself, then follow firerat's advice to organize the links into a table (i.e., impose a structure) then use the HTML/CSS facilities for ordering table rows.

Last edited by astrogeek; 08-29-2013 at 03:50 PM. Reason: typos
 
Old 08-29-2013, 04:30 PM   #11
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
Code:
[schneidz@hyper ~]$ sort 1.txt 





  
<a href="https://www.youtube.com/user/AvaazOrg">youtube.com/AvaazOrg</a><br>
<a href="http://www.youtube.com/user/betdaq"></a><a href="http://youtube.com/ehow">youtube.com/ehow</a><a href="http://youtube.com/user/engineerguyvideo"></a><br>
<a href="http://youtube.com/howcast">youtube.com/howcast</a><br>
</body></html>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html><head><meta content="text/html; charset=ISO-8859-1" http-equiv="content-type"><title>1</title></head><body><a href="http://www.youtube.com/user/AsapSCIENCE">youtube.com/AsapSCIENCE</a><br style="font-family: Arial; background-color: white;">
Perspective - The Future of Earth <a href="http://i.imgur.com/bwXbe.gif">imgur.com/bwXbe.gif</a><br>
not sure why one would want mal-formed sorted html code.
 
Old 08-30-2013, 08:34 AM   #12
linustalman
LQ Guru
 
Registered: Mar 2010
Location: Ireland
Distribution: Debian 12 Bookworm
Posts: 5,726

Original Poster
Rep: Reputation: 479Reputation: 479Reputation: 479Reputation: 479Reputation: 479
Quote:
Originally Posted by TB0ne View Post
There is not really an 'of course' here, since what you originally said was "alphabetically sort lines"...which could mean alphabetize the entire line or not.
Hi TB0ne.

If you go to Gedit or Pluma and select "Sort", it sorts the lines the way people generally expect. That's why I did not mention any other type of sorting. No offence intended.

I'd rather opt for a GUI method. I copied all into LibreOffice Calc and then sorted and copied back into Kompozer. The only hitch is that the gaps are too big between entries that have 2 or more lines. Is there a way to change this in table properties perhaps?

Again like last time, just rename attached file to .html
Attached Files
File Type: txt 2.txt (1.9 KB, 55 views)
 
Old 08-30-2013, 09:00 AM   #13
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
is 1.txt the before picture and 2.txt the after picture (i am not sure how the 2 co-relate).

can you please illustrate what you mean by sort... i dont think anyone knows what you are trying to do ?
 
Old 08-30-2013, 09:12 AM   #14
linustalman
LQ Guru
 
Registered: Mar 2010
Location: Ireland
Distribution: Debian 12 Bookworm
Posts: 5,726

Original Poster
Rep: Reputation: 479Reputation: 479Reputation: 479Reputation: 479Reputation: 479
Quote:
Originally Posted by schneidz View Post
is 1.txt the before picture and 2.txt the after picture (i am not sure how the 2 co-relate).

can you please illustrate what you mean by sort... i dont think anyone knows what you are trying to do ?
Goto Gedit and select "Sort", that's what I mean.

2.txt (.html) was sorted in Calc and then copied back into Kompozer. 1.txt can be disregarded now.
 
Old 08-30-2013, 01:40 PM   #15
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
http://tablesorter.com/docs/#Download

I don't know html, but I cobbled this together in vim


example
Sort.html
unzip the jquery into same dir as your sort.html
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
      <title>Sortable list</title>
      <script type="text/javascript" src="jquery-latest.js"></script> 
      <script type="text/javascript" src="jquery.tablesorter.js"></script>
      <script type="text/javascript">$(function(){$("#somelist").tablesorter()})</script>
  </head>
  <style>
    #somelist th:hover { cursor: pointer; cursor: hand; background-color: #EEE; }
  </style>
  <body>
    <table id="somelist" class="tablesorter" border="0" cellpadding="0" cellspacing="1">
      <thead><tr><th>Link</th></tr></thead>
      <tbody>
        <tr><td><a href="http://www.youtube.com/user/AsapSCIENCE">youtube.com/AsapSCIENCE</a></td></tr>
        <tr><td><a href="http://www.youtube.com/user/betdaq">youtube.com/user/betdaq</a></td></tr>
        <tr><td><a href="http://youtube.com/ehow">youtube.com/ehow</a></td></tr>
        <tr><td><a href="http://youtube.com/user/engineerguyvideo">engineerguyvideo</a></td></tr>
        <tr><td><a href="http://i.imgur.com/bwXbe.gif">imgur.com/bwXbe.gif</a></td></tr>
        <tr><td><a href="https://www.youtube.com/user/AvaazOrg">youtube.com/AvaazOrg</a></td></tr>
        <tr><td><a href="http://youtube.com/howcast">youtube.com/howcast</a></td></tr>
      </tbody>
    </table>
  </body>
</html>
load that file in browser, click on the header (Link)
 
  


Reply

Tags
html, lines, sort



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
[SOLVED] bash: sort lines in 2 files so that equal lines are at the same line number... masavini Programming 10 06-21-2012 01:58 PM
[SOLVED] [Perl] fail to sort a file with 300,000 lines by multiple columns Kunsheng Programming 10 11-13-2009 06:41 PM
sort lines inside blocks in a file llattan Linux - Newbie 9 04-05-2009 01:19 PM
Is there a line limit with the sort utility? Trying to sort 130 million lines of text gruffy Linux - General 4 08-10-2006 08:40 PM
How can I sort the lines in a file? windhair Linux - Software 2 11-17-2005 08:37 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 11:46 PM.

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