简体   繁体   English

从perl中输出文件的每行中有五个单词的输入文件中打印para

[英]To print a para from a input file having five words in each line in output file in perl

#! /bin/usr/perl
use strict ;
use warning;
print "Enter your database name: \n";

chomp($db=<STDIN>);
open(R1,"$db")||die("error");
 while($line1=(<R1>))
{
$l1=$line1;
@arr1= split("  ",$l1);
         for $i=(0..9)
           {
              print  "@arr1[$i] \t";
              print join (@arr1), "\n";
           }
}
close(R1);
exit;

To print a para from a input file having five words in each line in output file in perl 从perl中输出文件的每行中有五个单词的输入文件中打印para

input : 输入:

My name is ks and i study in class 7. i am 12 year old and i like to travel all over the world.i like thai food 我的名字是ks,我在7年级学习。我12岁,我喜欢到世界各地旅行。我喜欢泰国菜

desired outut: 期望的输出:
My name is ks 我的名字是ks
and in class 7. i 在第7课
am 12 year old and 我12岁了
i like to travel all 我喜欢旅游
over the world. 全世界。 i like 我喜欢
thai food 泰国食品

can you please help with the print function 你能帮忙打印功能吗?

Possible with a regular expression: 可以使用正则表达式:

use strict;
use warnings;

while (<DATA>) { chomp;
    print "$1\n" while /((?:\w+\W*){1,5})/g;
}

__DATA__
My name is k s and i study in class 7. i am 12 year old and i like to
travel all over the world.i like thai food

Gives: 得到:

My name is k s 
and i study in class 
7. i am 12 year 
old and i like to
travel all over the world.
i like thai food

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM