Awk 在渐变文件中放置一个大括号块
我有一个build.gradle文件的片段,如下所示:Awk 在渐变文件中放置一个大括号块,awk,curly-braces,Awk,Curly Braces,我有一个build.gradle文件的片段,如下所示: android { compileSdkVersion 24 buildToolsVersion "24.0.1" defaultConfig { applicationId "com.softtech.myApp" minSdkVersion 17 targetSdkVersion 24 versionCode
android {
compileSdkVersion 24
buildToolsVersion "24.0.1"
defaultConfig {
applicationId "com.softtech.myApp"
minSdkVersion 17
targetSdkVersion 24
versionCode 1
versionName "1.1"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt')
}
}
}
我需要将下面的块放入上面的gradle代码段(在jenkins execute shell中)
例如:
android {
compileSdkVersion 24
buildToolsVersion "24.0.1"
defaultConfig {
applicationId "com.softtech.myApp"
minSdkVersion 17
targetSdkVersion 24
versionCode 1
versionName "1.1"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
*myConfig{
debug{
myFile = file("/Users/user1/a/b/c/d/x.file")
}
}*
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt')
}
}
}
我已经生锈了,因为我已经很长时间没有使用高级AWK了,我非常感谢您的帮助,无论是AWK、sed还是perl
尝试将RS用作“}\n生成”和打印字段,但无效
谢谢您可以使用GNU-sed:
创建一个名为replace.txt的文本文件,并使用适当的间距
myConfig{
debug{
myFile = file("/Users/john/a/b/c/d/x.file")
}
}
使用sed在“buildTypes”上方插入文本
(将来,请将您自己试图解决问题的代码包含在您的问题中)是否没有能够理解该语言的编辑“gradle”文件的工具?就像JSON有
jq
,YAML有yq
等等?一定有,寻找它这是一个肮脏的解决办法。在gradle文件中,一个签名部分实际上丢失了,它必须放在那里并提交给git,我在那里没有可访问性来更改它,这就是为什么要寻找解决方法。thanksI使用了该命令,但失败了。Sed-i不受支持,但我尝试了如下替代方法:|=>uname Darwin |=>Sed-i”/buildTypes/e cat replace.txt“file.txt Sed:1:“file.txt”:无效命令代码f |=>Sed”/buildTypes/e cat replace.txt“file.txt>file.tmp&&mv file.tmp>file.txt Sed:1:“/buildTypes/e cat repla…”:无效的命令代码e
Yep,此命令需要使用GNU。它不会与BSD sed(“macos”sed)一起工作。非常感谢。真的,为我创建这项工作太乏味了。我很惊讶你竟然想到了缩进,并在实际操作之前设置它。干杯
myConfig{
debug{
myFile = file("/Users/john/a/b/c/d/x.file")
}
}
sed -i "/buildTypes/e cat replace.txt" file.txt
$ cat tst.sh
#!/usr/bin/env bash
awk '
NR==FNR { rec = (NR>2 ? rec ORS : "") $0; next }
$1 == "buildTypes" {
indent = $0
sub(/[^[:space:]].*/,"",indent)
gsub(/\n/,"&"indent,rec)
print indent rec
}
{ print }
' <( printf '
myConfig{
debug{
myFile = file("/Users/john/a/b/c/d/x.file")
}
}
' ) "$1"
$ ./tst.sh file.gradle
android {
compileSdkVersion 24
buildToolsVersion "24.0.1"
defaultConfig {
applicationId "com.softtech.myApp"
minSdkVersion 17
targetSdkVersion 24
versionCode 1
versionName "1.1"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
myConfig{
debug{
myFile = file("/Users/john/a/b/c/d/x.file")
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt')
}
}
}