Modify the content of variable using sed (or something similar)

10,652

Solution 1

You can do pattern substitution directly in bash:

P_REP=${P/./_}
Q_REP=${Q/./_}

From the bash(1) man page:

Paramter Expansion

${parameter/pattern/string}

Pattern substitution. The pattern is expanded to produce a pattern just as in pathname expansion. Parameter is expanded and the longest match of pattern against its value is replaced with string. If pattern begins with /, all matches of pattern are replaced with string. Normally only the first match is replaced. If pattern begins with #, it must match at the beginning of the expanded value of parameter. If pattern begins with %, it must match at the end of the expanded value of parameter. If string is null, matches of pattern are deleted and the / following pattern may be omitted. If parameter is @ or *, the substitution operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with @ or *, the substitution operation is applied to each member of the array in turn, and the expansion is the resultant list.

Solution 2

John Kugelman's answer is fine for your example, but if you need to process the content of a variable with the actual sed program (or some other arbitrary command), you can do it like this:

P_REP=$(sed 's/\./_/' <<< "$P")
Share:
10,652
jgyou
Author by

jgyou

I am an Assistant Professor of Mathematics and Statistics at The University of Vermont, VT, USA. I also am a faculty of the Translational Global Infectious diseases Research Center and the Vermont Complex Systems Center. My research is at the intersection of statistical inference, epidemiology, and complex systems.

Updated on July 27, 2022

Comments

  • jgyou
    jgyou over 1 year

    I wrote a BASH file that features multiple embedded loops of the form

    for P in {'0.10','0.20', [...] '0.90','1.00'}; do 
       for Q in {'0.10','0.20', [...] ,'0.90','1.00'}; do
        [...] 
    

    I use these variables both as parameters for a command line application, and to create file names directly in BASH. I would like to create duplicates, say $P_REP=0_10 that replaces the dot by an underscore without writting a explicit switch statement for every case, or some hardcoded equivalent. The (non-elegant way) I found to go about it is to

    1. dump the content of P,Q to a temporary file.
    2. replace the dot by an underscore using sed 's/./_/ -i.
    3. read the file again and load its content to the new variable.

    Hence, I was wondering if it is possible to run a sed like command directly on the content of a variable?

  • jgyou
    jgyou over 10 years
    Oh wow thank you. I'm not a bash expert and I did not know that there was such a thing as a man page for bash :-/. I will look there first next time!
  • John Kugelman
    John Kugelman over 10 years
    @JGab Check out {parameter#word} and ${parameter%word} as well, both under the same "Parameter Expansion" section of the man page. Bash can do a lot of cool things, but man oh man, its man page is dense.
  • OrangeDog
    OrangeDog almost 7 years
    This is the answer if you require captures in the substitution.
  • mgouin
    mgouin about 2 years
    That is awesome. I've been doing echo + pipe for nothing :)