URN Logo
UNIX Resources » Linux » China Linux Forum » Perl 编 程 » 3 » 如何删除一个文件中的指定行?
announcement 声明: 本页内容为中国Linux论坛的内容镜像,文章的版权以及其他所有的相关权利属于中国Linux论坛和相应文章的作者,如果转载,请注明文章来源及相关版权信息。
Resources
China Linux Forum(finished)
Linux Forum(finished)
FreeBSD China(finished)
linuxforum.net
  业界新闻与评论
  自由软件杂谈
  IT 人生
  Linux软件快递
  翻译作坊
  Linux图书与评论
  GNU Emacs/XEmacs
  Linux 中文环境和中文化
  Linux桌面与办公软件
  Linux 多媒体与娱乐版
  自由之窗Mozilla
  笔记本电脑上的Linux
  Gentoo
  Debian 一族
  网络管理技术
  Linux 安装与入门
  WEB服务器和FTP服务器
  域名服务器和邮件服务器
  Linux防火墙和代理服务器应用
  文件及打印服务器
  技术培训与认证
  Linux内核技术
  Linux 嵌入技术
  Linux设备驱动程序
  Linux 集群技术
  LINUX平台数据库
  系统和网络安全
  CPU 与 编译器
  系统计算研究所专栏
  Linux下的GUI软件开发
  C/C++编程版
  PHP 技 术
  Java&jsp技术
  Shell编程技术
  Perl 编 程
  Python 编 程
  XML/Web Service 技术
  永远的Unix
  FreeBSD世界
   
如何删除一个文件中的指定行?
如何删除一个文件中的指定行? - stan1ie [2005-04-11 01:23 | 291 byte(s)]
 
Re: 如何删除一个文件中的指定行? - tiw [2005-04-16 00:15 | 48 byte(s)]
 
 
Re: 如何删除一个文件中的指定行? - wandys [2005-04-11 11:18 | 44 byte(s)]
 
Re: 如何删除一个文件中的指定行? - stan1ie [2005-04-11 19:25 | 322 byte(s)]
 
Re: 如何删除一个文件中的指定行? - stan1ie [2005-04-11 16:22 | 691 byte(s)]
 
More than one way to do it. - wandys [2005-04-11 19:57 | 731 byte(s)]
 
Re: More than one way to do it. - stan1ie [2005-04-12 15:12 | 10 byte(s)]
 
Subject: 如何删除一个文件中的指定行?
Author: stan1ie    Posted: 2005-04-11 01:23    Length: 291 byte(s)
[Original] [Print] [Top]
比如文件内容为:


asdf123
sldkfjals 123
lkasjld
dfl'asdkf
sdkjfa
sdlk
asldf123lkdsf


我想在print的时候不包含有123的行,或者整行用正则表达式翻译空掉,咋整?谢谢!

[Original] [Print] [Top]
Subject: Re: 如何删除一个文件中的指定行?
Author: wandys    Posted: 2005-04-11 11:18    Length: 44 byte(s)
[Original] [Print] [Top]
m/123/ && s/.* //;
----
UN*X is user^H^H^H^Hfriend-friendly.
[Original] [Print] [Top]
Subject: Re: 如何删除一个文件中的指定行?
Author: stan1ie    Posted: 2005-04-11 16:22    Length: 691 byte(s)
[Original] [Print] [Top]
好像稍微有点问题……没成功,而且输出的文件打开后除第一行外,其他的在前面都多了个空格。

my $writefile = "lastwrite.txt";
my $win = "win.txt";

if (open(readfile,"$writefile") || die ("can't open passwdinput")){

@lines = <readfile>;


@lines =~m/123/ && s/.* //;


if (open (win,">$win") || die ("can't open win")){

print win ("@lines");

}

}
[Original] [Print] [Top]
Subject: Re: 如何删除一个文件中的指定行?
Author: stan1ie    Posted: 2005-04-11 19:25    Length: 322 byte(s)
[Original] [Print] [Top]
谢谢您了,没搞定,我用另外一个方法凑合上了,写了个bat:

type lastwrite.txt|find /V "123" >all.txt

然后整个sub就改成了:


sub edit

{
system ('edit.bat');
}



狂汗:)
[Original] [Print] [Top]
Subject: More than one way to do it.
Author: wandys    Posted: 2005-04-11 19:57    Length: 731 byte(s)
[Original] [Print] [Top]
1)

while (<>) {
m/123/ && s/.* //;
print $_;
}

2) better:

while (<>) {
m/123/ && next;
print $_;
}

3) much better:

while (<>) {
print unless m/123/;
}

4) or by using grep:

print grep(!/123/, <>);


./foo.pl <input >output


or by using the 'grep' command if you like:

$ grep -v 123 <input > output
----
UN*X is user^H^H^H^Hfriend-friendly.
[Original] [Print] [Top]
Subject: Re: More than one way to do it.
Author: stan1ie    Posted: 2005-04-12 15:12    Length: 10 byte(s)
[Original] [Print] [Top]
好!多谢!
[Original] [Print] [Top]
Subject: Re: 如何删除一个文件中的指定行?
Author: tiw    Posted: 2005-04-16 00:15    Length: 48 byte(s)
[Original] [Print] [Top]
perl -n -i.orig -e 'print unless /123/' test.dat
----

blog.csdn.net/wti
[Original] [Print] [Top]
Subject: Re: 如何删除一个文件中的指定行?
Author: stan1ie    Posted: 2005-04-20 01:06    Length: 80 byte(s)
[Original] [Print] [Top]
多谢!
捎带脚的:我的gmail下蛋了,有需要的朋友留下email,我发邀请过去。
[Original] [Print] [Top]
« Previous thread
一个文件中,即有gb 编码也有utf编码。。
Perl 编 程
3
Next thread »
急:solaris2.8下awk和gnu 下的awk执行结果不一样,为什么?
     

Copyright © 2007 UNIX Resources Network, All Rights Reserved.      About URN | Privacy & Legal | Help | Contact us
备案序号: 京ICP备05006143    webmaster: webmaster@unixresources.net
This page created on 2008-07-17 04:08:55, cost 0.050935983657837 ms.