awk +根据文件中的值对文件进行排序并写入两个不同的文件

发布于 2024-09-06 07:09:08 字数 1024 浏览 2 评论 0原文

我在文件 file_test 中右眼和左眼的值 如何通过awk将file_test分离为file1file2,以便将相同的值写入file1和不同的值值到 file2 中,如下例

file_test 所示:

NAME: jim 
LAST NAME: bakker
right eye: >|5|< 
left eye VALUE: >|5|< 
NAME: Jorg 
LAST NAME: mitchel
right eye: >|3|< 
left eye VALUE: >|5|< 
NAME: jimmy  
LAST NAME: kartter
right eye: >|6|< 
left eye VALUE: >|5|< 
NAME: david  
LAST NAME: kann
right eye: >|9|< 
left eye VALUE: >|9|< 

file1 应该是:

NAME: jim  
LAST NAME: bakker
right eye: >|5|< 
left eye VALUE: >|5|< 
NAME: david  
LAST NAME: kann
right eye: >|9|< 
left eye VALUE: >|9|< 

file2 应该是:

NAME: Jorg  
LAST NAME: mitchel
right eye: >|3|< 
left eye VALUE: >|5|< 
NAME: jimmy  
LAST NAME: kartter
right eye: >|6|< 
left eye VALUE: >|5|<

I have in file file_test values of right eye and left eye
How to separate the file_test to file1 and file2 by awk in order to write the equal values into file1 and different values into file2 as the following example down

file_test is:

NAME: jim 
LAST NAME: bakker
right eye: >|5|< 
left eye VALUE: >|5|< 
NAME: Jorg 
LAST NAME: mitchel
right eye: >|3|< 
left eye VALUE: >|5|< 
NAME: jimmy  
LAST NAME: kartter
right eye: >|6|< 
left eye VALUE: >|5|< 
NAME: david  
LAST NAME: kann
right eye: >|9|< 
left eye VALUE: >|9|< 

file1 should be:

NAME: jim  
LAST NAME: bakker
right eye: >|5|< 
left eye VALUE: >|5|< 
NAME: david  
LAST NAME: kann
right eye: >|9|< 
left eye VALUE: >|9|< 

file2 should be:

NAME: Jorg  
LAST NAME: mitchel
right eye: >|3|< 
left eye VALUE: >|5|< 
NAME: jimmy  
LAST NAME: kartter
right eye: >|6|< 
left eye VALUE: >|5|<

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

洛阳烟雨空心柳 2024-09-13 07:09:08
awk -F: '/^NAME:/ && buff {
  say(buff, eyes); buff = null 
  split(null, eyes) # use delete eyes if supported
  }
{ 
  buff = buff ? buff RS $0 : $0
  /eye/ && eyes[$NF]
  }
END { say(buff, eyes) }
func say(b, e) {
  # length(array) is not portable
  print b > (length(e) > 1 ? "file2" : "file1")
  }' file_test
awk -F: '/^NAME:/ && buff {
  say(buff, eyes); buff = null 
  split(null, eyes) # use delete eyes if supported
  }
{ 
  buff = buff ? buff RS $0 : $0
  /eye/ && eyes[$NF]
  }
END { say(buff, eyes) }
func say(b, e) {
  # length(array) is not portable
  print b > (length(e) > 1 ? "file2" : "file1")
  }' file_test
如梦 2024-09-13 07:09:08

我会为此使用 Perl:

    open my $f, "<", "file_input";
    open my $file1, ">", "file1";
    open my $file2, ">", "file2";
    while (! eof($f)) {
        my $person; $person .= <$f> for (1..4);  # read 4 lines
        my ($right) = ($person =~ /^right.*?(\d+)/m);
        my ($left) = ($person =~ /^left.*?(\d+)/m);
        my $file = ($right == $left) ? $file1 : $file2;
        print $file $person;
    }
    close $f;
    close $file1;
    close $file2

I'd use Perl for this:

    open my $f, "<", "file_input";
    open my $file1, ">", "file1";
    open my $file2, ">", "file2";
    while (! eof($f)) {
        my $person; $person .= <$f> for (1..4);  # read 4 lines
        my ($right) = ($person =~ /^right.*?(\d+)/m);
        my ($left) = ($person =~ /^left.*?(\d+)/m);
        my $file = ($right == $left) ? $file1 : $file2;
        print $file $person;
    }
    close $f;
    close $file1;
    close $file2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文