由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Unix版 - reverse the lines?
相关主题
awk questionhow to set a path which is more than one line?
Perl: conditionally match multiple linesHow to see line number in Emacs
a simple perl problem?how to get rid of the ^M in files?
an AWK question?what's the command to count the number of lines in a file?
how to count the total number of linesRe: what's the command to count the number of lines in a file?
文件删不掉how to get part of a big text file?
PERL入门求助emacs how to concatenate two lines?
Perl和Python哪个更好呀?emacs中如何去指定一行
相关话题的讨论汇总
话题: line话题: file话题: lines话题: reverse话题: tac
进入Unix版参与讨论
1 (共1页)
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.
相关主题
文件删不掉how to set a path which is more than one line?
PERL入门求助How to see line number in Emacs
Perl和Python哪个更好呀?how to get rid of the ^M in files?
进入Unix版参与讨论
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.

1 (共1页)
进入Unix版参与讨论
相关主题
emacs中如何去指定一行how to count the total number of lines
a beginner's question文件删不掉
perl QPERL入门求助
请问Soalris 8的cache line size 是多少?Perl和Python哪个更好呀?
awk questionhow to set a path which is more than one line?
Perl: conditionally match multiple linesHow to see line number in Emacs
a simple perl problem?how to get rid of the ^M in files?
an AWK question?what's the command to count the number of lines in a file?
相关话题的讨论汇总
话题: line话题: file话题: lines话题: reverse话题: tac