Replacing exact directory variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing exact directory variable
# 1  
Old 09-27-2012
Replacing exact directory variable

I am making a script to relocate a project file. I have all of the variables in place and everything is working except:

Quote:
sed -i.bak -e "s/$NFP/${NFPN}/" $ARCHIVEPATH/Script/${FILE}_renamed.nk
the first variable changes everytime it passes thru a loop. The second is a constant value.


when I run that it does what I want...but incorrectly. It is finding results that contain the current string....but sometimes the result is incorrect.

If the current string is

this/is/a/directory/here

and I need it to be

that/isnt/the/path/here

and it comes across

this/is/a/directory/and/more

it will change

this/is/a/directory/and/more

to

that/isnt/the/path/and/more

I don't want that. I only want it to change it if it matches 100%

I have tried:

Quote:
sed -i.bak -e s/$NFP$/${NFPN}/ $ARCHIVEPATH/Script/${FILE}_renamed.nk
it doesn't change anything

Quote:
sed -i.bak -e 's/'$NFP'/'${NFPN}''/' $ARCHIVEPATH/Script/${FILE}_renamed.nk
and
Quote:
sed -i.bak -e 's/'$NFP'/'${NFPN}''/g' $ARCHIVEPATH/Script/${FILE}_renamed.nk
with the same result.

I can not find a single function that will work!

my variables are all written out as

\/this\/is\/a\/directory btw, so that isn't the problem.

If anyone knows another solution to this I would REALLY appreciate it! I am not stuck on sed. It's just the only way I know to do this.

---------- Post updated at 07:35 PM ---------- Previous update was at 06:53 PM ----------

I seem to be having the same issues with perl

---------- Post updated at 07:37 PM ---------- Previous update was at 07:35 PM ----------

perl seems to be a lot faster tho
# 2  
Old 09-27-2012
Can you show more of your input? If you want to match more specifically you have to be more specific about the context.

I'm surprised sed is working at all for you and not throwing nonstop syntax errors. You are doing sed "s/$PATH1/$PATH2/" which amounts to sed "s/a/b/c//d/e/f/g/h/" Look a all those slashes! sed will think the expression ends at s/a/b/. Not sure what it's doing with the rest.

You can use something else as the separator instead which will make it much easier to work with sed. sed "s#${PATH1}#${PATH2}" for instance,

Running sed 9,000 times on the same file is incredibly wasteful. There's got to be a faster way.

Please show your input file and the output you'd want from it...
# 3  
Old 09-28-2012
$NFP is something like this

Quote:
\/Volumes\/SHOWS\/CURRENT_SHOW\/01_Shots\/CURRENT_SHOT\/CURRENT_FILE
and changes with each directory

and $NFPN is a constant

Quote:
\/Volumes\/ARCHIVE\/CURRENT_SHOW\/01_Shots\/CURRENT_SHOT\/Full_Archive\/Footage
but it is important that the exact match is used or it changes things ahead of time.

I've tried using

Quote:
sed -i.bak -e "s:$NFP:$NFPN:"
but it is still getting the same result

I've done this exact operation comparing two arrays in the same file and it works perfectly....but for some reason this is giving me trouble....should the same syntax!!

---------- Post updated at 08:29 PM ---------- Previous update was at 08:15 PM ----------

Quote:
Running sed 9,000 times on the same file is incredibly wasteful. There's got to be a faster way.
as I said, I am open to suggestion...if someone knows a better way I am all for it!

---------- Post updated at 09:23 PM ---------- Previous update was at 08:29 PM ----------

ok so heres an idea...is there a way to use a variable as a line number for sed?

example:

Quote:
LN=50
sed '$LN s/search/replace/'
that would basically solve my problem

---------- Post updated at 09:32 PM ---------- Previous update was at 09:23 PM ----------

GOT IT!!!!

Quote:
sed -i.bak "${LINES[$i]} s/$NFP/$NFPN/" $ARCHIVEPATH/Script/${FILE}_renamed.nk
I extracted the line numbers I needed to replace and used it as the line to replace...so it only does the operation one line at a time!

---------- Post updated 09-28-12 at 10:04 AM ---------- Previous update was 09-27-12 at 09:32 PM ----------

Quote:
Originally Posted by Corona688
Can you show more of your input? If you want to match more specifically you have to be more specific about the context.

I'm surprised sed is working at all for you and not throwing nonstop syntax errors. You are doing sed "s/$PATH1/$PATH2/" which amounts to sed "s/a/b/c//d/e/f/g/h/" Look a all those slashes! sed will think the expression ends at s/a/b/. Not sure what it's doing with the rest.

You can use something else as the separator instead which will make it much easier to work with sed. sed "s#${PATH1}#${PATH2}" for instance,

Running sed 9,000 times on the same file is incredibly wasteful. There's got to be a faster way.

Please show your input file and the output you'd want from it...
btw, as I said earlier, the variable is not

Quote:
a/b/c
resulting in

Quote:
sed 's/a/b/c/x/y/z/'
it is

Quote:
\/a\/b\/c
resulting in

Quote:
sed 's/\/a\/b\/c/\/x\/y\/z/'
which is correct.

The issue was partial matches. But calling out the line number to be addressed only works exactly as I want it to!

Last edited by gentlefury; 09-27-2012 at 10:21 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacing exact match

Hi All, My Input file contains a 1000’s of lines in which I have to replace a a string to the other. Here the problem is, I have the lines in my Input as below. Cable Yes && !Pay TV && !ADS \noUE \Label="Cable Yes && !Pay TV && !ADS" I want to replace exactly the string Cable Yes &&... (37 Replies)
Discussion started by: am24
37 Replies

2. UNIX for Dummies Questions & Answers

How to grep exact string with quotes and variable?

As the title says I'm running a korn script in attempts to find an exact match in named.conf finddomain.ksh #!/bin/ksh # echo "********** named.conf ************" file=/var/named/named.conf for domain in `cat $1` do grep -n '"\$domain "' $file done echo "********** thezah.inc... (1 Reply)
Discussion started by: djzah
1 Replies

3. Shell Programming and Scripting

echo exact xml tag from an exact file

Im stumped on this one. Id like to echo into a .txt file all names for an xml feed in a huge folder. Can that be done?? Id need to echo <name>This name</name> in client.xml files. $path="/mnt/windows/path" echo 'recording names' cd "$path" for names in $path than Im stuck on... (2 Replies)
Discussion started by: graphicsman
2 Replies

4. Shell Programming and Scripting

Match exact Variable in Awk

I want to match exact string stored in Variable INO and print next two lines. I have written below code which partially matches: awk '/\'$VAR'/{getline; getline; print}' Will be thankful , if somebody can help in to resolve this problem. Please view this code tag video for how to use... (9 Replies)
Discussion started by: nitj
9 Replies

5. Shell Programming and Scripting

QUESTION1: grep only exact string. QUESTION2: find and replace only exact value with sed

QUESTION1: How do you grep only an exact string. I am using Solaris10 and do not have any GNU products installed. Contents of car.txt CAR1_KEY0 CAR1_KEY1 CAR2_KEY0 CAR2_KEY1 CAR1_KEY10 CURRENT COMMAND LINE: WHERE VARIABLE CAR_NUMBER=1 AND KEY_NUMBER=1 grep... (1 Reply)
Discussion started by: thibodc
1 Replies

6. Shell Programming and Scripting

Sed find exact string and delete line with variable

All, I am trying to read in a variable and search a file then delete based on that string, but i want to match exact word. This works but it matches all, i don't want to match anthing that contains the string, just the exact string. sed -i "/$feedname/d" file I tried sed... (1 Reply)
Discussion started by: markdjones82
1 Replies

7. Shell Programming and Scripting

Replacing variable is files in same directory

Hi all, I'm writing a script to get values from the user and replace it in another file in the same directory. /usr/bin/sediff 's/$PROJECT/'$PROJECT'/' /ananth/TEMPLATE TEMPLATE is the file I want to replace the variable PROJECT from the script I'm writing but Im not getting it done. Is... (4 Replies)
Discussion started by: Ananthdoss
4 Replies

8. UNIX for Advanced & Expert Users

Find exact path of a file/directory

Hello Folks, A wrapper takes an argument of file or directory name. I want to allow paths that reside within the current directory only. Can simply discard the paths like "/A" & "../" as they go outside the current by looking at the path beginning. How to validate this one: A/../../../b... (4 Replies)
Discussion started by: vibhor_agarwali
4 Replies

9. Shell Programming and Scripting

sed - replacing on the right of a pattern and looking for exact word?

how would you get SED to do the following, say you have the following lines in a text file: user=tigger some text some text some text some text some text some text user=ted some text some text some text some text some text some text user=thekingofrockandroll you want to find any line... (15 Replies)
Discussion started by: rich@ardz
15 Replies

10. Shell Programming and Scripting

How to match a exact word in a variable ????

Hi All, Str="online maintenance" if then perform some action I know the above comparison is wrong ..... Actually I am very new to this solaris.. I want to check for online in a variable and perform some action based on that How can I do it? (6 Replies)
Discussion started by: vijaysachin
6 Replies
Login or Register to Ask a Question