c**o 发帖数: 166 | 1 Suppose we want to reverse the lines in a file, say the original file looks
like this:
line 1
line 2
line 3
we now want to get a new file like this:
line 3
line 2
line 1
How will you do it? Either shell or perl is ok. Just notice that this file
could have many many lines.
Thanks, |
p******f 发帖数: 162 | 2
you want to opposite of cat, try tac
【在 c**o 的大作中提到】 : Suppose we want to reverse the lines in a file, say the original file looks : like this: : line 1 : line 2 : line 3 : we now want to get a new file like this: : line 3 : line 2 : line 1 : How will you do it? Either shell or perl is ok. Just notice that this file
|
t*****t 发帖数: 72 | 3 cool.hehe
【在 p******f 的大作中提到】 : : you want to opposite of cat, try tac
|
c**o 发帖数: 166 | 4 Thanks. That is a nice one.
But suppose I have a file, say tac.out, which is generated by the following
program:
#include
int main(void)
{
int i;
for(i=1;i<5;i++) printf("line %d\n",i);
printf("line 5");
return 0;
}
Can you see what would happen if you enter
tac tac.out
?
【在 p******f 的大作中提到】 : : you want to opposite of cat, try tac
|
t*****t 发帖数: 72 | 5 what do you want?
【在 c**o 的大作中提到】 : Thanks. That is a nice one. : But suppose I have a file, say tac.out, which is generated by the following : program: : #include : int main(void) : { : int i; : for(i=1;i<5;i++) printf("line %d\n",i); : printf("line 5"); :
|
c**o 发帖数: 166 | 6 Yes, tac is a very nice one to reverse the lines of a file. But it really
needs a "return" at the end of the line. Unfortunately, I happened have a file
without "return" at the end of the last line. It did not give me the right
answer.
I am still looking for some different ways to do it, hopefully it can handle
the special case I mentioned above.
Actually, last line and the second last line could be numbers. If there is no
"return" at the end of the last line, it will "merge" those last two line
【在 t*****t 的大作中提到】 : what do you want?
|
t*****t 发帖数: 72 | 7 Here is a silly solution, forget it if your file is a very large one. |
D*********s 发帖数: 555 | 8 Add a return then.
file
no
【在 c**o 的大作中提到】 : Yes, tac is a very nice one to reverse the lines of a file. But it really : needs a "return" at the end of the line. Unfortunately, I happened have a file : without "return" at the end of the last line. It did not give me the right : answer. : I am still looking for some different ways to do it, hopefully it can handle : the special case I mentioned above. : Actually, last line and the second last line could be numbers. If there is no : "return" at the end of the last line, it will "merge" those last two line
|
k**e 发帖数: 86 | 9 awk '{ arr[NR]=$0 } END {for(x=NR; x>0; --x) print arr[x]}' < your_file
file
no
【在 c**o 的大作中提到】 : Yes, tac is a very nice one to reverse the lines of a file. But it really : needs a "return" at the end of the line. Unfortunately, I happened have a file : without "return" at the end of the last line. It did not give me the right : answer. : I am still looking for some different ways to do it, hopefully it can handle : the special case I mentioned above. : Actually, last line and the second last line could be numbers. If there is no : "return" at the end of the last line, it will "merge" those last two line
|
t*****t 发帖数: 72 | 10 #The following one could be better:
perl -ple 1 tac.out | tac
【在 t*****t 的大作中提到】 : Here is a silly solution, forget it if your file is a very large one.
|
|
|
c**o 发帖数: 166 | 11 Yes, this should be the right one.
Thanks.
The previous two are for small files.
【在 t*****t 的大作中提到】 : #The following one could be better: : perl -ple 1 tac.out | tac
|
c****j 发帖数: 258 | 12 use vi(even it's not vim) when you don't have tac on old unix systems
you can also do the reverse for certain range of lines:
50,100g/^/m 49 |
A**s 发帖数: 8 | 13 Many smart programmers here, but why reinvent the wheel? Can't a simple
command do? tail -r FILE
【在 c**o 的大作中提到】 : Suppose we want to reverse the lines in a file, say the original file looks : like this: : line 1 : line 2 : line 3 : we now want to get a new file like this: : line 3 : line 2 : line 1 : How will you do it? Either shell or perl is ok. Just notice that this file
|
c**o 发帖数: 166 | 14 yes, this command is a good one too.
But it, just like tac, also needs a "return" at the end of the last line.
【在 A**s 的大作中提到】 : Many smart programmers here, but why reinvent the wheel? Can't a simple : command do? tail -r FILE
|
p******f 发帖数: 162 | 15 You should fix your broken file! Like, echo >> file
Or if it is readonly, use this:
(cat file; echo) | tac
【在 c**o 的大作中提到】 : yes, this command is a good one too. : But it, just like tac, also needs a "return" at the end of the last line.
|