【问题标题】:Shell Script to copy from one file to another file用于从一个文件复制到另一个文件的 Shell 脚本
【发布时间】:2016-05-24 19:48:16
【问题描述】:

假设有一个 file.txt 写在 "Start from here" 行之间的某个地方。所以我的要求是使用此行将整个文本复制到另一个文件中的 EOF shell 脚本

【问题讨论】:

    标签: shell


    【解决方案1】:
    #!/bin/bash
    
    write_=false
    
    while read line || [[ -n "$line" ]]; do
        if [ $write_ == true ]; then
            echo $line
        elif [ "$line" == "Start from here" ]; then
            write_=true
        fi
    done < "file.txt" > "other_file.txt"
    

    文件.txt

    foo foo foo
    bar bar bar
    Start from here
    This should be
    in the file
    and not anything else
    

    【讨论】:

    • 我会将&gt;&gt; other_file.txt 移出循环,移至done &lt; file.txt &gt;&gt; other_file.txt,或者您为每一行打开和关闭它。
    • TIL bash 在循环结束时可以有多个箭头。我已经更新了答案,以在其他几个边缘案例中包含该更改。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2015-07-19
    • 1970-01-01
    • 2013-11-01
    • 2020-09-20
    • 1970-01-01
    • 2020-06-18
    • 2018-10-05
    • 1970-01-01
    相关资源
    最近更新 更多