简体   繁体   中英

Delete lines in range from file

I have text file with content like below, i want to delete the lines from specific position.. like from -displayName: "TIBCO_EMS_2" to excludeTopics: []

I tried like below.

DP=TIBCO_EMS_2
sed "/- displayName: $DP/,/excludeTopics/d" config.yml

But still no luck, is there any right command to use instead of sed.

emsServers:

   - displayName: "TIBCO_EMS_1"
     host: "8988.auf.net"
     port: 7443
     protocol: "tcp"
     user: "admin"
     password: ""
     encryptedPassword: "xzID9SbHsQTMkoRSVTGu4g=="
     encryptionKey: "EncryptionKey"
     showTemp: false
     showSystem: false
     excludeQueues: [sample1 sample2]
     excludeTopics: []

   - displayName: "TIBCO_EMS_2"
     host: "8988.auf.net"
     port: 7333
     protocol: "tcp"
     user: "admin"
     password: ""
     encryptedPassword: "d4FqX7O6bZCDs2corcd2eA=="
     encryptionKey: "EncryptionKey"
     showTemp: false
     showSystem: false
     excludeQueues: [sample1 sample2]
     excludeTopics: []

You can try this awk :

awk -vRS= '!/displayName: "TIBCO_EMS_2"/{print}' filename

Without changing original format,

awk -vRS= '!/displayName: "TIBCO_EMS_2"/{print}' ORS="\n\n" filename

If you want to pass value in variable,

DP='"TIBCO_EMS_2"'
awk -vVAR="$DP" -vRS= '{patt="displayName: "VAR} $0 !~ patt{print}' ORS="\n\n"

您错过了引号。

DP=TIBCO_EMS_2 sed "/- displayName: \"$DP\"/,/excludeTopics/d" config.yml

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM