Quantcast
Channel: How can I reverse the order of lines in a file? - Stack Overflow
Browsing latest articles
Browse All 25 View Live

Answer by Jessica Baker-seuk for How can I reverse the order of lines in a file?

Not sure if I missed something.How about pipe into sorti.e.cat file | sort -rSorry if I missed the point of this question.I often use this to scan syslogs.In linux :watch " tail /var/log/syslog | sort...

View Article



Answer by Georg Fischer for How can I reverse the order of lines in a file?

You may use Perl on the commandline:perl -e 'my @b=(); while(<>) {push(@b, $_);}; print join("", reverse(@b));' orig > rev

View Article

Answer by youkaichao for How can I reverse the order of lines in a file?

It happens to me that I want to get the last n lines of a very large text file efficiently.The first thing I tried is tail -n 10000000 file.txt > ans.txt, but I found it very slow, for tail has to...

View Article

Answer by driver for How can I reverse the order of lines in a file?

I see lots of interesting ideas. But try my idea. Pipe your text into this: rev | tr '\n''~' | rev | tr '~''\n'which assumes that the character '~' is not in the file. This should work on every UNIX...

View Article

Answer by Mark Booth for How can I reverse the order of lines in a file?

If you want to modify the file in place, you can runsed -i '1!G;h;$!d' filenameThis removes the need to create a temporary file and then delete or rename the original and has the same result. For...

View Article


Answer by Marius Hofert for How can I reverse the order of lines in a file?

For Emacs users: C-x h (select the whole file) and then M-x reverse-region. Also works for only selecting parts or the lines and reverting those.

View Article

Answer by Yakir GIladi Edry for How can I reverse the order of lines in a file?

at the end of your command put:| tactac does exactly what you're asking for, it "Write each FILE to standard output, last line first."tac is the opposite of cat :-).

View Article

Answer by Bohdan for How can I reverse the order of lines in a file?

tail -r works in most Linux and MacOS systemsseq 1 20 | tail -r

View Article


Answer by dosentmatter for How can I reverse the order of lines in a file?

You can do it with vimstdin and stdout. You can also use ex to be POSIX compliant. vim is just the visual mode for ex. In fact, you can use ex with vim -e or vim -E (improved ex mode).vim is useful...

View Article


Answer by R. Kumar for How can I reverse the order of lines in a file?

This will work on both BSD and GNU.awk '{arr[i++]=$0} END {while (i>0) print arr[--i] }' filename

View Article

Answer by jins for How can I reverse the order of lines in a file?

tac <file_name>example:$ cat file1.txt12345$ tac file1.txt54321

View Article

Answer by lacostenycoder for How can I reverse the order of lines in a file?

For cross OS (i.e. OSX, Linux) solution that may use tac inside a shell script use homebrew as others have mentioned above, then just alias tac like so:Install libFor MacOSbrew install coreutilsFor...

View Article

Answer by ITNM for How can I reverse the order of lines in a file?

Best solution:tail -n20 file.txt | tac

View Article


Answer by Yoker Rekoy for How can I reverse the order of lines in a file?

sort -r < filenameorrev < filename

View Article

Answer by Yekatandilburg for How can I reverse the order of lines in a file?

The simplest method is using the tac command. tac is cat's inverse.Example:$ cat order.txtroger shah armin van buurenfpga vhdl arduino c++ java gridgain$ tac order.txt > inverted_file.txt$ cat...

View Article


Answer by konsolebox for How can I reverse the order of lines in a file?

Just Bash :) (4.0+)function print_reversed { local lines i readarray -t lines for (( i = ${#lines[@]}; i--; )); do printf '%s\n'"${lines[i]}" done}print_reversed < file

View Article

Answer by Yauhen Yakimovich for How can I reverse the order of lines in a file?

EDITthe following generates a randomly sorted list of numbers from 1 to 10:seq 1 10 | sort -R | tee /tmp/lst |cat <(cat /tmp/lst) <(echo '-------') **...**where dots are replaced with actual...

View Article


Answer by DigitalRoss for How can I reverse the order of lines in a file?

$ (tac 2> /dev/null || tail -r)Try tac, which works on Linux, and if that doesn't work use tail -r, which works on BSD and OSX.

View Article

Answer by Tim Menzies for How can I reverse the order of lines in a file?

I really like the "tail -r" answer, but my favorite gawk answer is....gawk '{ L[n++] = $0 } END { while(n--) print L[n] }' file

View Article

Answer by DerMike for How can I reverse the order of lines in a file?

If you happen to be in vim use:g/^/m0Explanation from @Ronopolis below:g means "do this globally.^ means "the beginning of a line".m means "move the line to a new line number. 0 is which line to move...

View Article

Answer by DG for How can I reverse the order of lines in a file?

Try the following command:grep -n "" myfile.txt | sort -r -n | gawk -F : "{ print $2 }"

View Article


Answer by ephemient for How can I reverse the order of lines in a file?

There's the well-known sed tricks:# reverse order of lines (emulates "tac")# bug/feature in HHsed v1.5 causes blank lines to be deletedsed '1!G;h;$!d' # method 1sed -n '1!G;h;$p' # method...

View Article


Answer by Mihai Limbășan for How can I reverse the order of lines in a file?

Also worth mentioning: tac (the, ahem, reverse of cat). Part of coreutils.Flipping one file into anothertac a.txt > b.txt

View Article

Answer by Jason Cohen for How can I reverse the order of lines in a file?

BSD tail:tail -r myfile.txtReference: FreeBSD, NetBSD, OpenBSD and OS X manual pages.

View Article

How can I reverse the order of lines in a file?

I'd like to reverse the order of lines in a text file (or stdin), preserving the contents of each line.So, i.e., starting with:foobarbazI'd like to end up with bazbarfooIs there a standard UNIX...

View Article

Browsing latest articles
Browse All 25 View Live




Latest Images