LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to add and delete a string in front of a string by sed? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-add-and-delete-a-string-in-front-of-a-string-by-sed-4175542787/)

Ben Wang 05-16-2015 01:02 PM

how to add and delete a string in front of a string by sed?
 
I am using R program. Part of my program is as following
Code:

  pvper[j,6]=PCH(y,x,numper)
  pvper[j,7]=O_cluster2(y,x,numper)
  pvper[j,8]=O_cluster(y,x,numper)

I want it to be like this
Code:

  pvper[j,6]=PCH(y,x,numper)
  pvper[j,7]=O_cluster2(y,x,numper)
 #pvper[j,8]=O_cluster(y,x,numper)

I use
Code:

sed '/pvper\[j,8\]=/a #' K_10_mmodel_1.R
. But it does not work.
I also tried
Code:

sed '/pvper\[j,8\]=/i #' K_10_mmodel_1.R
. But it does not work.

schneidz 05-16-2015 01:17 PM

is this what you are after:
Code:

[schneidz@hyper ~]$ cat ben.wang
  pvper[j,6]=PCH(y,x,numper)
  pvper[j,7]=O_cluster2(y,x,numper)
  pvper[j,8]=O_cluster(y,x,numper)
[schneidz@hyper ~]$ sed 's/ pvper\[j,8\]/#pvper\[j,8\]/' ben.wang
  pvper[j,6]=PCH(y,x,numper)
  pvper[j,7]=O_cluster2(y,x,numper)
 #pvper[j,8]=O_cluster(y,x,numper)


Ben Wang 05-16-2015 01:20 PM

Quote:

Originally Posted by schneidz (Post 5363324)
is this what you are after:
Code:

[schneidz@hyper ~]$ cat ben.wang
  pvper[j,6]=PCH(y,x,numper)
  pvper[j,7]=O_cluster2(y,x,numper)
  pvper[j,8]=O_cluster(y,x,numper)
[schneidz@hyper ~]$ sed 's/ pvper\[j,8\]/#pvper\[j,8\]/' ben.wang
  pvper[j,6]=PCH(y,x,numper)
  pvper[j,7]=O_cluster2(y,x,numper)
 #pvper[j,8]=O_cluster(y,x,numper)


Thanks a lot. Could you please tell how to delete # in front of pvper[j,8]
Code:

pvper[j,6]=PCH(y,x,numper)
  pvper[j,7]=O_cluster2(y,x,numper)
 #pvper[j,8]=O_cluster(y,x,numper)


grail 05-16-2015 02:03 PM

I'm confused?? You just asked to put the '#' symbol there ... if you don't want it don't run the sed command.

schneidz 05-16-2015 02:47 PM

Quote:

Originally Posted by grail (Post 5363343)
I'm confused?? You just asked to put the '#' symbol there ... if you don't want it don't run the sed command.

i initially thought the same but i'll give op the benefit of the doubt that they are just looking for a practical example of how to do the reverse. since it is very similar to my first post i will allow the op to trivially do the sed substitution rather than spoon-feed them the answer.

Ben Wang 05-16-2015 06:05 PM

Quote:

Originally Posted by grail (Post 5363343)
I'm confused?? You just asked to put the '#' symbol there ... if you don't want it don't run the sed command.

I mean if I have following
Code:

pvper[j,6]=PCH(y,x,numper)
  pvper[j,7]=O_cluster2(y,x,numper)
 #pvper[j,8]=O_cluster(y,x,numper)

.
How can I delete #?

Ben Wang 05-16-2015 06:23 PM

I already know that, just reverse it.
Thanks a lot.


All times are GMT -5. The time now is 09:23 PM.