How to insert # with sed

General support questions
Post Reply
afewgoodman
Posts: 98
Joined: 2019/12/11 03:51:58

How to insert # with sed

Post by afewgoodman » 2020/06/08 08:12:51

Hi,

I hope to insert # to starting of copy_old_man the below the shell script. copy_old_man is a function which defined in the earlier and many functions are defined in this shell script also.

[BEFORE]
create_man () {
copy_old_man
.
.
}


[AFTER]
create_man () {
# copy_old_man
.
.
}



BR.

Thraex
Posts: 51
Joined: 2019/05/14 19:50:28

Re: How to insert # with sed

Post by Thraex » 2020/06/08 12:42:25

Should be something like:

sed -i '/^copy_old_man/s//#&/' /path/to/file

I'd test with sed -e first though to be safe.

afewgoodman
Posts: 98
Joined: 2019/12/11 03:51:58

Re: How to insert # with sed

Post by afewgoodman » 2020/06/09 01:21:31

Thraex wrote:
2020/06/08 12:42:25
Should be something like:

sed -i '/^copy_old_man/s//#&/' /path/to/file

I'd test with sed -e first though to be safe.
Hi,
Thanks to reply, but it doesn't work. sorry. it add # to function definition of copy_old_man. I hope add # calling copy_old_man in the create_man, not definition.

BR.

User avatar
jlehtone
Posts: 4530
Joined: 2007/12/11 08:17:33
Location: Finland

Re: How to insert # with sed

Post by jlehtone » 2020/06/09 07:55:42

We can't see what is in your file. How could we possibly suggest a regexp that matches (only) the correct line?

afewgoodman
Posts: 98
Joined: 2019/12/11 03:51:58

Re: How to insert # with sed

Post by afewgoodman » 2020/06/09 08:48:58

jlehtone wrote:
2020/06/09 07:55:42
We can't see what is in your file. How could we possibly suggest a regexp that matches (only) the correct line?
Hi jlehtone,

I add snippet of my shell script.
#!/bin/sh



copy_guest_tools() {
echo "Copying $GUESTTOOLS"
echo "Initiallizing and fetching submodule $GUESTTOOLS"
git submodule init
git submodule update
cp -R ./$GUESTTOOLS/ $DVD_LAYOUT/
}


modify_boot_menu() {
echo "Modifying grub boot menu"
cp ./isolinux.cfg $DVD_LAYOUT/isolinux/
}

cleanup_layout() {
echo "Cleaning up $DVD_LAYOUT"
find $DVD_LAYOUT -name TRANS.TBL -exec rm '{}' +
COMPS_XML=$(find $DVD_LAYOUT/repodata -name '*-minimal-x86_64-comps.xml' ! -name 'repomd.xml' -exec basename {} \;)
mv $DVD_LAYOUT/repodata/$COMPS_XML $DVD_LAYOUT/repodata/comps.xml
find $DVD_LAYOUT/repodata -type f ! -name 'comps.xml' -exec rm '{}' +
}

create_newiso() {
copy_guest_tools
modify_boot_menu
cleanup_layout
}
In the script I hope to disable to call 'copy_guest_tools' in 'create_newiso' by adding comment, # to starting of line of copy_guest_tools with sed or awk.

I have tried to find "create_newiso()" first, then I have tried to add # to next line, but, I can not find how to do it.

BR.

User avatar
jlehtone
Posts: 4530
Joined: 2007/12/11 08:17:33
Location: Finland

Re: How to insert # with sed

Post by jlehtone » 2020/06/09 08:57:22

Ok, the lines are:

Code: Select all

copy_guest_tools() {
copy_guest_tools
and you want to match the line that does not have parenthese after the tools?

What do you get with:

Code: Select all

sed "s/^\(copy_guest_tools[^(]\)/# \1/"

afewgoodman
Posts: 98
Joined: 2019/12/11 03:51:58

Re: How to insert # with sed

Post by afewgoodman » 2020/06/10 01:17:46

jlehtone wrote:
2020/06/09 08:57:22
Ok, the lines are:

Code: Select all

copy_guest_tools() {
copy_guest_tools
and you want to match the line that does not have parenthese after the tools?

What do you get with:

Code: Select all

sed "s/^\(copy_guest_tools[^(]\)/# \1/"
Hi jlehtone,

Sorry for confusing you.

I hope to only add # to in front of copy_guest_tools in next line create_newiso() { .

BR.

afewgoodman
Posts: 98
Joined: 2019/12/11 03:51:58

Re: How to insert # with sed

Post by afewgoodman » 2020/06/10 02:08:23

Hi,

I resolve it.

sed '/^create_newiso\(\)/,/^END/s/copy_guest_tools/\#copy_guest_tools/'

BR.

Post Reply