0

well i ask here because i have around 12 hours trying with examples on internet for put a replace with sed on a custom script.

In the file a had a lots of lines but i want to replace values after "=" are the follow

[Paths]
config=
data=
mupen64plus=
plugins=
roms=

[Plugins]
audio=
input=
rsp=
video=

Values i need to put in results after replace : Well i need the result of variable "$PWD" not literally "$PWD"

[Paths]
config="$PWD/Datos/config"
data="$PWD/Datos/config"
mupen64plus="$PWD/$EJECUTABLE"
plugins=./.libs/64Bits/plugin
roms="$PWD/Roms"

[Plugins]
audio=mupen64plus-audio-sdl
input=mupen64plus-input-sdl
rsp=mupen64plus-rsp-cxd4-sse2
video=mupen64plus-video-glide64mk2

Because for too much hours i try things like this :

sed -i "s|mupen64plus=.*\"|mupen64plus="$PWD/"$EJECUTABLE\"|g" "file.conf"

This not make something on file

another varations like :

sed "s#^(mupen64plus=).*#\l ${"\/`pwd`"//#/\\#}#" "file.conf"

Show "bad substitution" or something like that

Another things i try and don't work :

sed 's:#mupen64plus=:'`pwd`':' "file.conf"

This :

sed -E "s/(data=).*/\1\.\/Datos\/\config/" "./file.conf"

Make this replacement :

data=./Datos/nfig

sed -E "s/(data=).*/\1\.\/Datos\/\c\/o\/nfig/" "./Datos/config/mupen64plus-qt.conf"
data=./Datos/oo/nfig

sed -E "s/(data=).*/\1\.\/Datos\\co\nfig/" "./Datos/config/mupen64plus-qt.conf"
data=./Datos/

well or is much easer use another thing like awk, someone can help me with this, please ?

2 Answers 2

2

With your skeletton config file

VAR1=/home/archemar
VAR2=some/lib
sed -e "/^mupen64plus/s:=.*$:=${VAR1}/${VAR2}:" \
    -e "/^config/s:=.*:=${VAR1}/DataOs/config:" config

where

  • first two lines initialize variables
  • -e in sed introduce edit command
  • /^mupen64plus/ apply to lines beggining with (^) pattern (here fixed string mupen64plus )
  • s: : : substitue, use : as separator instead of / so slashes don't need to be escaped
  • =.* replace =, any char (.), any number (*) till end of line (a $ is superfluous as * will match as many as possible)
  • =${VAR1}/${VAR2} by = and value of ${VAR1}/${VAR2} (actual expansion is done by calling shell (likely bash), that's why we need double quotes instead of single quotes)

result is

[Paths]
config=/home/archemar/DataOs/config
data=
mupen64plus=/home/archemar/some/lib
plugins=
roms=

[Plugins]
audio=
input=
rsp=
video=

I would advise against inplace edition (-i), or use a backup file

sed -i .bak "/^mupen64plus/s:=.*$:=${VAR1}/${VAR2}:
    /^config/s:=.*:=${VAR1}/DataOs/config:" config

another way is a here document

cat <<END-OF-FILE > config
[Paths]
config="$PWD/Datos/config"
data="$PWD/Datos/config"
mupen64plus="$PWD/$EJECUTABLE"
plugins=./.libs/64Bits/plugin
roms="$PWD/Roms"

[Plugins]
audio=mupen64plus-audio-sdl
input=mupen64plus-input-sdl
rsp=mupen64plus-rsp-cxd4-sse2
video=mupen64plus-video-glide64mk2
END-OF-FILE

which will write a config file with all variable expanded

1

After many hours searching I found this (whilst looking for AWK usage examples), Using sed to replace Windows path with numbers.

In a comment I saw the following:

sed -i 's^<customTag>C:\\path\\to\\'${OLD_VER}'</customTag>^<customTag>C:\\path\\to\\'${NEW_VER}'</customTag>^g' test.txt

I solved with these commands:

sed -i "s/[.].*//" "./Datos/config/mupen64plus-qt.conf"
sed -i "s/[:/].*//" "./Datos/config/mupen64plus-qt.conf"

sed -i 's^mupen64plus='${VIEJO}'^mupen64plus='${PWD}'/'${EJECUTABLE}'^g' ./Datos/config/mupen64plus-qt.conf #Especifica el binario ejecutable que se utilizara
sed -i 's^config='${VIEJO}'^config='${PWD}'/'Datos/config'^g' ./Datos/config/mupen64plus-qt.conf
sed -i 's^data='${VIEJO}'^data='${PWD}'/'Datos/config'^g' ./Datos/config/mupen64plus-qt.conf
sed -i 's^plugins='${VIEJO}'^plugins='${PWD}'/'.libs/64Bits/plugin'^g' ./Datos/config/mupen64plus-qt.conf
sed -i 's^roms='${VIEJO}'^roms='${PWD}'/'roms'^g' ./Datos/config/mupen64plus-qt.conf

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .