use some part of variable value to replace ../../ values in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting use some part of variable value to replace ../../ values in a file
# 1  
Old 05-24-2012
use some part of variable value to replace ../../ values in a file

hi,

i have variable value as follows
Code:
val="/dir1/dir2/dir3/dir4/dir5/dir6/file1"

it is pointing to some file location.

and i have another file as
Code:
../../dir4/file3
../../dir4/dir5/file4
../../dir7/file5


i want the output as
Code:
/dir1/dir2/dir3/dir4/file3
/dir1/dir2/dir3/dir4/dir5/file4
/dir1/dir2/dir3/dir7/file5

please anybody help me.
# 2  
Old 05-24-2012
If I got your purpose right, you may try this:

Code:
sed "s|^\.\./\.\.|$(echo "$val"|cut -c 1-15)|g" <filename>

What would be your required output if one of the lines in the file was

Code:
../../dir5/file5

????

Code:
/dir1/dir2/dir3/dir5/file5

OR

Code:
/dir1/dir2/dir3/dir4/dir5/file5

?

Last edited by elixir_sinari; 05-24-2012 at 06:29 AM..
# 3  
Old 05-24-2012
for example i wrote the directories names as dir1, dir2 and so on.

those directories names are different.

i need the script for different directory names but no.of directories to be replaced is 3 as

if variable is val="/dir1/dir2/dir3/dir4/dir5/dir6/file1" then i have to replace ../../ with /dir1/dir2/dir3/

if variable is val="/home/gopu/india/dir4/dir5/dir6/file1" then i have to replace ../../ with /home/gopu/india/
# 4  
Old 05-24-2012
Then try

Code:
sed "s|^\.\./\.\.|$(echo "$val"|cut -d/ -f1-4)|g" <filename>

# 5  
Old 05-24-2012
it's working fine . thank you elixir_sinari
# 6  
Old 05-24-2012
I always worry about marking relative references in scripts. If something doesn't exist, a cd will just refuse and unless you carefully manage errors, then you could end up damaging important files. Consider:-
Code:
cd /etc
grep -v bankhost hosts > dr_test_hosts
cd /disaster
rm *
cp -pR /etc/* .

This would (probably in a bad way anyway) remove a sensitive remote host address from /etc/hosts in preparation for a DR test to reduce the risk of actually sending data to the bank. Of course, if you run this and /disaster does not exist then you empty /etc, which could be a little awkward, to say the least.

Could it be a better plan to have a variable made of several others, such as:
Code:
maindir=/dir1/dir2
val=${maindir}/dir3/dir4/file1
...do something...
val=${maindir}/dir3/newdir4/file2
...do something...
val=${maindir}/dir3/bigdir4/file3
...do something...

You would still have to check that these exist, of course, but it reduces the risk of a typing error and one action compounding on another.

Maybe I'm paranoid, but unless you consider every possible action and error, then it's a dangerous game. I've been bitten in the past, so I can't claim perfection, just learning from experience.


I hope that this helps,
Robin
Liverpool/Blackburn
UK
# 7  
Old 05-24-2012
one small thing elixir_sinari

if in the file there are morethan ../../ values like ../../../ or ../../../../

then how to modify the sed command. i tried as follows but it is not working

Code:
sed "s|^(\.\./)* | $(echo "$val"|cut -d/ -f1-4)|g" filename

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace values on file

Gents, Please i need your help. Using the file2.txt i will like to replace values in file3.txt. Example in file 2 column 1 is the value to find in file3.txt and replace with value in colunm2 (file2.txt). Example file2.txt 21 1209 22 1210file3.txt SCI TB Timestamp Local : 8/30/17... (2 Replies)
Discussion started by: jiam912
2 Replies

2. Shell Programming and Scripting

Extract a part of variable/line content in a file

I have a variable and assigned the following values ***XYZ_201519_20150929140642_20150929140644_211_0_0_211 I need to read this variable from backward and stop read when I get first underscore (_) In this scenario I should get 211 Thanks Kris (3 Replies)
Discussion started by: mkris
3 Replies

3. Shell Programming and Scripting

Replace values using other file

Gents, Please can you help me. I need to update file1 using file2 values file1 S 44519.00 49349.00 1V1 0.0 0 0.0 0.0 0.0 0.0289091513 S 44513.00 48581.00 1V1 0.0 0 0.0 0.0 0.0 0.0289094319 S 44511.00 48605.00 1V1 0.0 0 0.0... (1 Reply)
Discussion started by: jiam912
1 Replies

4. Shell Programming and Scripting

Shell script - Replace just part of a single line in a file.....

Hey guy's.... I new here, But im working on a school project, and I am not really good at programming. In fact, this is the only programming class that I need because programming is not what I am majoring in. But I have everything done in this shell script except for this last part..... ... (9 Replies)
Discussion started by: hxdrummerxc
9 Replies

5. Programming

PASSING PART OF FILE CONTENT TO VARIABLE

All, I have a log file containing lots of data now i want to extract all text between block below(names) without the title or end pattern but only names, ++++START++++ SCOTT TIGER HENRY PAUL JARED OTIENO OMOLLO JA NIGERIA ++++END++++ the names i want to return and store in a variable in... (1 Reply)
Discussion started by: Scott2000
1 Replies

6. Red Hat

How to pass value of pwd as variable in SED to replace variable in a script file

Hi all, Hereby wish to have your advise for below: Main concept is I intend to get current directory of my script file. This script file will be copied to /etc/init.d. A string in this copy will be replaced with current directory value. Below is original script file: ... (6 Replies)
Discussion started by: cielle
6 Replies

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

8. Shell Programming and Scripting

Variable of Content From Part of Other File

I may not being doing this description justice, but I'll give it a try. I created a mailx script; there will be several messages using the same script where the only difference is the content. So I figured I'd make the content of the message a variable retrieved from a separate file. I have five... (5 Replies)
Discussion started by: royarellano
5 Replies

9. Shell Programming and Scripting

Reading variable from file variable values

Hi, Here is the output of lpstat. I would like to read value of Queue which is(abxxxxb1)and status that is DOWN in first line. i dont care what is in second line. any one can help me.thanks Queue Dev Status Job Files User PP % Blks Cp Rnk ------- ----- ---------... (5 Replies)
Discussion started by: sagii
5 Replies

10. UNIX for Dummies Questions & Answers

replace part of single string in a file

hi! i have a file consisting of the following lines: (BTW, = space) . . . 12ME_T1mapping_flip30bshortf 13DCE_whole_brainbshortf 13DCE_3Dbshortf . . . the list of scans starts at 1 and goes on sometimes up to 60 scans. i would like to change only the lines that contain 'whole' to... (2 Replies)
Discussion started by: nixjennings
2 Replies
Login or Register to Ask a Question