- 论坛徽章:
- 0
|
quickly remove one line from a file
Question:
1. 如题:Perl中读入一个文件的一行后顺带把这一行在原文件中删除的操作??
2. 如何删除文件的第一行,或者
3. 最后一行??
Answer:
simply using perl's option -i, -n, -e (See more perl -h)
1. unkown the line position, using regexp to match this line
i.e.: match the required datetime
perl -lne 'print if /^Mar 29 10:25:34/;' /var/log/auth.log
2. using specical var $.: $.== 1 is 1st. line from a file
i.e.: get first line
perl -lne 'print" if $.==1;' /var/log/auth.log
3. using builtin eof(): print "this is the last line($.)" if eof;
i.e.: get last line
perl -lne 'print "$.: $_" if eof;' /var/log/auth.log
Using s/// to remove found line
There are more another way to do that, ...
-- ulmer |
|