而是在所有文件的bash中使用awk in for循环-仅在awk中执行某些操作 [英] Instead using awk in for loop in bash through all files - do something only in awk

查看:47
本文介绍了而是在所有文件的bash中使用awk in for循环-仅在awk中执行某些操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题.这是我的输入文件.

I have a question. This is my input file.

#!/bin/bash
name="eq6"
tmp=$(mktemp) || exit 1  
for index in {1..2}
do
    awk 'f;/hbonds_Other-SOL/{f=1}'  "${name}_$index.ndx" > "$tmp" && mv "$tmp" "${name}_$index.ndx"  
done

是否只能在awk中执行此操作?我不想在每个for循环中调用awk.

Is it possible do this only in awk? I don't want to call awk in every for loop.

这是我的输入eq6_1.ndx

This is my input eq6_1.ndx

98536  98539  98542  98545  98548
[ hbonds_Other-SOL ]
      8      9  76759

eq6_2.ndx

eq6_2.ndx

  98542  98545  98548
[ hbonds_Other-SOL ]
      8      9  65281

预期的输出-打印"hbonds_Other-SOL"之后所有文件中的所有行

Expected output - print all lines from all files which are after "hbonds_Other-SOL"

eq6_1.ndx

eq6_1.ndx

      8      9  76759

eq6_2.ndx

eq6_2.ndx

      8      9  65281

推荐答案

使用GNU awk表示就地";

With GNU awk for "inplace" editing:

$ head *.ndx
==> eq6_1.ndx <==
98536  98539  98542  98545  98548
[ hbonds_Other-SOL ]
      8      9  76759

==> eq6_2.ndx <==
  98542  98545  98548
[ hbonds_Other-SOL ]
      8      9  65281

$ awk -i inplace 'FNR==1{f=0} f; /hbonds_Other-SOL/{f=1}' eq6_{1..2}.ndx

$ head *.ndx
==> eq6_1.ndx <==
      8      9  76759

==> eq6_2.ndx <==
      8      9  65281

其他awk版本有多种选择,但最简单的选择可能是:

There are various options with other awk versions but the simplest is probably:

dir=$(mktemp -d) &&
awk -v dir="$dir" 'FNR==1{close(out); out=dir"/"FILENAME; f=0} f{print > out} /hbonds_Other-SOL/{f=1}' eq6_{1..2}.ndx &&
mv "$dir"/* . &&
rmdir "$dir"

这篇关于而是在所有文件的bash中使用awk in for循环-仅在awk中执行某些操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