sed 既存のインデントの後にテキストを追加するには?

sed 既存のインデントの後にテキストを追加するには?

以下を含むsedスクリプトがあります。

sed -i '/ *it.*do/! {
          /\.should/ {
            s/\.should/)\.to/
            s/\(\S\)/expect(\1/ 
            # ...others

最後の項目が行の先頭にs追加されます。expect(私はそれがどのように機能するのか理解していません。

これは次のとおりです。

オリジナル:

  it "uses the given count if set" do
    call('5').should == 5
  end

  it "uses the processor count from Parallel" do
    call(nil).should == 20
  end

後ろに:

  it "uses the given count if set" do
    expect(call('5')).to eq 5
  end

  it "uses the processor count from Parallel" do
    expect(call(nil)).to eq 20
  end

expect(ただし、for:は追加されません。

オリジナル:

  it "does not wait if not run in parallel" do
    ParallelTests.should_not_receive(:sleep)
    ParallelTests.wait_for_other_processes_to_finish
  end

  it "stops if only itself is running" do
    ENV["TEST_ENV_NUMBER"] = "2"
    ParallelTests.should_not_receive(:sleep)
    with_running_processes(1) do
        ParallelTests.wait_for_other_processes_to_finish
      end
  end

その後 - いいえexpect(...

  it "does not wait if not run in parallel" do
    ParallelTests).to_not receive(:sleep)
    ParallelTests.wait_for_other_processes_to_finish
  end

  it "stops if only itself is running" do
    ENV["TEST_ENV_NUMBER"] = "2"
    ParallelTests).to_not receive(:sleep)
    with_running_processes(1) do
        ParallelTests.wait_for_other_processes_to_finish
      end
  end

しかしそれはする労働時間:
以後:

  it "should be true when there is a Gemfile" do
    use_temporary_directory_for do
      FileUtils.touch("Gemfile")
      expect(ParallelTests.send(:bundler_enabled?)).to eq true
    end
  end

ベストアンサー1

それだけキャラクタークラス-rこれは、後で参照するためにフラグを設定し、\(グループ\)を作成せずに\S補完的なグループであるためです。スペースに一致するグループは、スペース以外のすべてに一致するグループである\sためです。\s\S

これは、空白ではなく最初の前に正規表現がs/\(\S\)/expect(\1/追加されることを意味します。expect(

# echo ' ' | sed "s/\(\S\)/expect(\1/"

# echo '   a' | sed "s/\(\S\)/expect(\1/"
   expect(a

だから私が言うのは、あなたのスクリプトが次の行を変更することです。

ParallelTests.should_not_receive(:sleep)

それを変えなければなりません:

# echo "ParallelTests.should_not_receive(:sleep)" | sed '/ *it.*do/! {
    /\.should/ {
        s/\.should/)\.to/
        s/\(\S\)/expect(\1/ 
    }
}'
expect(ParallelTests).to_not_receive(:sleep)

おすすめ記事