Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have 3 configuration files in my Linux box which contains config information for a custom application. I want to change some values in configuration files. The contents of the files and descriptions are given below:

config1 file content:

set VAR1=/app/client/10x_64/instance

config2 file content:

set VAR2=/app/client/11x/instance

config3 file content:

set VAR3=/app/client/11x_64/instance

I want change all values 10x_64, 11x, 11x_64 to 12x_64 in all the files. Currently I'm using three commands to change the content; commands are given below:

sed -i 's/10x_64/12x_64/g' config1
sed -i 's/11x/12x_64/g' config2
sed -i 's/11x_64/12x_64/g' config3

I want a single generalized command to change the content of all 3 files.

share|improve this question
1  
for file in config1 config2 config3; do sed -i 's/10x_64/12x_64/g;s/11x/12x_64/g;s/11x_64/12x_64/g' $file; done – DopeGhoti Mar 1 at 7:52
1  
For 3 files, cooking up a convoluted command to do the change automatically (and testing it, and fixing it, and handling the inevitable fallout when it fails, and ...) takes much more time and effort to just dig in with $EDITOR. Just sayin'. – vonbrand Mar 1 at 13:40
up vote 5 down vote accepted

If you want a single expression, you can do:

sed -i 's#/client/[^/]*#/client/12x_64#g' config*

I've used /client/[^/]* as the marker to find what we want to replace (ie whatever is after /client/ but before the next /) , but we could have done client/[^/]*/instance instead if that avoids matching other items in the file.

share|improve this answer
    
Thanks...It works.. – Thejesh PR Mar 1 at 9:55
    
Using /client/[^/]*/ might be a bit safer, since it prevents the variable part of the regexp from gobbling up more than one path element even if the config file contains something like /app/client/10x_64/foo/instance/bar. – Ilmari Karonen Mar 1 at 10:28
    
@IlmariKaronen Yes, you're right. That is better (more reliable). I've updated my answer. – seumasmac Mar 1 at 10:50

You can just simply combine them like this:

sed -i 's/10x_64/12x_64/g;s/11x/12x_64/g;s/11x_64/12x_64/g' config*

share|improve this answer

How about preparing a "template configuration file", in which you replace the needed configuration parts for a particular version? Let's say you have the following template

#/bin/bash
echo "set ##V1##=/app/client/##ARCH1##/instance" > cfg-file1
echo "set ##V2##=/app/client/##ARCH2##/instance" > cfg-file1
echo "set ##V3##=/app/client/##ARCH3##/instance" > cfg-file1

Then you could do replace-commands by version depending on a commandline-parameter:

#!/bin/bash
[ $# -lt 1 ] && (echo "usage: replace <version>"; exit 1;)
if [ "$1" == "1" ]; then
        cat  template.txt | sed -e "s/##V1##/VAR1/g;s/##ARCH1##/arch_xy_/g;s/##V2##/VAR2/g;s/##ARCH2##/arch2_xy_/g;"
        # etc...
fi

This may be easily extended.

share|improve this answer

You can combine all three commands into a single expression. What you want to do is replace all cases of 1 followed by either a 0 or a 1, then an x and then an optional _64. This can be expressed by the following extended regular expression:

1[01]x(_64)*

The [ ] denote a character class, so [01] matches either a one or a zero. The (foo)* construct will match 0 or more instances of foo. So, (_64)* will match both the string _64 (and its repetitions like _64_64_64) and the empty string. This lets you match both 11_x64 and 11x and when it is not.

So, if we combine that into a sed expression, we get:

sed -iE 's/1[01]x(_64)*/12_64/' config*
share|improve this answer
    
Above command will work...but my config file architecture values(10x_64, 11x,11x_64) will varies.. – Thejesh PR Mar 1 at 9:57
    
@ThejeshPR that's the kind of thing you really need to mention in your question. How can they vary? What remains constant? – terdon Mar 1 at 9:58
    
Yeah remaining things will be constant...actually the configuration files contains some application related path so it will vary according to application version and architecture – Thejesh PR Mar 1 at 10:02
    
@ThejeshPR what remains constant? Will it always be 1Nx for example? Can you also have Nx with no 1? Will there always be two numbers before the x or can there be fewer? Will there always be an x? – terdon Mar 1 at 10:04
    
It depends on application version. it will vary from 6 to 12. for ex: 6x_32, 7x_64,10x_64,12x...like this – Thejesh PR Mar 2 at 6:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.