2

I have a text file named test and it has content in it like this:

Pigeon is the world's oldest domesticated bird. 
Research suggests that domestication of pigeons was as early as ten thousand years ago.
People who keep domestic pigeons are generally called pigeon fanciers.

Now I want a result like this with the sentences in reverse order:

People who keep domestic pigeons are generally called pigeon fanciers.
Research suggests that domestication of pigeons was as early as ten thousand years ago.
Pigeon is the world's oldest domesticated bird. 

How do I do this?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Murtaza Munshi
  • 1,065
  • 4
  • 13
  • 40
  • possible duplicate of [How can I reverse the order of lines in a file?](http://stackoverflow.com/questions/742466/how-can-i-reverse-the-order-of-lines-in-a-file) – dcaswell Sep 15 '13 at 16:42
  • 1
    Are your sentences always on a single line each — no sentence spread over several lines, and no line containing (parts of) several sentences? If so, the `tac` command works fine. If you have normal running text with single sentences spread over multiple lines, and possibly parts of multiple sentences on a single line, then you need a tool to split into sentences, then reverse them. That's definitely harder. Since you've accepted the `tac` answer, your problem is really 'get the lines in reverse order'. – Jonathan Leffler Sep 15 '13 at 17:02
  • Thanx for the extra guidance @Jonathan. I will keep that in mind. – Murtaza Munshi Sep 15 '13 at 17:03
  • @ user814064: I didnt knew that question existed else i wouldnt have posted this one. Sorry mate. – Murtaza Munshi Sep 15 '13 at 17:04

7 Answers7

7

You can use tac command:

$ tac file
P.P
  • 117,907
  • 20
  • 175
  • 238
1

Did you try this ?

tail -r myfile.txt
Vyko
  • 11
  • 1
1

Just for fun:

nl file | sort -nr | cut -b8-

Unlike the tail based solutions, this handles the whole file. I wouldn't call it elegant or efficient though.

DrC
  • 7,528
  • 1
  • 22
  • 37
0

If you have Ruby installed:

ruby -e "puts File.readlines('test').reverse" > test_reversed
Linuxios
  • 34,849
  • 13
  • 91
  • 116
0

You can do it using tail:

tail -r file.txt
Nils Werner
  • 34,832
  • 7
  • 76
  • 98
0

sort -r test should do it. From the man page:

-r, --reverse reverse the result of comparisons

kshade
  • 21
  • 5
  • Welcome to Stack Overflow. Please read the [About] page soon. This would not work correctly; it would print the lines in the order 'Research…', 'Pigeon…', 'People…', which is not the required order. If the lines were numbered, sorted in reverse numerical order, and then unnumbered, you could use `sort -r`; otherwise, no. – Jonathan Leffler Sep 15 '13 at 17:04
  • ... yeah. Sorry, don't know what I was thinking. – kshade Sep 15 '13 at 21:09
0

Bash ;-)

function print_reversed {
    readarray -t LINES
    for (( I = ${#LINES[@]}; I; )); do
        echo "${LINES[--I]}"
    done
}

print_reversed < file
konsolebox
  • 72,135
  • 12
  • 99
  • 105