LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Which text editor etc can reverse the order of lines from top to bottom? (https://www.linuxquestions.org/questions/linux-software-2/which-text-editor-etc-can-reverse-the-order-of-lines-from-top-to-bottom-4175596549/)

grumpyskeptic 12-31-2016 06:05 PM

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.

273 12-31-2016 06:09 PM

Would something like...
Code:

tac inputfile > outputfile
...work for you?

grumpyskeptic 12-31-2016 06:12 PM

No, sorry. As I wrote above, I do not understand command lines.

273 12-31-2016 06:15 PM

Fair enough, your loss.

rkelsen 12-31-2016 06:53 PM

Quote:

Originally Posted by grumpyskeptic (Post 5648702)
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!

astrogeek 12-31-2016 07:01 PM

Quote:

Originally Posted by grumpyskeptic (Post 5648702)
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!

grumpyskeptic 01-01-2017 04:42 AM

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

Turbocapitalist 01-01-2017 04:48 AM

You'd navigate with "cd" but maybe to find the file in the first place, you could use locate

273 01-01-2017 04:49 AM

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.

Sector11 01-01-2017 08:55 AM

Quote:

Originally Posted by 273 (Post 5648700)
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!

273 01-01-2017 09:01 AM

Quote:

Originally Posted by Sector11 (Post 5648855)
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.

Sector11 01-01-2017 09:11 AM

@273 - sorry I missed your post above. :(
Quote:

Originally Posted by grumpyskeptic (Post 5648792)
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.

Sector11 01-01-2017 09:26 AM

OFF TOPIC
Quote:

Originally Posted by 273 (Post 5648856)
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.

grumpyskeptic 01-01-2017 10:39 AM

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.


All times are GMT -5. The time now is 09:06 AM.