Linux - SoftwareThis 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
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.
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?
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?
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
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
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.
Last edited by linustalman; 08-29-2013 at 02:55 PM.
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
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.
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
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.