Bash: Inserting one file's content into another file after the pattern

后端 未结 5 1478
一向
一向 2020-12-30 07:16

I\'m trying to write a bash script, which will do the following:

  1. reads the content from the first file (as a first argument)
  2. reads the content from th
5条回答
  •  一整个雨季
    2020-12-30 07:56

    You need spaces around the =~ operator. Compare:

    [[ foo=~bar ]]
    [[ foo =~ bar ]]
    

    This is because the first expression essentially evaluates as "Is this string empty?"

    Also, the OP code uses small tilde rather than tilde.

    Even so, you can easily get rid of the inner loop. Just replace the whole while read -r line2 bit with cat -- "$second_filename".

    Your last echo $line is only correct if the file does not end in a newline character (standard with *nix tools). Instead, you should use while read -r line || [[ $line ~= '' ]]. This works with or without newline at the end.

    Also, Use More Quotes™.

提交回复
热议问题