Learn more. How can I reverse the order of lines in a file? Ask Question. Asked 12 years, 9 months ago. Active 1 year, 4 months ago. Viewed k times. So, i. Good Person 1, 2 2 gold badges 22 22 silver badges 41 41 bronze badges. Scotty Allen Scotty Allen 12k 9 9 gold badges 35 35 silver badges 51 51 bronze badges.
Important note about reversing the lines: make sure your file has a trailing newline first. Also pretty nearly a duplicate though older of unix. As in that case, migration to unix. Add a comment. Active Oldest Votes. Flipping one file into another tac a. Especially worth mentioning to those using a version of tail with no -r option! Just a note, because people have mentioned tac before, but tac doesn't appear to be installed on OS X.
Not that it'd be difficult to write a substitute in Perl, but I don't have the real one. If you use OS X with homebrew, you can install tac using brew install coreutils installs as gtac by default. One of the problems is if the file doesn't have a trailing new line, the first 2 lines may be conjoined as 1 line.
Show 6 more comments. BSD tail: tail -r myfile. Cristian Ciupitu Jason Cohen Jason Cohen The sed and awk solutions below are going to work even in the wonkiest systems. Just tried this on Ubuntu Use 'tac' instead see Mihai's answer below. The checkmark should move below to tac. This isn't in GNU tail so it's not even a de facto standard.
Show 12 more comments. I wish this one is the accepted answer. It is unclear whether sed solution works for such files. Hence my upvote for the awk alternative, linear. I didn't try the perl option, less piping-friendly. Show 1 more comment. Dror 2, 1 1 gold badge 18 18 silver badges 12 12 bronze badges. Why should he? Please explain the value of the tac command, this is useful for new users who might end up searching the same subject.
This really should be the accepted answer. Shame the above has so many votes. BTW: You don't have to pipe to tac if it's coming from a file. You can simply tac filename. DerMike DerMike Related: How to reverse the order of lines? I'd vote that up if you briefly explained what it did. Yeah, I get that bit, but I meant breaking down what the various bits of the vim command are doing. I've now looked at the answer kenorb linked, which provides the explanation.
So: "Find every line that has a beginning, and move it to line number 0. Does nothing. Then find line 2 and move it above line 1, to the top of the file.
Now find line 3 and move it to the top. Repeat this for every line. At the end you finish by moving the last line to the top. When you are done, you've reversed all the lines. It should be noted that the :g global command behaves in a very particular way vs simply using ranges. Nice trick and explanation Oh yea, forgot the token "see :help :g for more information" Daniel Alder 4, 1 1 gold badge 43 43 silver badges 49 49 bronze badges. DigitalRoss DigitalRoss k 23 23 gold badges silver badges bronze badges.
Why not tac myfile. Neither is tail -r. Still not foolproof, but this improves the odds of things working. Good enough for me. You need a proper test to see if tac is available. What happens if tac is available, but runs out of RAM and swap half way through consuming a giant input stream. It fails, and then tail -r succeeds in processing the remainder of the stream giving an incorrect result. Try the following command: grep -n "" myfile. DG DG. GoodPerson, nl by default will fail to number empty lines.
Instead of gawk I use cut -d: -f2- — Alexander Stumpf. Just Bash : 4. I really like the " tail -r " answer, but my favorite gawk answer is Vikdor Tim Menzies Tim Menzies 5 5 silver badges 14 14 bronze badges. Tested with mawk on Ubuntu Kalle Richter 6, 19 19 gold badges 64 64 silver badges bronze badges.
Yekatandilburg Yekatandilburg 1 1 silver badge 2 2 bronze badges. Yauhen Yakimovich Yauhen Yakimovich If you want to modify the file in place, you can run sed -i '1!
HoldOffHunger Mark Booth Mark Booth 7, 2 2 gold badges 63 63 silver badges 89 89 bronze badges. Vadim Kotov 7, 8 8 gold badges 45 45 silver badges 61 61 bronze badges. Kumar R. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow.
Learn more. Reverse text file in Bash Ask Question. Asked 7 years, 7 months ago. Active 6 years, 4 months ago. Viewed 5k times. How can I reverse a text file in Bash? For example, if some. Just use tac. Add a comment. Active Oldest Votes. Avinash Raj Avinash Raj k 23 23 gold badges silver badges bronze badges. Under OsX, try this for replacing tac : sed -n -e '1! Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password.
0コメント