在文件中的特定行之后添加一个空行

时间:2018-07-01 16:26:57

标签: python bash sed

有人可以帮我吗?我想在“ ----- BEGIN RSA PRIVATE KEY -----”行之后添加空行(输入),并在“ ----- END RSA PRIVATE KEY-”行之前添加空行(输入) -“

文件中的所有数据都不是静态的,因此我认为可以根据行数(行数始终相同)使用sed或python来完成...

这是我的文件(使用vi和“ set nu”命令打开):

        test: /\.css$/,
        loader: ExtractTextPlugin.extract(
          Object.assign(
            {
              fallback: {
                loader: require.resolve('style-loader'),
                options: {
                  hmr: false,
                },
              },
              use: [
                {
                  loader: require.resolve('css-loader'),
                  options: {
                    importLoaders: 1,
                    modules: true,
                    localIdentName: '[name]__[local]__[hash:64:5]',
                    //   minimize: true,
                    //   sourceMap: shouldUseSourceMap,
                  },
                },
                {
                  loader: require.resolve('postcss-loader'),
                  options: {
                    // Necessary for external CSS imports to work
                    // https://github.com/facebookincubator/create-react-app/issues/2677
                    ident: 'postcss',
                    plugins: () => [
                      require('postcss-flexbugs-fixes'),
                      autoprefixer({
                        browsers: [
                          '>1%',
                          'last 4 versions',
                          'Firefox ESR',
                          'not ie < 9', // React doesn't support IE8 anyway
                        ],
                        flexbox: 'no-2009',
                      }),
                    ],
                  },
                },
              ],
            },
            extractTextPluginOptions
          )
        ),
        // Note: this won't work without `new ExtractTextPlugin()` in `plugins`.

我想要的内容(通常添加了两个空行):

      1 oc_ci:
  2   password: kyvmRHb88c7Y6JJnkqsdkYj4R
  3   username: admin
  4 uc_ci:
  5   password: 7a43ba66480b633195acd7a33f627545e9e87f1c
  6   tenant: admin
  7   username: admin
  8 uc_ssh_creds:
  9   username: heat-admin
 10   password: '-----BEGIN RSA PRIVATE KEY-----
 11 MIIEpQIBAAKCAQEAr0Kj5sL7QrkMn5+YK7/0DxCeeYmV0qTBbOTv0SGf4FHUKiNO
 12 ZcHnQHY+AF/ilAliyL9nxFW04nsVPwLWFQRunDu2SS9d05cRYwZJqLyrwNMAAMeg
 13 GeDGlVk2MDChNoRrDZZJ1P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hq6u7TfC9q7zF6BohaaHbKPajVBvMYqNwo0g7MDDhFqrNsyR8tAHTtRLJcj
 30 ygyuQ/5AiqK9bOc34Uvwx3kCgYEAvV8B/HhM5xI1XfZ9CkUx5Djn2wfnP+hc7Eru
 31 T1wePZh+VGv6ixJyE2NmFGgrn+CuQTwsw8ysFuk9WTH/OFIC0pqf+VwR4SLlnMN+
 32 Up/XvImC98ykMa/M5jIzUfXpEKzGy21utQ5K+BR9qvZXcpa6PKSi9RaB2T3r7XB7
 33 ZunRB6ECgYEAmQ38xl7AZgIW0rkEe/9mm0naMwG4YPXVYJxByRfmaoJ7qK9dEDYa
 34 tUNg+crSvl+JZxprkARZTjIq4Mwbg/JL/q5etM6tK4BKuJGSCGEqZ16fUYFUyDj1
 35 c2q99tcpGItrbccHjRjSbxk2FVDTnhbxmftWFYelTM/uIJTcgEtNpzU=
 36 -----END RSA PRIVATE KEY-----'

5 个答案:

答案 0 :(得分:1)

多种方式,例如使用awk:

awk '/-----END RSA PRIVATE KEY-----/ { print "" }
{ print }
/-----BEGIN RSA PRIVATE KEY-----/ { print "" }' origfile.txt >result.txt

Vi也可以通过脚本来打开文件,添加两行并关闭(至少Vim可以。)

答案 1 :(得分:0)

python中的

'\ n'会在您要在字符串中添加新行的地方添加新行

答案 2 :(得分:0)

以下内容可能会对您有所帮助。

awk '/BEGIN RSA/{$0=$0 ORS} /END RSA/{$0=ORS $0}1' Input_file

如果您想将输出保存到Input_file本身,请在上述命令中附加> temp_file && mv temp_file Input_file

答案 3 :(得分:0)

您可以使用sed轻松地做到这一点。

sed "s/-----BEGIN RSA PRIVATE KEY-----/&\n/" origfile > newfile

答案 4 :(得分:0)

这可能对您有用(GNU sed):

sed -i '/BEGIN RSA/G;/END RSA/i\\' file

在包含BEGIN RSA的行之后添加空白的保留空间,并在包含END RSA的行之前插入一个空白行。