我怎样才能从文件中提取的文本行? [英] How can I extract lines of text from a file?

查看:123
本文介绍了我怎样才能从文件中提取的文本行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个目录文件的完整,我需要拉页眉和页脚了他们。他们是如此使用头部或尾部是行不通的所有变长。每个文件确实有一条线,我可以寻找,但我不希望在结果中包括行了。

I have a directory full of files and I need to pull the headers and footers off of them. They are all variable length so using head or tail isn't going to work. Each file does have a line I can search for, but I don't want to include the line in the results.

它通常是

*** Start (more text here)

和结尾

*** Finish (more text here)

我想要的文件名保持不变,所以我需要覆盖源文件,或写入到不同的目录,我会改写他们自己。

I want the file names to stay the same, so I need to overwrite the originals, or write to a different directory and I'll overwrite them myself.

哦,是的,这是当然的在Linux服务器上,所以我有Perl,sed的,awk中,grep的等。

Oh yeah, it's on a linux server of course, so I have Perl, sed, awk, grep, etc.

推荐答案

尝试触发器!..操作符。

# flip-flop.pl
use strict;
use warnings;

my $start  = qr/^\*\*\* Start/;
my $finish = qr/^\*\*\* Finish/;

while ( <> ) {
    if ( /$start/ .. /$finish/ ) {
        next  if /$start/ or /$finish/;
        print $_;
    }
}

ü可以再使用-i perl的开关来更新您的文件(S),像这样......

U can then use the -i perl switch to update your file(s) like so.....

 $ perl -i'copy_*' flip-flop.pl data.txt

...这改变data.txt中,但使得复印留存为copy_data.txt。

...which changes data.txt but makes a copy beforehand as "copy_data.txt".

/ I3az /

这篇关于我怎样才能从文件中提取的文本行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