Help with search and replace


 
Thread Tools Search this Thread
# 1  
Help with search and replace

Hi, I am using vi. I need help with search and replace.

I have a file close to 100 lines. I want to search /var and replace with rm /var.

Code:
/var/sadm/pkg/SUNWcsu/save/125378-03/undo.Z
/var/sadm/pkg/SUNWcsr/save/pspool/SUNWcsr/save/127718-02/undo.Z
/var/sadm/pkg/SUNWcsr/save/127718-02/undo.Z

My results should look like this..

Code:
rm /var/sadm/pkg/SUNWcsu/save/125378-03/undo.Z
rm /var/sadm/pkg/SUNWcsr/save/pspool/SUNWcsr/save/127718-02/undo.Z
rm /var/sadm/pkg/SUNWcsr/save/127718-02/undo.Z

Please help. I did :s didn't work. the rm word comes before /
# 2  
Code:
:1,$s!/var!rm /var!g

# 3  
Try this:
Code:
sed -e 's/^\/var/rm &/g' <file>

# 4  
The / separator is arbitrary. You can use a different separator such as ! as I used in this case.
# 5  
Substitute /var at the beginning of the line only!
Code:
:1,$s!^/var!rm /var!

 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Science: Mathematics
Difficulty: Medium
Zero factorial is equal to zero.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Nested search in a file and replace the inner search

Hi Team, I am new to unix, please help me in this. I have a file named properties. The content of the file is : ##Mobile props east.url=https://qa.east.corp.com/prop/end west.url=https://qa.west.corp.com/prop/end south.url=https://qa.south.corp.com/prop/end... (2 Replies)
Discussion started by: tolearn
2 Replies

2. UNIX for Dummies Questions & Answers

Help with search and replace or search only of / in vi

Hi all, I am editing a config file in vi that has a / on it. At the moment, search and replace looks alright as am able to use a # as a temporary separator, i.e. :,$s#/u01/app#/u02/app#g For doing a search, I have to escape the / do. So if I want to search for /u01/app, I am having to do... (2 Replies)
Discussion started by: newbie_01
2 Replies

3. 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

4. Shell Programming and Scripting

Search and Replace

Hi!!! I have following xml file with 3 sections. aaa1bbb-ccc default aaa1bbbdd default 0 11 23 * * aaa2bbb-ccc default aaa2bbbdd default 0 11 23 * * aaa3bbb-ccc default aaa3bbbdd default 0 15 23 * * (15 Replies)
Discussion started by: tdev457
15 Replies

5. Shell Programming and Scripting

search and replace

Hi, I am trying to obtain a string from a file and replace it in another file. Here is my code # Environment Variables CRED_PATH=/fxxxx/testpwd/ CRED_FILE=password.credential DOMAIN_CFG=/fxxxx/testpwd/ CFG_FILE=file.xml NEW_PWD=`cat ${CRED_PATH}${CRED_FILE}` #echo $NEW_PWD... (5 Replies)
Discussion started by: userscript
5 Replies

6. Shell Programming and Scripting

search and replace in VI

Hi, I want to searcha nd replace a string in file. I am using VI I tried d comm :s/search_string/replacement_string/g but it is giving me " string not found" i know for sure the search_string is there in file. please help me how to do this thanks, Firestar (5 Replies)
Discussion started by: firestar
5 Replies

7. Shell Programming and Scripting

awk - replace number of string length from search and replace for a serialized array

Hello, I really would appreciate some help with a bash script for some string manipulation on an SQL dump: I'd like to be able to rename "sites/WHATEVER/files" to "sites/SOMETHINGELSE/files" within the sql dump. This is quite easy with sed: sed -e... (1 Reply)
Discussion started by: otrotipo
1 Replies

8. Shell Programming and Scripting

Search and replace

I have a script which has several occurences of statement like command: $UCMDPATH/xyz abc I would like to replace the entire line starting from $UCMDPATH with the ls -ltr command eg: Prior to change:: command: $UCMDPATH/xyz abc After change: command: ls -ltr Is there a way to do... (12 Replies)
Discussion started by: jobbyjoseph
12 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. UNIX for Dummies Questions & Answers

search and replace

I have a I need to replace all instances of the word mysite.com to maysite.net within the public_html web folder. How can I accomplish this. (18 Replies)
Discussion started by: dannyd
18 Replies

Featured Tech Videos