bash 匹配 2 个文件的内容并将字符串追加到匹配行

bash Match contents of 2 files and append string to matching lines

我正在 Mac OS 工作,我是 shell 的新手。 我有 2 个文件,一个 Main.txt 其内容是:

# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'

target 'Sample' do
  # Comment the next line if you don't want to use dynamic frameworks
  use_frameworks!

  # Pods for Sample
  pod 'FirebaseCore', '7.8.0'
  pod 'GoogleUtilities', '7.2.2'
  pod 'FirebaseMessaging', '7.8.0'
  pod 'FirebaseCrashlytics', '7.8.0'
  pod 'FirebaseAnalytics', '7.8.0'
  pod 'FirebasePerformance', '7.8.0'
  pod 'Fluper', '2.0.0.1'
  pod 'lottie-ios', '2.5.0
  pod 'XYZ', :git => 'git@bitbucket.org:myteam/xyz.git', :commit => 'a32d154'
  pod 'ABC', :git => 'git@bitbucket.org:mytmteam/abc.git', :branch => 'debug101'
  pod 'myProject-auth-test', '2.0.0'
  pod 'myProject-network-test', '2.0.1'
  pod 'myProject-core-test', '2.0.1'
end

post_install do |installer|
  installer.pods_project.targets.each do |target|
    target.build_configurations.each do |config|
      config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '11.0'
          if config.name.include?("Release") || config.name.include?("Adhoc")
        config.build_settings['LLVM_LTO'] = 'YES_THIN'
      elsif config.name.include?("Debug")
        config.build_settings['LLVM_LTO'] = 'NO'
      end
    end
  end
end

还有一个是Module.txt其中的内容是:

  pod 'FirebaseCore'
  pod 'GoogleUtilities'
  pod 'FirebaseMessaging'
  pod 'FirebaseCrashlytics'
  pod 'FirebaseAnalytics'
  pod 'FirebasePerformance'
  pod 'Fluper'
  pod 'lottie-ios'
  pod 'myProject-auth-test'
  pod 'myProject-network-test'
  pod 'myProject-core-test'

我要追加

, :binary => true

在所有匹配行的末尾 In Main.txt from Module.txt, 所以预期的输出应该是(Main.txt 的内容):

  pod 'FirebaseCore', '7.8.0', :binary => true
  pod 'GoogleUtilities', '7.2.2', :binary => true
  pod 'FirebaseMessaging', '7.8.0', :binary => true
  pod 'FirebaseCrashlytics', '7.8.0', :binary => true
  pod 'FirebaseAnalytics', '7.8.0', :binary => true
  pod 'FirebasePerformance', '7.8.0', :binary => true
  pod 'Fluper', '2.0.0.1', :binary => true
  pod 'lottie-ios', '2.5.0, :binary => true
  pod 'XYZ', :git => 'git@bitbucket.org:myteam/xyz.git', :commit => 'a32d154' 
  pod 'ABC', :git => 'git@bitbucket.org:mytmteam/abc.git', :branch => 'debug101'
  pod 'myProject-auth-test', '2.0.0', :binary => true
  pod 'myProject-network-test', '2.0.1', :binary => true
  pod 'myProject-core-test', '2.0.1', :binary => true

以下条目将被忽略,因为它们不在 Module.txt 个文件中

pod 'XYZ', :git => 'git@bitbucket.org:myteam/xyz.git', :commit => 'a32d154'
pod 'ABC', :git => 'git@bitbucket.org:mytmteam/abc.git', :branch => 'debug101'

这匹配 Main.txt 中 Modules.txt 的每一行,并将 , :binary => true 附加到该行。它很脆弱,但假设输入数据的格式与问题中给出的格式相似,它就可以正常工作。

while IFS="" read -r line; do
    sed -i "/^${line}/s/$/, :binary => true/" Main.txt
done < Module.txt

将Module.txt转换为合适的正则表达式,然后就地修改Main.txt:

match=$(sed '$!s/.*/^&.*\|/; $s/.*/^&.*/' Module.txt | tr -d '\n')
sed -i "/$match/s/$/, :binary => true/" Main.txt

这可能适合您 (GNU sed):

sed 's#.*#/&/ba#' modulesFile | sed -f - -e 'b;:a;s/$/, :binary => true/' file

modulesFile 创建一个 sed 命令文件并将其通过管道传递给针对 file

的第二次 sed 运行 调用

N.B。如果 file 中的一行与 modulesFile 中的地址不匹配,它将不会被修改,即 b 命令在替换之前中断。