search and replace the whole line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting search and replace the whole line
# 8  
Old 09-25-2007
OK,
try this one:

Code:
sed '1,/SOURCEFILE=\/usr\/platform\/sun4v\/driver\/file.cfg/s/4v/4x/' filename

P.S. Note that you may need to insert the whole pattern after the s command ...
ex:

Code:
sed '1,/SOURCEFILE=\/usr\/platform\/sun4v\/driver\/file.cfg/s/SOURCEFILE=\/usr\/platform\/sun4v\/driver\/file.cfg/SOURCEFILE=\/usr\/platform\/sun4x\/driver\/file.cfg/' filename


Last edited by radoulov; 09-25-2007 at 12:46 PM.. Reason: addition
# 9  
Old 09-25-2007
Hi,

Iam sorry but that one failed too. It replaced everywhere it found sun4v.
Is there anyway i could insert a comment ('#') in front of that line and append a new line with the new variable next to that line.

Like,

#SOURCEFILE=/usr/platform/sun4v/driver/file.cfg
SOURCEFILE=/usr/platform/sun4x/driver/file.cfg


Thanks for your help.
# 10  
Old 09-25-2007
Code:
nawk '{
	if(!(checked)&&($0~/SOURCEFILE=\/usr\/platform\/sun4v\/driver\/file.cfg/)){
		print "#"$0
		sub(/4v/,"4x")
		checked=1
		print 
	}else{
		print
		}
}' file

# 11  
Old 09-25-2007
Hi,

Iam sorry i didnt see ur edited message.
Here's the command i tried

sed '1,/SOURCEFILE=\/usr\/platform\/sun4v\/driver\/file.cfg/s/SOURCEFILE=\/usr\/platform\/sun4v\/driver\/file.cfg/SOURCEFILE=\/usr\/platform\/sun4x\/driver\/file.cfg/' test.cfg

It returns nothing , the file remains unchanged.
Also i tried the nawk script the file remains unchanged and it prints the whole file with no changes in it.

Thanks for your help.
# 12  
Old 09-25-2007
Hm,
may be there is an error in the pattern,
did you copy/paste-it in your post, or you modified it by hand?
Here is what I get:

Code:
$ uname -r
5.8
$ cat file
SUN4V_AVERAGE_MESSAGE_SIZE=128
SUN4V_MESSAGES_IN_INITIAL_RESPONSES=6500
SUN4V_ENFORCE_YIELD_NAMES=FALSE

Similar to the above lines there are lots of variable declarations with the variable SUN4V

#SOURCEFILE_LOCATION---------------------------

SOURCEFILE=/usr/platform/sun4v/driver/file.cfg
SOURCEFILE=/usr/platform/sun4v/driver/file.cfg
SUN4V_AVERAGE_MESSAGE_SIZE=128
SUN4V_MESSAGES_IN_INITIAL_RESPONSES=6500
SUN4V_ENFORCE_YIELD_NAMES=FALSE
$ nawk '{
if(!(checked)&&($0~/SOURCEFILE=\/usr\/platform\/sun4v\/driver\/file.cfg/)){
print "#"$0
sub(/4v/,"4x")
checked=1
print 
}else{
print
}
}' file
SUN4V_AVERAGE_MESSAGE_SIZE=128
SUN4V_MESSAGES_IN_INITIAL_RESPONSES=6500
SUN4V_ENFORCE_YIELD_NAMES=FALSE

Similar to the above lines there are lots of variable declarations with the variable SUN4V

#SOURCEFILE_LOCATION---------------------------

#SOURCEFILE=/usr/platform/sun4v/driver/file.cfg
SOURCEFILE=/usr/platform/sun4x/driver/file.cfg
SOURCEFILE=/usr/platform/sun4v/driver/file.cfg
SUN4V_AVERAGE_MESSAGE_SIZE=128
SUN4V_MESSAGES_IN_INITIAL_RESPONSES=6500
SUN4V_ENFORCE_YIELD_NAMES=FALSE


With sed:

Code:
$ sed '1,/SOURCEFILE=\/usr\/platform\/sun4v\/driver\/file.cfg/s/SOURCEFILE=\/usr\/platform\/sun4v\/driver\/file.cfg/SOURCEFILE=\/usr\/platform\/sun4x\/driver\/file.cfg/' file    
SUN4V_AVERAGE_MESSAGE_SIZE=128
SUN4V_MESSAGES_IN_INITIAL_RESPONSES=6500
SUN4V_ENFORCE_YIELD_NAMES=FALSE

