×

Loading...
Ad by
Ad by

再次请教: 如何找到所有匹配的行,然后拷贝两次此行,并在其中一行上加注释。 谢谢

例如:

输入文件:

int main(int argc, char **argv)
{
printf("hello 1111 ~~~~\n");
printf("hello 2222\n");
printf("hello 3333 ~~~~\n");
printf("hello 4444\n");
return 0;
}

其中 ~~~~ 作为匹配条件
输出为:

int main(int argc, char **argv)
{
//printf("hello 1111 ~~~~\n");
printf("hello 1111 ~~~~\n");
printf("hello 2222\n");
//printf("hello 3333 ~~~~\n");
printf("hello 3333 ~~~~\n");
printf("hello 4444\n");
return 0;
}
Report

Replies, comments and Discussions:

  • 工作学习 / 学科技术讨论 / shell问题请教: 如何将两个文件,行对行的拷贝成第三个文件,谢谢。 (见内)
    文件A:
    1111
    2222
    3333

    文件B:
    AAAA
    BBBB
    CCCC

    合并后的文件C
    1111
    AAAA
    2222
    BBBB
    3333
    CCCC
    • 还是用PERL吧。SHELL做起来大概不容易呀
      • use python, better choice than perl
        • 因为要跨平台,不想带那么多的东西,用简单的几个shell命令最好了。这样可以简单把几个命令打包进去。无论怎样,感谢。
    • awk 'NR==FNR{a[FNR]=$0;next} {print a[FNR]} {print $0}' A B
      where A, B are the file names, you could use getlines as well.
    • paste -d'\n' A B > C
      • This one is simplest and the best.
      • Cool! in case of the two files have different lines., you have to use paste -d'\n' A B |sed '/^$/d'
        • 多谢, 多谢
    • 再次请教: 如何找到所有匹配的行,然后拷贝两次此行,并在其中一行上加注释。 谢谢
      例如:

      输入文件:

      int main(int argc, char **argv)
      {
      printf("hello 1111 ~~~~\n");
      printf("hello 2222\n");
      printf("hello 3333 ~~~~\n");
      printf("hello 4444\n");
      return 0;
      }

      其中 ~~~~ 作为匹配条件
      输出为:

      int main(int argc, char **argv)
      {
      //printf("hello 1111 ~~~~\n");
      printf("hello 1111 ~~~~\n");
      printf("hello 2222\n");
      //printf("hello 3333 ~~~~\n");
      printf("hello 3333 ~~~~\n");
      printf("hello 4444\n");
      return 0;
      }