Chinaunix

标题: 文件删除批量处理? [打印本页]

作者: tengfei0311    时间: 2017-01-15 15:19
标题: 文件删除批量处理?
本帖最后由 tengfei0311 于 2017-01-15 15:24 编辑

文件名: ES01.dat .............................ES999.dat(1000多个要处理的文件)

文件内容:

YN   ES22 31.2  90.1  (第一行)
XW  ES49 30.1  180   (第二行)
10  3.1
11  2.9
12  3.9
13  3.5
...
...
...
120  4.9

每个文件内的内容格式都相同,我想删去这么多文件中前2行前2列(上面红色的字符),该怎么处理?因为没法匹配字符串,因为每个文件里面的前两行两列字母都不一样。谢谢!








作者: moperyblue    时间: 2017-01-15 15:30

  1. sed -r '1,2{s/^(\S+\s+){2}//M}' file
复制代码

作者: tengfei0311    时间: 2017-01-15 15:39
回复 2# moperyblue

不对啊!
illegal option -- r

作者: jason680    时间: 2017-01-15 15:39
回复 1# tengfei0311

$ head -4 ES*.dat
==> ES01.dat <==
YN  ES22 31.2  90.1
XW  ES49 30.1  180
10  3.1
11  2.9

==> ES99.dat <==
YN  ES22 31.2  90.1
XW  ES49 30.1  180
10  3.1
11  2.9

$ sed -i -r '1,2s/[^ ]+ +[^ ]+ +//' ES*.dat

$ head -4 ES*.dat
==> ES01.dat <==
31.2  90.1
30.1  180
10  3.1
11  2.9

==> ES99.dat <==
31.2  90.1
30.1  180
10  3.1
11  2.9


作者: moperyblue    时间: 2017-01-15 15:50
回复 3# tengfei0311

什么系统?

sed --version
sed (GNU sed) 4.2.2

  1. echo 'YN   ES22 31.2  90.1
  2. XW  ES49 30.1  180
  3. 10  3.1
  4. 11  2.9
  5. 12  3.9
  6. 13  3.5'|sed -r '1,2{s/^(\S+\s+){2}//M}'
  7. 31.2  90.1
  8. 30.1  180
  9. 10  3.1
  10. 11  2.9
  11. 12  3.9
  12. 13  3.5
复制代码


试试
  1. sed '1,2{s/^\(\S\+\s\+\)\{2\}//M}'
复制代码

作者: tengfei0311    时间: 2017-01-15 15:56
回复 5# moperyblue

mac系统!
作者: tengfei0311    时间: 2017-01-15 15:59
本帖最后由 tengfei0311 于 2017-01-15 16:01 编辑

回复 5# moperyblue
sl-MacBookProesktop sl$ sed '1,2{s/^\(\S\+\s\+\)\{2\}//M}' test.dat
sed: 1: "1,2{s/^\(\S\+\s\+\)\{2\ ...": bad flag in substitute command: 'M'



作者: tengfei0311    时间: 2017-01-15 15:59
..
作者: tengfei0311    时间: 2017-01-15 16:03
回复 4# jason680

大神,你是什么系统?我在mac终端运行就生成了一个.dat-r的文件
作者: sunzhiguolu    时间: 2017-01-15 16:15
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;

  4. chdir ('urPath');
  5. foreach (glob ('*')){
  6.     my $old = $_;
  7.     open (my $FHr, '<', $old);
  8.     my @aT = <$FHr>;
  9.     close ($FHr);

  10.     foreach (0 .. 1){
  11.         my (undef, undef, @T) = split (' ', $aT[$_]);
  12.         $aT[$_] = "@T\n";
  13.     }
  14.     #unlink ($old);
  15.     rename ($old, $old . '.bak');
  16.     open (my $FHw, '>', $old);
  17.     print $FHw @aT;
  18.     close ($FHw);
  19. }
复制代码

作者: tengfei0311    时间: 2017-01-15 16:44
回复 4# jason680

谢谢 在Ubuntu下面运行成功!
作者: tengfei0311    时间: 2017-01-15 16:45
回复 5# moperyblue

谢谢!已搞定!
作者: jcdiy0601    时间: 2017-01-16 09:33
  1. awk 'NR<=2{$0=$3" "$4}{print > "file"}' file
复制代码

作者: sditmaner    时间: 2017-01-16 12:05
不错,真是太棒了!

作者: magee_yue    时间: 2017-01-17 15:23
回复 2# moperyblue

请问s///M 这个大写M是什么作用?
作者: moperyblue    时间: 2017-01-17 15:30
回复 15# magee_yue

大小写一样

info sed
=>
`M'
`m'
     The `M' modifier to regular-expression matching is a GNU `sed'
     extension which causes `^' and `$' to match respectively (in
     addition to the normal behavior) the empty string after a newline,
     and the empty string before a newline.  There are special character
     sequences (`\`' and `\'') which always match the beginning or the
     end of the buffer.  `M' stands for `multi-line'.




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2