Sed question to substitute data in \2


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed question to substitute data in \2
# 1  
Sed question to substitute data in \2

Hi All,
Here is what I'm trying to do with sed:

Input File:
Code:
somechangeVariable1=Something I would like to change
somechangeVariable2=Something else I would like to change
...

Output File:
Code:
somechangeVariable1=Something I would like to different
somechangeVariable2=Something else I would like to different
...

So I tried this unsuccessfully in sed:
Code:
/\(.*\)=\(.*\)/{
	/\2/ s/change/different/
}

Error:
Code:
line 2: Invalid back reference

Question:
How do I run the substitute on \2 alone?
I realize that I can use other tools but would like to solve this with sed as more of a theoretical exercise at this point.

Thanks in advance for any help provided.
Peace!

Last edited by Scott; 05-27-2010 at 06:24 PM.. Reason: Code tags, please...
# 2  
Quote:
Originally Posted by Peace_Dude1
...Here is what I'm trying to do with sed:

Input File:
somechangeVariable1=Something I would like to change
somechangeVariable2=Something else I would like to change
...

Output File:
somechangeVariable1=Something I would like to different
somechangeVariable2=Something else I would like to different
...
...
How do I run the substitute on \2 alone?
I realize that I can use other tools but would like to solve this with sed as more of a theoretical exercise at this point.
...

Code:
$
$ cat f2
somechangeVariable1=Something I would like to change
somechangeVariable2=Something else I would like to change
$
$ sed 's/^\(.*=.*\) \(.*\)$/\1 different/' f2
somechangeVariable1=Something I would like to different
somechangeVariable2=Something else I would like to different
$
$

tyler_durden
# 3  
Clever but not quite what I wanted;
Here is the rephrased question:
Hi All,
Here is what I'm trying to do with sed:

Input File:
Code:
somechangeVariable1=Something I would like to change something
somechangeVariable2=Something else I would like to change else
...

Output File:
Code:
somechangeVariable1=Something I would like to different something
somechangeVariable2=Something else I would like to different else
...

So I tried this unsuccessfully in sed:
Code:
/\(.*\)=\(.*\)/{
	/\2/ s/change/different/
}

Error:
Code:
line 2: Invalid back reference

Question:
How do I run the substitute on \2 alone?
I realize that I can use other tools but would like to solve this with sed as more of a theoretical exercise at this point.

Thanks in advance for any help provided.
Peace!

Moderator's Comments:
Mod Comment Removed some formatting, added code tags

Last edited by Scott; 05-27-2010 at 06:25 PM.. Reason: Code tags, please...
# 4  
Code:
sed 's/\(.*\)change\(.*\)$/\1different\2/' file
OR
sed 's/\(.*\)=\(.*\)change\(.*\)/\1=\2different\3/' file

# 5  
Code:
sed 'h; s/=.*/=/; x; s/^[^=]*=//; s/change/different/g; H; g; s/\n//'


Sample run:
Code:
$ cat data
somechangeVariable1=Something I would like to change something
somechangeVariable2=Something else I would like to change else
somechangeVariable3=change change change and still more change
leavealone=nothing to see here
$ sed 'h; s/=.*/=/; x; s/^[^=]*=//; s/change/different/g; H; g; s/\n//' data
somechangeVariable1=Something I would like to different something
somechangeVariable2=Something else I would like to different else
somechangeVariable3=different different different and still more different
leavealone=nothing to see here


Explanation:

1. h: Copy the original, pristine, unaltered line to the hold space, for future use.
2. s/=.*/=/: From the current line, remove everything that follows the equals sign inclusive, replacing it with just the equals sign. What remains is the variable name (with the trailing equals sign), in which we are not interested in substituting anything.
3. x: Store the variable name in the hold space by swapping the pattern and hold spaces. The original version of the line is now back in the pattern space.
4. s/^[^=]*=//: Remove all leading characters that are not an equals sign and the first equals sign, leaving nothing but the text of interest to the primary substitution operation.
5. s/change/different/g: Substitute each instance of "change" with "different" in the relevant text.
6. H: Append the resulting string to the variable name which we've kept tucked away in the hold space.
7. g: Copy the hold space to the pattern space.
8. s/\n//: Remove the newline that was inserted between the variable name and variable value by step 6's H command.