Similar to the above lines there are lots of variable declarations with the variable SUN4V

#SOURCEFILE_LOCATION---------------------------

SOURCEFILE=/usr/platform/sun4x/driver/file.cfg
SOURCEFILE=/usr/platform/sun4v/driver/file.cfg
SUN4V_AVERAGE_MESSAGE_SIZE=128
SUN4V_MESSAGES_IN_INITIAL_RESPONSES=6500
SUN4V_ENFORCE_YIELD_NAMES=FALSE

# 13  
Old 09-25-2007
Quote:
Originally Posted by Jartan
[...]
It returns nothing , the file remains unchanged.
[...]
The file will remain unchanged anyway,
you'll get only modified output.
# 14  
Old 09-25-2007
Hi,

I get the same output now as you get, but i need to save the changes in the same file, so what can i do for that.

Thanks a lot for your help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Multiple line search, replace second line, using awk or sed

All, I appreciate any help you can offer here as this is well beyond my grasp of awk/sed... I have an input file similar to: &LOG &LOG Part: "@DB/TC10000021855/--F" &LOG &LOG &LOG Part: "@DB/TC10000021852/--F" &LOG Cloning_Action: RETAIN &LOG Part: "@DB/TCCP000010713/--A" &LOG &LOG... (5 Replies)
Discussion started by: KarmaPoliceT2
5 Replies

2. Shell Programming and Scripting

Search text and replace line next to it

I have a file which requires modification via a shell script. Need to do the following: 0. take input from user for new text. 1. search for a keyword in the file. 2. replace the line next to this to this keyword with user supplied input. for e.g., my file has the following text: (some... (7 Replies)
Discussion started by: chingupt
7 Replies

3. UNIX for Advanced & Expert Users

Search and replace a line in perl

Hi All, i can replace a perticular value in sentence using perl. perl -pi -e 's/old/new/' sample.txt but i am not able to replace whole string by perl. file1 contains "jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=10.147.109.211)(PORT=1526))(CONNECT_DATA=(SID= MWDBD22)))". i... (3 Replies)
Discussion started by: arindam guha
3 Replies

4. Shell Programming and Scripting

perl search and replace - search in first line and replance in 2nd line

Dear All, i want to search particular string and want to replance next line value. following is the test file. search string is tmp,??? ,10:1 "???" may contain any 3 character it should remain the same and next line replace with ,10:50 tmp,123 --- if match tmp,??? then... (3 Replies)
Discussion started by: arvindng
3 Replies

5. Shell Programming and Scripting

Multi-Line Search and Replace

There appears to be several threads that touch on what I'm trying to do, but nothing quite generic enough. What I need to do is search through many (poorly coded) HTML files and make changes. The catch is that my search string may be on one line or may be on several lines. For example there... (5 Replies)
Discussion started by: bisbell
5 Replies

6. UNIX for Advanced & Expert Users

Search Parameter in first line and replace next line content

Hi, I need help. I have XML file as below <a n="infoLevel"> <v s="true"/> </a> <a n="localAddr"> <v s="server.host.com"/> </a> <a n="ListenPort"> <v s="21111"/> </a> I need to find variable "ListenPort" in line and then replace... (4 Replies)
Discussion started by: rdtrivedi
4 Replies

7. Shell Programming and Scripting

search a pattern and replace the whole line

Hi All, I have a requirement where I have to find a pattern in a file and comment the whole line containing the search pattern. Any ideas in shell is welcome. Thanks in advance, Regards, Arun (3 Replies)
Discussion started by: arun_maffy
3 Replies

8. Shell Programming and Scripting

search for a string ,replace the whole line with new line

hai i am very new to unix. i am having two files like this. first.properties cache.ZA.TL_CCY=SELECT trim(CCY_CODE)||trim(COUNTRY_CODE)||trim(CITY_CODE) AS... (4 Replies)
Discussion started by: kkraja
4 Replies

9. Shell Programming and Scripting

Perl: Search for string on line then search and replace text

Hi All, I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text. An example of 4 lines in my file is: 1. MatchText_randomNumberOfText moreData ReplaceMe moreData 2. MatchText_randomNumberOfText moreData moreData... (4 Replies)
Discussion started by: Crypto
4 Replies

10. Shell Programming and Scripting

sed search and replace in next line

Hello, I am hoping someone can provide some guidance on using context based search and replace to search for a pattern and then do a search and replace in the line that follows it. For example, I have a file that looks like this: <bold>bold text </italic> somecontent morecontent... (3 Replies)
Discussion started by: charissaf67
3 Replies
Login or Register to Ask a Question