sed back reference error


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed back reference error
# 1  
Old 02-26-2015
Hammer & Screwdriver sed back reference error

I am trying to change a single line of a special file whose comment character is ! to show a path to the file in the comment. such as:

Code:
!!HFSS and mcm path:     \Signal_Integrity\Package_SI\Section_Models\C4toTrace\28nm\D6HS\SLC_5-2-5\GZ41_ICZ\NSSS\

to a different path and replace the !!HFSS and mcm path: text.

here is my code:
Code:
path='!! HFSS and mcm path: '$(pwd)

for file in $(find . -name "*s[48]p*")
do
  old=$(grep '!! HFSS\|!!HFSS' $file)
  sed -i "s@$old@$path@" $file
done

I get the following error for some but not all files!
Code:
sed: -e expression #1, char 260: Invalid back reference

can someone please help explain why it works for some files but not others?
Thanks,
Mike

Last edited by Scrutinizer; 02-26-2015 at 11:34 AM.. Reason: code tags, please!
# 2  
Old 02-26-2015
My guess is that your strings contains backslashes followed by numbers, which gets interpreted as a back reference. Note that sed substitute functions does not use strings, but rather regular expressions.

A safer way would be to use fixed strings, for example

Code:
path='!! HFSS and mcm path: '$(pwd)
awk -v p="$path" '/!! *HFS/{$0=p}1' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed - use back reference in 2nd command

I have data that looks like this: <Country code="US"><tag>adsf</tag><tag>bdfs</tag></Country><Country code="CA"><tag>asdf</tag><tag>bsdf</tag></Country> I want to grab the country code save it, then drop each new "<..." onto a new line with the country code added to the beginning of each So,... (9 Replies)
Discussion started by: JenniferAmon
9 Replies

2. UNIX for Dummies Questions & Answers

Extract text in sed using back reference

i have a text 20 21 22 23 24 25 26 i want to get 22 using sed back reference. I have used sed 's/{6}\(..\).*/\1/' but, it does not work. I am missing something somewhere. Please help. (5 Replies)
Discussion started by: gotamp
5 Replies

3. Shell Programming and Scripting

Pattern match and replace indirect directory reference using sed

Hi, I need a ksh script to replace indirect directory references in an .ini file with a env variable using sed or awk. The .ini file is for example as such: A=.. B=../ C=../.. D=../../ E=../bin F=../../bin G=../../bin/xml H=../../bin/xml/ Need to replace an instance of .. or... (2 Replies)
Discussion started by: andyatit
2 Replies

4. UNIX for Dummies Questions & Answers

Invalid back reference

The thread can be closed now :D. (3 Replies)
Discussion started by: vaz0r
3 Replies

5. Shell Programming and Scripting

sed error: invalid reference

Hello all, I am using sed to parse a particular part of a string and am having problems. I am getting the following error: sed: -e expression #1, char 28: invalid reference \1 on `s' command's RHS Here is the code I am using: echo "Alarm SET:" echo "" echo "Date: " $DATE echo... (4 Replies)
Discussion started by: dlundwall
4 Replies

6. Shell Programming and Scripting

Shell Scripting Problem - Invalid Back Reference

Here is the question... Create a new script, sub2, taking three parameters... 1.) the string to be replaced 2.) the string with which to replace it 3.) the name of the file in which to make the substitution ...that treats the string to be replaced as plain text instead of as a regular... (1 Reply)
Discussion started by: johnhisenburg
1 Replies

7. Shell Programming and Scripting

Back Referencing in SED

Hi, I have tried all examples of back referencing from the web but all in vain. It would be heavily helpful if someone explains me the use of back referencing and sub expression using an example of substitution. Thanks (1 Reply)
Discussion started by: sinpeak
1 Replies

8. Shell Programming and Scripting

Perl: Getting back reference from s modifier

My input text has the following pattens: func_a(3, 4, 5); I want to replace it with this: func_b(3, 4, 5, 6); I'm trying the following expression, but it does not work: perl -p -e "s/func_a\((.*)?\);/func_b(\1,\n6)/s" <... (8 Replies)
Discussion started by: cooldude
8 Replies

9. Shell Programming and Scripting

How to reference a variable within sed?

Hi all, How can I use sed to perform a substitution if the string that I'm going to substitute is stored in a variable: Let's say: sed 's/abcdefg/good' VS tmp="abcdefg" sed 's/$tmp/good' The second case doesn't work. Guess it's due to the single quotes on the outside. How can I... (1 Reply)
Discussion started by: rockysfr
1 Replies

10. Shell Programming and Scripting

back reference error

Hi, i am getting this error........ find ./ | sed '/\(*\) \(*\)/\2\1/' Unrecognized command: /\(*\) \(*\)/\2\1/ Any idea??? regards Apoorva Kumar (4 Replies)
Discussion started by: apoorvasharma80
4 Replies
Login or Register to Ask a Question