bash:在第一个字符串出现之前,合并(交错)插入第二个字符串的第一行的两个文件。。。

bash:在第一个字符串出现之前,合并(交错)插入第二个字符串的第一行的两个文件。。。,bash,awk,sed,Bash,Awk,Sed,我正在尝试将两个看起来像File1和File2的文件合并为类似的结果。其目的是在文件1中使用模式,并在文件1中模式之前的行中插入文件2的第一行,然后递归地处理这两个文件 文件1 文件2 结果 到目前为止,这将把File2中的行插入到模式出现的下方,而不是上方: awk 'NR==FNR{a[NR]=$0;next}1;/Input:/{print a[++i]}' File2 File2 感谢您的帮助,谢谢 据我所知,您希望在与Input:from File1匹配的每一行上方插入File2中的

我正在尝试将两个看起来像File1和File2的文件合并为类似的结果。其目的是在文件1中使用模式,并在文件1中模式之前的行中插入文件2的第一行,然后递归地处理这两个文件

文件1 文件2 结果 到目前为止,这将把File2中的行插入到模式出现的下方,而不是上方:

awk 'NR==FNR{a[NR]=$0;next}1;/Input:/{print a[++i]}' File2 File2

感谢您的帮助,谢谢

据我所知,您希望在与
Input:
from File1匹配的每一行上方插入File2中的一行

下面的步骤可以做到这一点:

#!/usr/bin/env bash
case $BASH_VERSION in ''|[123].*|4.0.*) echo "ERROR: Bash 4.1 or newer required" >&2; exit 1;; esac

exec {file1_fd}<File1 # open File1 for input, storing the FD# it's opened on in file1_fd
exec {file2_fd}<File2 # likewile for File2 and file2_fd

while IFS= read -r f1line <&"$file1_fd"; do  # as long as we can read a line from file1...
  if [[ $f1line =~ Input: ]]; then           # if that line contains "Input:"
    IFS= read -r f2line <&"$file2_fd"        # ...then read a second line from file2
    printf '%s\n' "$f2line"                  # ...and write that second line to our output
  fi
  printf '%s\n' "$f1line"                    # before writing the content from file1.
done
#/usr/bin/env bash
“|[123].|4.0.*)echo中的$BASH_版本“错误:需要BASH 4.1或更高版本”>&2;出口1;;以撒

exec{file1_fd}据我所知,您希望在与file1中的
Input:
匹配的每一行上方插入File2中的一行

下面的步骤可以做到这一点:

#!/usr/bin/env bash
case $BASH_VERSION in ''|[123].*|4.0.*) echo "ERROR: Bash 4.1 or newer required" >&2; exit 1;; esac

exec {file1_fd}<File1 # open File1 for input, storing the FD# it's opened on in file1_fd
exec {file2_fd}<File2 # likewile for File2 and file2_fd

while IFS= read -r f1line <&"$file1_fd"; do  # as long as we can read a line from file1...
  if [[ $f1line =~ Input: ]]; then           # if that line contains "Input:"
    IFS= read -r f2line <&"$file2_fd"        # ...then read a second line from file2
    printf '%s\n' "$f2line"                  # ...and write that second line to our output
  fi
  printf '%s\n' "$f1line"                    # before writing the content from file1.
done
#/usr/bin/env bash
“|[123].|4.0.*)echo中的$BASH_版本“错误:需要BASH 4.1或更高版本”>&2;出口1;;以撒
exec{file1_fd}你就快到了

$ awk 'NR==FNR{p[NR]=$0; next} /^Input:/{print p[++c]}1' file2 file1

Process1
Input:
Processed:
Result:
Process2
Input:
Result:
Process3
Input:
Error:
Result:
要在插入后打印该行,请将打印行移到末尾,即
1
作为简写

你就快到了

$ awk 'NR==FNR{p[NR]=$0; next} /^Input:/{print p[++c]}1' file2 file1

Process1
Input:
Processed:
Result:
Process2
Input:
Result:
Process3
Input:
Error:
Result:
要在插入后打印该行,请将打印行移到末尾,即
1
作为简写

这可能适合您(GNU-sed):

此解决方案从file1和file2生成一个sed脚本,该脚本使用file1中的行号,行号开始于
Input:
,并将该行号与file2中的相应行组合为insert命令

如果在
输入之后始终有
结果:
,则可以接受以下解决方案

sed -e '/Input:/R file2' -e '//h;//d' -e 'x;/./p;z;x' file1
还有另一种方式:

sed '/Input:/i\insert here' file1 | sed -e '/^insert here/R file2' -e '//d'
这可能适用于您(GNU-sed):

此解决方案从file1和file2生成一个sed脚本,该脚本使用file1中的行号,行号开始于
Input:
,并将该行号与file2中的相应行组合为insert命令

如果在
输入之后始终有
结果:
,则可以接受以下解决方案

sed -e '/Input:/R file2' -e '//h;//d' -e 'x;/./p;z;x' file1
还有另一种方式:

sed '/Input:/i\insert here' file1 | sed -e '/^insert here/R file2' -e '//d'

您想要bash答案还是awk答案?
1
应该是awk行的最后一个字符。首先打印存储在数组
a
中的内容,然后打印条目。因此,
awk'NR==FNR{a[NR]=$0;next}/Input://{print a[++i]}1文件2文件1
抱歉,我本应该更清楚一点-我想要一个awk答案(但它是用bash脚本编写的)。你想要bash答案还是awk答案?
1
应该是awk行的最后一个字符。首先打印存储在数组
a
中的内容,然后打印条目。因此,
awk'NR==FNR{a[NR]=$0;next}/Input:/{print a[++i]}1'文件2文件1
抱歉,我本应该更清楚一点-我想要一个awk答案(但它是用bash脚本编写的)。