[SOLVED] how to sed remove text on Nth occurrence of same word
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
how to sed remove text on Nth occurrence of same word
from what I've read online, it states just write this.
Code:
sed 's:exit(0):\/* removed *\/:2' /home/userx/bin/main.c
the '2' indicating the 2nd occurrence of that word, nope, it works if I use nothing or g but replaces both occurrences of that word. and yes that this the correct number, it , exit(0), is only in that file 2 times.
Code:
if (event.xbutton.time - lastTime < 250) {
if (system(cmd) == -1) {
fprintf(stdout, "Failed to run command:%s\n", cmd);
exit(0);
}
} else {
lastTime = event.xbutton.time;
}
} else if (event.xbutton.button == Button3) {
exit(0);
}
/*printf("ButtonRelease\n");*/
break;
}
}
running without g
Code:
case ButtonRelease:
if (event.xbutton.button == Button1) {
if (event.xbutton.time - lastTime < 250) {
if (system(cmd) == -1) {
fprintf(stdout, "Failed to run command:%s\n", cmd);
/* removed */;
}
} else {
lastTime = event.xbutton.time;
}
} else if (event.xbutton.button == Button3) {
/* removed */;
}
/*printf("ButtonRelease\n");*/
break;
}
}
usleep(10000);
}
/* we should never get here */
return (0);
}
#ran this
userx@slackwhere101:~
$ sed 's:exit(0):\/* removed *\/:' /home/userx/bin/main.c
I only need that last one to be removed leaving the semi-colon.
The 2 modifier means the 2nd match within the line.
Example
Code:
exit(0); exit(0); exit(0);
Likewise the g modifier matches all occurences within the line.
Note that the repeated matches go from left to right and must not overlap (what matched once is left from the next match).
For counting the matches in the whole file you need a variable or an awful trick. And sed does not have variables...
The 2 modifier means the 2nd match within the line.
Example
Code:
exit(0); exit(0); exit(0);
Likewise the g modifier matches all occurrences within the line.
Note that the repeated matches go from left to right and must not overlap (what matched once is left from the next match).
For counting the matches in the whole file you need a variable or an awful trick. And sed does not have variables...
I know what the g does, but that 2 nd was not working like I was reading it is done, then I came across something that said the very first one is not counted, or some such strange this as that.
because their are only 2 exit (0) within that entire file, and I just needed to remove that 2nd one.
this is what worked for me.
Code:
sed -i -e '0,/exit(0)/! {0,/exit(0)/ s/exit(0)/\/*removed exit to keep dockapp running *\//}' $PWD/src/main.c
sed -i -e '0,/strcat/ s/strcat/strcpy/' $PWD/src/main.c
whereas the second line I needed to get just the very first occurrence of the same thing which was in the file 3 times, to fix that.
You are using two nested scopes (only GNU sed allows this).
The following is a general+portable sed trick that (mis-)uses the hold buffer for counting.
Code:
sed -i '
/exit(0)/{
x
# add a dot to the hold space
s/$/./
# #dots==#desired?
/^.\{2\}$/{
# do the replacement action
x
s:exit(0):/* removed */:
x
}
x
}
' $PWD/src/main.c
You are using two nested scopes (only GNU sed allows this).
The following is a general+portable sed trick that (mis-)uses the hold buffer for counting.
Code:
sed -i '
/exit(0)/{
x
# add a dot to the hold space
s/$/./
# #dots==#desired?
/^.\{2\}$/{
# do the replacement action
x
s:exit(0):/* removed */:
x
}
x
}
' $PWD/src/main.c
it is for a slackbuild, so that is all it was going to be used on, but, that worked too, (of course). as long as it works.thanks.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.