簡體   English   中英

嘗試使用 sed 命令將此 var 包含在 test.txt 文件中以刪除雙引號

[英]Attempting to include this var in the test.txt file using sed command to remove double quotes

我正在嘗試使用sed命令將此var包含在test.txt文件中。

嘗試了以下腳本:

#!/bin/bash
var="test 'test:test:1.0'"
sudo sed -i '/dependencies {/a '"${var}"'' test.txt

測試.txt

  dependencies {        
        implementation 'org.springframework.boot:spring-boot-starter-web'
        implementation 'org.springframework.boot:spring-boot-starter-cache'        
    }
}

group = 'org.gradle'

我正在使用sed來做它,它正在更新test.txt文件,但它添加了雙引號,

當前腳本結果:

dependencies {
"test 'test:test:1.0'"
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-cache'        
    }
}

group = 'org.gradle'

如果我從變量或sed命令中刪除雙引號會導致錯誤。

錯誤

sed: can't read 'ch.qos.logback:logback-classic:1.2.2'": No such file or directory

我怎么能忽略這個雙引號?

如果我使用 OP 的var賦值...

var="test 'test:test:1.0'"

...然后 OP 的sed生成:

  dependencies {
test 'test:test:1.0'
        implementation 'org.springframework.boot:spring-boot-starter-web'
        implementation 'org.springframework.boot:spring-boot-starter-cache'
    }
}

換句話說,我無法重現該問題。

另一方面,如果正如 KamilCuk 所推測的那樣, var包含雙引號,例如:

var="\"test 'test:test:1.0'\""

...然后我可以復制OP的輸出。

因此,假設雙引號實際上是分配給var ,一個想法是使用參數替換,例如:

var="\"test 'test:test:1.0'\""
sed "/dependencies {/a ${var//\"/}" test.txt

注意:一旦 OP 對結果感到滿意,可以將-i標志重新添加到sed調用中

這會產生:

  dependencies {
test 'test:test:1.0'
        implementation 'org.springframework.boot:spring-boot-starter-web'
        implementation 'org.springframework.boot:spring-boot-starter-cache'
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM