LinuxQuestions.org
Visit Jeremy's Blog.
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 12-31-2016, 07:05 PM   #1
grumpyskeptic
Member
 
Registered: Apr 2016
Posts: 291

Rep: Reputation: Disabled
Which text editor etc can reverse the order of lines from top to bottom?


I am using Linux Mint Cinnamon 17.3 Rosa.

I have a large text file containing hundreds of lines. I want to reverse their order from top to bottom. As an example, if my text file was:

Lion
Hippo
Zebra
Monkey
Antelope
Yeti

then I want to reverse the order of these lines so that the text file is now:

Yeti
Antelope
Monkey
Zebra
Hippo
Lion

gedit can only sort lines into alphabetical order or reverse-alphabetical order. Neither of those are what I want.

As far as I recall in Windows I used to use Ted text editor to do the above.

I do not understand command line stuff, so a text editor or program with a GUI is what I seek please.

Thanks.
 
Old 12-31-2016, 07:09 PM   #2
273
LQ Addict
 
Registered: Dec 2011
Location: UK
Distribution: Debian Sid AMD64, Raspbian Wheezy, various VMs
Posts: 7,518

Rep: Reputation: 2319Reputation: 2319Reputation: 2319Reputation: 2319Reputation: 2319Reputation: 2319Reputation: 2319Reputation: 2319Reputation: 2319Reputation: 2319Reputation: 2319
Would something like...
Code:
tac inputfile > outputfile
...work for you?
 
2 members found this post helpful.
Old 12-31-2016, 07:12 PM   #3
grumpyskeptic
Member
 
Registered: Apr 2016
Posts: 291

Original Poster
Rep: Reputation: Disabled
No, sorry. As I wrote above, I do not understand command lines.
 
Old 12-31-2016, 07:15 PM   #4
273
LQ Addict
 
Registered: Dec 2011
Location: UK
Distribution: Debian Sid AMD64, Raspbian Wheezy, various VMs
Posts: 7,518

Rep: Reputation: 2319Reputation: 2319Reputation: 2319Reputation: 2319Reputation: 2319Reputation: 2319Reputation: 2319Reputation: 2319Reputation: 2319Reputation: 2319Reputation: 2319
Fair enough, your loss.
 
1 members found this post helpful.
Old 12-31-2016, 07:53 PM   #5
rkelsen
Senior Member
 
Registered: Sep 2004
Distribution: slackware
Posts: 2,299

Rep: Reputation: 616Reputation: 616Reputation: 616Reputation: 616Reputation: 616Reputation: 616
Quote:
Originally Posted by grumpyskeptic View Post
No, sorry. As I wrote above, I do not understand command lines.
!

He gave you the exact command to use. It couldn't be any simpler.

Using Linux without the CLI is like buying a house in Miami and not going to the beach. It can be done, but you're truly missing out.

Anyhow, Happy New Year!
 
2 members found this post helpful.
Old 12-31-2016, 08:01 PM   #6
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1}
Posts: 5,346
Blog Entries: 11

Rep: Reputation: 3310Reputation: 3310Reputation: 3310Reputation: 3310Reputation: 3310Reputation: 3310Reputation: 3310Reputation: 3310Reputation: 3310Reputation: 3310Reputation: 3310
Quote:
Originally Posted by grumpyskeptic View Post
No, sorry. As I wrote above, I do not understand command lines.
To be fair, you should try it as it is a complete, simple, immediate answer to your question.

You don't have to understand it, just type it with your own file names replacing inputfile and outputfile and you will have your file in reverse order. For example if your input file is named myfile.txt then:

Code:
tac path/to/myfile.txt > newfile.txt
Just be sure to use a different name for the second file to prevent overwriting the original!

You should learn to use the tools available to you. If you are not using these simple filters then you are missing out on the main reason for using GNU/Linux!

Last edited by astrogeek; 12-31-2016 at 08:09 PM. Reason: added example and filename caution
 
1 members found this post helpful.
Old 01-01-2017, 05:42 AM   #7
grumpyskeptic
Member
 
Registered: Apr 2016
Posts: 291

Original Poster
Rep: Reputation: Disabled
The file I want to reverse is not only located deep within a hierarchy or tree of folders, but is on my secondary HD as well. How do I navigate to that folder in the Console please, or find out what its file path is?

Thanks
 
Old 01-01-2017, 05:48 AM   #8
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,430
Blog Entries: 3

Rep: Reputation: 2206Reputation: 2206Reputation: 2206Reputation: 2206Reputation: 2206Reputation: 2206Reputation: 2206Reputation: 2206Reputation: 2206Reputation: 2206Reputation: 2206
You'd navigate with "cd" but maybe to find the file in the first place, you could use locate
 
Old 01-01-2017, 05:49 AM   #9
273
LQ Addict
 
Registered: Dec 2011
Location: UK
Distribution: Debian Sid AMD64, Raspbian Wheezy, various VMs
Posts: 7,518

