sed 将字符串追加到匹配行的末尾

时间:2021-07-07 06:33:07

标签: regex macos awk sed

我是 sed 新手,我有一个包含以下内容的文件

# 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'
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

我想添加, :binary => true 在以 pod 开头并匹配条件 pod 'anyWord', 'Any Number' & pod 'anyWord', :git => 'Anyword', :branch => 'anyWord' 的所有行的末尾 总结一下输出应该是

# 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', :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', :binary => true
  pod 'ABC', :git => 'git@bitbucket.org:mytmteam/abc.git', :branch => 'debug101', :binary => true
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

请帮助我在 Mac 终端上使用 sed 我最初尝试了以下正则表达式 pod '[a-zA-Z_-]*', '[0-9].*'

1 个答案:

答案 0 :(得分:2)

编辑: 使用 OP 后,以下版本正在运行。这将就地保存到 Input_file 本身。将匹配具有匹配模式的行并为其添加新值字符串。

awk '/pod \047.*\047,[[:space:]]+(\047([0-9]+\.){1,}[0-9]+\047?)/ || /pod[[:space:]]+\047[[:alnum:]]+\047,[[:space:]]+:git[[:space:]]+=>[[:space:]]+\047git.*:(commit|branch)[[:space:]]+=>[[:space:]]+\047[[:alnum:]]+\047/{$0=$0 " :binary => true"} 1' file > temp && mv temp file

正则表达式的解释:

  • /pod \047.*\047,[[:space:]]+(\047([0-9]+\.){1,}[0-9]+\047?) 检查行是否有 pod(string) 后跟 ' 直到 ' 后跟逗号空格后跟 ' 数字(1 次或多次出现)后跟 .(整个组出现 1 次或多次)后跟 [0-9]+\047? 位数字 '(可选)。
  • pod[[:space:]]+\047[[:alnum:]]+\047,[[:space:]]+:git[[:space:]]+=>[[:space:]]+\047git.*:(commit|branch)[[:space:]]+=>[[:space:]]+\047[[:alnum:]]+\047 解释:

pod[[:space:]]+\047[[:alnum:]]+\047,    ##Matching pod followed by spaces following by ' follwed by alphanumeric(1 or more occurrences) followed by ',
[[:space:]]+:git[[:space:]]+=>          ##Followed by space(s) colon git followed by spaces here.
[[:space:]]+\047git.*:(commit|branch)   ##Matching spaces 'git till : commit or branch.
[[:space:]]+=>[[:space:]]+\047[[:alnum:]]+\047 ##Matching spaces => spaces ' alphanumeric followed by ' here.


您可以使用这个简单的 sed 来做同样的事情。这不会对 Input_file 进行就地更新,运行此命令,一旦您对终端上显示的结果感到满意,您就可以使用 -i 选项对以下命令进行就地保存。

sed '/pod/s/$/ :binary => true/' Input_file

解释: 简单的解释是,在每一行中查找字符串 pod,然后在任何找到的地方用新字符串替换行尾。

如果您在替换之前对搜索条件更具体,请尝试以下代码。

sed -E '/pod\s+'"'"'[a-zA-Z0-9]+'"'"',\s+('"'"'([0-9]+\.){1,}[0-9]+)?(:git\s+=>\s+'"'"'.*branch.*)?/s/$/ :binary => true/' Input_file