简体   繁体   中英

To add a string before and after, if the specific string is matched between patterns using sed in bash

Question : To add a string before and after, if the specified string is matched between patterns using sed in bash??

In the below code, I want to add /* one line above object Host "kali" { and add */ to the next line after the occurrence of } ( not to the last occurrence of } ).

This is my code

object Host "linux" {
import "windows"
address = "linux"
groups = ["linux"]
}


object Host "kali" {
import "linux"
address = "linux"
groups = [linux ]
}


object Host "windows" {
import "linux"
address = "linux"
groups = ["windows" ]
}

This is the expected output:

object Host "linux" {
import "windows"
address = "linux"
groups = ["linux"]
}

/*
object Host "kali" {
import "linux"
address = "linux"
groups = [linux ]
}
*/

object Host "windows" {
import "linux"
address = "linux"
groups = ["windows" ]
}

**This is what I tried**


#! /bin/bash
NONE='\033[00m'
RED='\033[01;31m'
GREEN='\033[0;32m'
clear
echo -e  "Enter the names to comment in config file"
cat > comment-file.txt
clear
echo -e "#################################################################################"
echo "Please wait. The names will be commented shortly............"
echo -e "#################################################################################"
echo "Dont press any button, Please hold on...."
while read -r names
do
loc=$(grep -il "object.*Host.*\"$names.*\"" /home/jo/folders/test-sc/*.txt)
if [ -z $loc ]
then
        echo -e " $names$RED No Object definition found $NONE "
else
sed -i '/object Host \"$names.*\" {/ { s,^,/*\n,
    : loop
    /}/ {
        s,$,\n*/,
        p
        d
    }
    N
    b loop
}' "$loc"

  echo -e " $names - $loc -   $GREEN Object host defenition commented $NONE "
fi
done < comment-file.txt
echo -e "#################################################################################"
echo -e "\t\t\t\t Script completed \t\t\t\t"
echo -e "#################################################################################"

rm -rf comment-file.txt

Error :

No changes had been made in the output file which means /home/jo/folders/test-sc/*.txt

This might work for you (GNU sed):

sed -e '/object Host "kali"/{i\/*' -e ':a;n;/}/!ba;a\*/' -e '}' file

Look for a line containing object Host "kali" insert a line before it containing /* , read/print further lines until one containing } and append the line */ .

After finding object Host "kali" { :

  1. Prepend /*\n to the pattern space
  2. If } is seen, append \n*/ to the pattern space, p rint and d elete
  3. Append N ext line to the pattern space and loop back to step 2.
sed -e '/object Host "kali" {/ {
    s,^,/*\n,
    : loop
    /}/ {
        s,$,\n*/,
        p
        d
    }   
    N
    b loop
}'

.... addendum... To properly pass "$names" to be a part of the sed script, we will need to follow the quoting rules for sh ... the idea will be to embedding "$names" into the sed script, and the sed line will look like the following:

sed -i -e "/object Host \"$names\" {/ {
    s,^,/*\n,
    : loop
    /}/ {
        s,$,\n*/,
        p
        d
    }   
    N
    b loop
}" "$loc"

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