Perl读取文本格式化后写入文本
2013-12-13 13:43
141 查看
demo:
#!/usr/bin/perl -w #Perl pragma to restrict unsafe constructs use strict; use utf8; #main function sub main { #get params # @_ # Within a subroutine the array @_ contains the parameters passed to that subroutine. # Inside a subroutine, @_ is the default array for the array operators push, pop, shift, and unshift. # set source file location my $s_file = "example_file.txt"; # set destinate file location my $d_file = "file_1.txt"; # notice and exit if $s_file or $d_file is null die "must have source file and destination file!\n" unless $s_file && $d_file; #open file for read, FO file handler if ( open(FO, $s_file) ) { if ( open(FOO,">$d_file") ) { #do while loop while(<FO>) { # $_ general variables my $line = $_; # remove head and tail blank $line =~ s{^\s|\s$}{}g; # use , split line # split /PATTERN/,EXPR. use what split depend on your file delimiter my @items = split/,/, $line; # write some item to new file print FOO $items[1] . "|" . $items[0] . " "; } # close file handler close(FOO); } else { # open file fail print "open destination file $d_file error\n"; } # close file handler close(FO); } else { # open file fail print "open source file $s_file error\n"; } } # pass params to main function, # @ARGV # The array @ARGV contains the command-line arguments intended for the script. main(@ARGV);
相关文章推荐
- python读取 .txt 文本内容以及将程序执行结果写入txt文件
- [MFC] 向文本编辑框写入数据与从其中读取数据
- Matlab处理读取ASCII文本数据找到规则需求,重新写入数据到新的文本文件[示例:有限元边界条件施加]
- java基础-输入输出流-大文本文件读取及写入另一文本-----(此程序print部分有bug,输出不正常,原因琢磨不清了。。。)
- asp OpenTextFile文本读取与写入实例代码
- perl一次读取多行文本的策略
- Json读取写入文本
- PHP大数据文本读取与写入
- 文本文件内容 读取 写入 替换 复制
- .net读取写入文本操作
- 读取本地文本写入list
- 【Java学习笔记】基础知识学习17【文件文本读取写入】
- C# 读取Text文本,写入Text文本
- matlab中读取图中数据 并将其写入文本
- Java操作读取写入文本TXT及XML文件内容
- android文件的写入与读取---简单的文本读写context.openFileInput() context.openFileOutput()
- perl一次读取多行文本的策略
- 【C语言】逐行读取文本数字数据,经过某种处理再逐行写入另外一个文本
- Java读取Excel并解析文本(并格式化输出)
- Java读取txt文本内容并写入Excel