Rep: Reputation: 2319Reputation: 2319Reputation: 2319Reputation: 2319Reputation: 2319Reputation: 2319Reputation: 2319Reputation: 2319Reputation: 2319Reputation: 2319Reputation: 2319
It depends upon how your file manager and the like work but it may be as simple as finding it in the file manager then right-clicking and selecting "Open in terminal". If not then you should be able to find out the path from any application you open it in -- the full path is often listed and, if not, should be available if you look a little more into it.
Then, once in the terminal, changing directory is accomplished using the "cd" command which has autocomplete functionality so you can type things like "cd /home/grumpyscheptic/my" then tab to complete something like "my favourite files", for example. To list the contents of the current directory it's just "ls".

Edit: One of many basic guides.

Last edited by 273; 01-01-2017 at 05:50 AM.
 
Old 01-01-2017, 09:55 AM   #10
Sector11
Member
 
Registered: Feb 2010
Distribution: BunsenLabs (Debian Stable)
Posts: 132

Rep: Reputation: Disabled
Quote:
Originally Posted by 273 View Post
Would something like...
Code:
tac inputfile > outputfile
...work for you?
So many time's I've wanted to do the same thing ... this is AWESOME!!! Thank you!
 
Old 01-01-2017, 10:01 AM   #11
273
LQ Addict
 
Registered: Dec 2011
Location: UK
Distribution: Debian Sid AMD64, Raspbian Wheezy, various VMs
Posts: 7,518

Rep: Reputation: 2319Reputation: 2319Reputation: 2319Reputation: 2319Reputation: 2319Reputation: 2319Reputation: 2319Reputation: 2319Reputation: 2319Reputation: 2319Reputation: 2319
Quote:
Originally Posted by Sector11 View Post
So many time's I've wanted to do the same thing ... this is AWESOME!!! Thank you!
You're welcome, I came across it on one of those "silly commands" lists and thought it might come in handy so it stuck in my mind.
 
Old 01-01-2017, 10:11 AM   #12
Sector11
Member
 
Registered: Feb 2010
Distribution: BunsenLabs (Debian Stable)
Posts: 132

Rep: Reputation: Disabled
@273 - sorry I missed your post above.
Quote:
Originally Posted by grumpyskeptic View Post
The file I want to reverse is not only located deep within a hierarchy or tree of folders, but is on my secondary HD as well. How do I navigate to that folder in the Console please, or find out what its file path is?

Thanks
Until you become comfortable with the 'CLI' you can use your file manager if you know 'where the file is'

1. Open your file manager and "copy" the directory the file is in - image
2. Open a terminal type "cd " <--- a space after cd
3. Right click in the terminal with your mouse and "Paste" from the menu and hit enter:
Code:
 01 Jan 17 @ 11:57:16 ~
  $ cd /media/5/Documents/Text
 
 01 Jan 17 @ 12:02:21 /media/5/Documents/Text
  $
4. now you are in the directory where the file is you want to use:
5. just "tac" it
Code:
 01 Jan 17 @ 12:02:21 /media/5/Documents/Text
  $ tac packages.txt > packages-rev.txt
 
 01 Jan 17 @ 12:04:03 /media/5/Documents/Text
  $
and the results.

Last edited by Sector11; 01-01-2017 at 10:56 AM. Reason: spelling
 
Old 01-01-2017, 10:26 AM   #13
Sector11
Member
 
Registered: Feb 2010
Distribution: BunsenLabs (Debian Stable)
Posts: 132

Rep: Reputation: Disabled
OFF TOPIC
Quote:
Originally Posted by 273 View Post
You're welcome, I came across it on one of those "silly commands" lists and thought it might come in handy so it stuck in my mind.
So I'll share a recent find with you: compgen
Code:
compgen -ac | sort
No man page - so: compgen Now that's a list!
Code:
 01 Jan 17 @ 12:17:36 ~
  $ compgen -ac su
sude
sudf
sudh
sudm
sudv
sued
sup2
supd
supg
sude
sudf
sudh
sudm
sudv
sued
sup2
supd
supg
suspend
sundata.sh
sundata.sh_old
su-to-root
sulogin
sudo
surfraw-update-path
sudoedit
surfraw
sum
su-to-root
sudoreplay
suspicious-source
su
sudoku

 01 Jan 17 @ 12:25:10 ~
  $
Back to the topic.

Last edited by Sector11; 01-01-2017 at 10:27 AM. Reason: Off Topic
 
1 members found this post helpful.
Old 01-01-2017, 11:39 AM   #14
grumpyskeptic
Member
 
Registered: Apr 2016
Posts: 291

Original Poster
Rep: Reputation: Disabled
Thanks, I have followed Sector11's advice and the file is now nicely reversed.

It is actually a large Comma Separated Value file, and I have asked another question about any programs that can manipulate the columns etc in .csv files.
 
  


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
Reverse the order of lines using sed Karljoe2 Programming 4 12-26-2012 11:38 AM
Kernel Call Trace Order - Is it top to bottom OR vice-versa nkataria Linux - Kernel 4 04-27-2009 05:30 AM
reverse order some lines of text files Melsync Programming 4 09-20-2005 05:40 PM
Getting multi-colour lines from top to bottom djeanveau Fedora - Installation 3 01-30-2005 09:58 PM
Getting multi-colour lines from top to bottom djeanveau Linux - Newbie 1 01-30-2005 01:02 PM

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

All times are GMT -5. The time now is 01:22 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration