【问题标题】:sed append string to the end of matching linessed 将字符串追加到匹配行的末尾
【发布时间】:2021-09-17 17:01:14
【问题描述】:

我是 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].*'

【问题讨论】:

    标签: regex macos awk sed


    【解决方案1】:

    编辑: 使用 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,然后在找到的地方用新字符串替换行尾。 p>

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

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

    【讨论】:

    • @nitinupadhyay,如果您想更具体地了解您的情况,请尝试sed -E '/pod\s+'"'"'[a-zA-Z0-9]+'"'"',\s+('"'"'([0-9]+\.){1,}[0-9]+)?(:git\s+=>\s+'"'"'.*branch.*)?/s/$/ :binary => true/' file 一次,让我知道这是否对您有帮助?
    • 以上不行,文件没有变化
    • @nitinupadhyay,它将打印输出,要更改为文件,我们需要使用-i 选项。让我知道。
    • 我用sed -i "" -E '/pod\s+'"'"'[a-zA-Z0-9]+'"'"',\s+('"'"'([0-9]+\.){1,}[0-9]+)?(:git\s+=>\s+'"'"'.*branch.*)?/s/$/ :binary => true/' TestPodfile文件没有效果
    • @nitinupadhyay,如果打印正确,请先在您的文件上运行sed -E '/pod\s+'"'"'[a-zA-Z0-9]+'"'"',\s+('"'"'([0-9]+\.){1,}[0-9]+)?(:git\s+=>\s+'"'"'.*branch.*)?/s/$/ :binary => true/' Input_file
    猜你喜欢
    • 2017-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-11
    相关资源
    最近更新 更多