LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-16-2015, 01:02 PM   #1
Ben Wang
LQ Newbie
 
Registered: Apr 2015
Posts: 16

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

Last edited by Ben Wang; 05-16-2015 at 06:13 PM.
 
Old 05-16-2015, 01:17 PM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,145

Rep: Reputation: 886Reputation: 886Reputation: 886Reputation: 886Reputation: 886Reputation: 886Reputation: 886
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)

Last edited by schneidz; 05-16-2015 at 01:18 PM.
 
1 members found this post helpful.
Old 05-16-2015, 01:20 PM   #3
Ben Wang
LQ Newbie
 
Registered: Apr 2015
Posts: 16

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by schneidz View Post
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)
 
Old 05-16-2015, 02:03 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,530

Rep: Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897
I'm confused?? You just asked to put the '#' symbol there ... if you don't want it don't run the sed command.
 
Old 05-16-2015, 02:47 PM   #5
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,145

Rep: Reputation: 886Reputation: 886Reputation: 886Reputation: 886Reputation: 886Reputation: 886Reputation: 886
Quote:
Originally Posted by grail View Post
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.
 
1 members found this post helpful.
Old 05-16-2015, 06:05 PM   #6
Ben Wang
LQ Newbie
 
Registered: Apr 2015
Posts: 16

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
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 #?
 
Old 05-16-2015, 06:23 PM   #7
Ben Wang
LQ Newbie
 
Registered: Apr 2015
Posts: 16

Original Poster
Rep: Reputation: Disabled
I already know that, just reverse it.
Thanks a lot.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
How to show selected string using grep from file and replace it with new input string prasad1990 Linux - Software 2 03-19-2015 08:02 AM
What happened when passing a static string to a std::string& parameter? fantasy1215 Programming 7 11-30-2012 07:34 AM
How to use Vi to match a string of text, add a new line, insert string... Slycraft Linux - Newbie 7 07-20-2012 10:24 AM
[SOLVED] copy string a to string b and change string b with toupper() and count the chars beep3r Programming 3 10-22-2010 07:22 PM
read string after specific string from a text file using C++ programing language badwl24 Programming 5 10-08-2009 05:41 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:10 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration