小编use*_*907的帖子

Bash:在模式之后将一个文件的内容插入另一个文件中

我正在尝试编写一个bash脚本,它将执行以下操作:

  1. 从第一个文件中读取内容(作为第一个参数)
  2. 从第二个文件中读取内容(作为第二个参数)
  3. 使用给定模式查找第二个文件中的行(作为第三个参数)
  4. 在模式行之后将文本从第一个文件插入到第二个文件中.
  5. 在屏幕上打印最终文件.

例如:

first_file.txt:

111111
1111
11
1
Run Code Online (Sandbox Code Playgroud)

second_file.txt:

122221
2222
22
2
Run Code Online (Sandbox Code Playgroud)

图案:

2222
Run Code Online (Sandbox Code Playgroud)

输出:

122221
111111
1111
11
1
2222
111111
1111
11
1
22
2
Run Code Online (Sandbox Code Playgroud)

我应该用什么来实现BASH上的这个功能?

我编写了代码,但它无法正常工作(为什么?):

    #!/bin/bash

    first_filename="$1"
    second_filename="$2"
    pattern="$3"

    while read -r line
    do
    if [[ $line=˜$pattern ]]; then
            while read -r line2
            do
                    echo $line2
            done < $second_filename
    fi
    echo $line
    done < $first_filename
Run Code Online (Sandbox Code Playgroud)

linux bash shell sh

11
推荐指数
3
解决办法
2万
查看次数

标签 统计

bash ×1

linux ×1

sh ×1

shell ×1