Last edited by alister; 05-27-2010 at 08:38 PM..
These 2 Users Gave Thanks to alister For This Post:
# 6  
Thanks Alister. Precisely what I was looking for.
Peace be with you.
# 7  
Quote:
Originally Posted by alister
Code+explanation
Thank you for adding the explanation, It was quite interesting Smilie
Login or Register for Dates, Times and to Reply

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

Test Your Knowledge in Computers #571
Difficulty: Medium
A simple units bug in the Mars Climate Orbiter caused an error which resulted in loses of over $125M USD in 1999.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Substitute a character with sed

hi all, i'd like to modify a file with sed , i want to substuite a char "-" with "/" how can i do this? Thanks for all regards Francesco (16 Replies)
Discussion started by: Francesco_IT
16 Replies

2. Shell Programming and Scripting

[sed]: Substitute a string with a multiline value

Dear all, I try to replace a string of characters in a file (MyFile.txt) by a multiline value of the variable "Myvar": $ cat MyFile.txt DESCRIPTION '@TargetTable SCHEMA' ( @InputFlowDef ); $ The content of Myvar: $ echo "$Myvar" col1 , col2 , col3 $ (4 Replies)
Discussion started by: dae
4 Replies

3. Shell Programming and Scripting

How can I modify my script to include or substitute missing data?

Let me start off by saying I am a self taught sometimes scripter so what you will see below won't be pretty. I have created a script to parse through a file with a large amount of data and simply pull out what I need. In doing this I create several files and then paste them together in order to... (2 Replies)
Discussion started by: fsanchez
2 Replies

4. Shell Programming and Scripting

sed substitute command -- need help

I am trying to do what I thought should be a simple substitution, but I can't get it to work. File: Desire output: I thought I'd start with a sed command to remove the part of the header line preceding the string "comp", then go on to remove the suffix of the target string (e.g. ":3-509(-)"),... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

5. Shell Programming and Scripting

Substitute partial data only within a xml tag

Hello, I have huge xml files and I need to replace only part of the data within a particular xml tag. This doesnt seem to be as simple as it sounds. I have searched everywhere and couldnt find any solution. Ex: In the below case I would like "def" to be replaced by "xyz" only when found in... (8 Replies)
Discussion started by: roshanjain2
8 Replies

6. Shell Programming and Scripting

using awk to substitute data in a column delimited text file

using awk to substitute data in a column delimited text file hello i would like to use awk to do the following calculation from the following snippet. input file C;2390 ;CV BOUILLOTTE 2L 2FACES NERVUREES ;1.00 ;3552612239004;13417 ;25 ;50 ; 12;50000 ; ; ... (3 Replies)
Discussion started by: iindie
3 Replies

7. UNIX for Dummies Questions & Answers

Using sed to substitute between quotes.

I'm using sed to perform a simply search and replace. The typical data is: <fig><image href="Graphics/BAV.gif" align="left" placement="break" I need to replace the value in the first set of quotes, keeping the remainder of the line the same. Thus: <fig><image href="NEW_VALUE" align="left"... (3 Replies)
Discussion started by: Steve_altius
3 Replies

8. Shell Programming and Scripting

Using sed to substitute first occurrence

I am trying to get rid of some ending tags but I run into some problems. Ex. How are you?</EndTag><Begin>It is fine.</Begin><New> Just about I am trying to get rid of the ending tags, starts with </ and ending with >. (which is </EndTag> and </Begin>) I tried the following sed... (2 Replies)
Discussion started by: quixoticking11
2 Replies

9. Shell Programming and Scripting

Using SED to substitute between two patterns.

Hi All, I'm currently using SED to make various changes to some .xml files I'm working on, but I'm stuck on this particular problem. I want to remove '<placeholder>element-name</placeholder>' from the following: <heading>Element <placeholder>element-name</placeholder> not... (2 Replies)
Discussion started by: Steve_altius
2 Replies

10. UNIX for Dummies Questions & Answers

sed substitute situation

I am having a problem executing a sed substitute in a file. I have tried alot of different things I found in previous posts, however non seem to work. I want to substitute this in $FILE: VALUE=33.4 In the script I have tried the following: prev=$(awk -F"=" '{ print $2 }' $FILE ) new=$(echo... (16 Replies)
Discussion started by: newbreed1
16 Replies

Featured Tech Videos