How do i use vi to substitute an expression that is part of a path?


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users How do i use vi to substitute an expression that is part of a path?
# 1  
Old 06-22-2010
How do i use vi to substitute an expression that is part of a path?

Let say I have /home/user1/bin/ and i want it to be /root/bin
I tried :
Code:
s/\/home\/user1/user2/

but it can't find the pattern user1.
I tried:
Code:
s/*\/home\/user1*/user2/

as well same result

many thanks

Last edited by Yogesh Sawant; 06-23-2010 at 07:10 AM.. Reason: added code tags
# 2  
Old 06-23-2010
Code:
$
$ X="/home/user1/bin/"
$
$ echo $X
/home/user1/bin/
$
$ echo $X | sed -e 's/\/home\/user1/\/root/' -e 's/\/$//'
/root/bin
$
$

tyler_durden

---------- Post updated 06-23-10 at 02:07 PM ---------- Previous update was 06-22-10 at 03:51 PM ----------

Quote:
Originally Posted by c00lsnoopy
Let say I have /home/user1/bin/ and i want it to be /root/bin
I tried :
Code:
s/\/home\/user1/user2/

but it can't find the pattern user1.
I tried:
Code:
s/*\/home\/user1*/user2/

as well same result
...
Sorry, I did not notice that you want to do this in vi editor.

Place your cursor on the line that has the string "/home/user1/bin/" and hit ":" for the editor prompt. Then type the following and press "Enter" -

Code:
s/\/home\/user1\/\(.*\)\//\/root\/\1/

I don't have vi, but this works in vim.

HTH,
tyler_durden
This User Gave Thanks to durden_tyler For This Post:
# 3  
Old 06-23-2010
put cursor on the line you want to change and type


Code:
:%s=/home/user1/bin/=/root/bin

## if you want to globally change
:%s=/home/user1/bin/=/root/bin=g

This User Gave Thanks to denn For This Post:
# 4  
Old 06-24-2010
when you use / as delimiter then you have to properly escape the / that occurs in your pattern, or else use some other delimiter other than /.


Moderator's Comments:
Mod Comment Removed Self Promoting Links
This User Gave Thanks to thegeek For This Post:
# 5  
Old 06-24-2010
Try this

in the vi mode, type : then the below line. If you have multiple lines with usr/home which needs to be changed, use g at the end else just skip it.
this should work fine.

<:%s/\/usr\/home/\/root/g>
This User Gave Thanks to raghu_shekar For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

A way to print only part of directory path

Hi, So I struggled to find a solution to the following problem: I want to make sed print only part of multiple different paths. So lets say we have /path/path1/path2/logs/bla/blabla /path/path1/path2/path3/logs/yadda/yadda/yadda Can someone suggest a way to make sed or other... (5 Replies)
Discussion started by: dampio
5 Replies

2. Shell Programming and Scripting

String variable as part of expression in find command

Hi, I am new in scripting, and I am currently working on a script that will look for other files in a certain directory and exclude some file type. this works fine:Find_File2Exclude=`find ${paths} -maxdepth 1 -type f \( ! -iname '*.out' ! -iname '*.auc' ! -iname '*.cps' ! -iname '*.log' ! -iname... (4 Replies)
Discussion started by: kedd05
4 Replies

3. Shell Programming and Scripting

Grep regular expression to get part of a line

Hi I just started on GNU Grep with regex and am finding it very challenging and need to ask for help already... here is the problem, I have a page (MYFILE) which consists of the following.... <div> <input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" /> <input type="hidden"... (2 Replies)
Discussion started by: noobie74645
2 Replies

4. Shell Programming and Scripting

Perl command to replace path part of variable

I'm trying to replace path which is part of variable inside script file: FROM: ABC_HOME=$ABC_ROOT/abc/1.0 TO: ABC_HOME=$ABC_ROOT/abc/1.5 I'm using this: perl -pi -e 's\ABC_HOME=$ABC_ROOT/abc/1.0\ABC_HOME=$ABC_ROOT/abc/1.5\g' /apps/scripts/test.sh This command is not working because... (2 Replies)
Discussion started by: djanu
2 Replies

5. Shell Programming and Scripting

How to substitute a line that matches an expression with another line

I need some help. I have a file (all.txt) whereby I want to substitute using sed/awk all lines that matches an expression with another line with different expression i.e subtitute expression, database_id: filename; WITH database_id: PY; There are many occurrences of the expression... (4 Replies)
Discussion started by: aimsoft
4 Replies

6. Shell Programming and Scripting

AWK script issue for the part regular expression

Hi I am having a file as shown below FILE 1 TXDD00, TXDD01, TXDD02, TXDD03, TXDD04, TXDD05, TXDD06, TXDD07, TXDD08, TXDD09, TXDD10, TXDD11, TXDD12, TXDD13, TXDD14, TXDD15, TXDD16, TXDD17, TXDD18, TXDD19, TXDDCLK, TXDJTAGAMPL0, TXDJTAGAMPL1,... (3 Replies)
Discussion started by: jaita
3 Replies

7. Shell Programming and Scripting

taking a part of string from a given path

Hi I have two path as follows system/console/bin/code/sample/testfile.txt system/console/bin/database/files/new/dbfile.txt I need the output as code/sample in first case database/files/new in second case That is I am omitting system/console/bin and the filename(s) in both... (2 Replies)
Discussion started by: ssuresh1999
2 Replies

8. Shell Programming and Scripting

regex to select last part of a path

Hi all, I am learning the use of regular expression and I would like to know which regex can be used to select only the last part of a directory path name. Something like: /dir1/dir2/dir2 and I want to select the last /dir2 where dir2 can be any kind of string. Thanks a lot for your help.... (7 Replies)
Discussion started by: elric
7 Replies

9. Shell Programming and Scripting

retrieve part of file path

Hi I am trying to use sed to retrieve part of my html file's path. I am having a hard time getting what I want. Could someone give me some help? I want to retrieve the section after html and before the file name For example if I have the following, ... (3 Replies)
Discussion started by: tiger66
3 Replies

10. Shell Programming and Scripting

getting the path part of an argument

Seems I'm inundating this forum with questions, but anyway: I am writing a script that should accept one and only one argument when called. That argument should designate a file, either with path/filename or just filename. Now to the difficult bit: I want to figure out a way to store... (9 Replies)
Discussion started by: ropers
9 Replies
Login or Register to Ask a Question