avatar
c*o
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,
avatar
p*f
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

avatar
t*t
3
cool.hehe

【在 p******f 的大作中提到】
:
: you want to opposite of cat, try tac

avatar
c*o
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

avatar
t*t
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");
:

avatar
c*o
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?
avatar
t*t
7
Here is a silly solution, forget it if your file is a very large one.
avatar
D*s
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

avatar
k*e
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

avatar
t*t
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.
avatar
c*o
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

avatar
c*j
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
avatar
A*s
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

avatar
c*o
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

avatar
p*f
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.

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。