LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-21-2018, 03:26 PM   #1
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
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.

Last edited by BW-userx; 02-21-2018 at 03:38 PM.
 
Old 02-21-2018, 03:38 PM   #2
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Maybe this thread ...
https://www.linuxquestions.org/quest...or-awk-800171/
... will be helpful.

Daniel B. Martin

.
 
1 members found this post helpful.
Old 02-21-2018, 04:18 PM   #3
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by danielbmartin View Post
Maybe this thread ...
https://www.linuxquestions.org/quest...or-awk-800171/
... will be helpful.

Daniel B. Martin

.
Oh yeah there it is. I've been looking for that.
Thanks
 
Old 02-22-2018, 01:15 AM   #4
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,804

Rep: Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203
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...
 
Old 02-22-2018, 08:33 AM   #5
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by MadeInGermany View Post
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.
 
Old 02-22-2018, 09:42 AM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,804

Rep: Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203
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
 
1 members found this post helpful.
Old 02-22-2018, 09:57 AM   #7
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by MadeInGermany View Post
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sed removing everything before the second occurrence of a word forkbomb Linux - General 9 04-05-2016 07:17 AM
Get nth occurrence of two strings and replace it Mann_engg Linux - Newbie 2 05-30-2013 05:56 PM
LXer: Bash / sed / tr / Linux word occurrence counter and analyzer (e.g. for song lyrics) LXer Syndicated Linux News 0 05-20-2012 04:40 AM
[SOLVED] remove special tagged word using sed nourah Programming 3 08-22-2011 01:48 AM
using sed to remove word wrap Harpune Linux - Software 2 03-02-2009 06:53 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration