sed (replacing / with \/ using shell) - bash

I'm using a shell script that uses sed
I have this sentence in a variable
kpi="ENFRIADORA CONDENSADA POR AIRE/AIRE-AIRE/AGUA - MENSUAL"
if in a console I put
echo $kpi | sed 's/\//\\\//g'
I see
ENFRIADORA CONDENSADA POR AIRE\/AIRE-AIRE\/AGUA - MENSUAL
That is what I want, but when I put this in a script like this
echo start
echo $kpi2
kpi_2=`echo $kpi2 | sed 's/\//\\\//g'`
echo $kpi_2
echo end
That I see is :
start
ENFRIADORA CONDENSADA POR AIRE/AIRE-AIRE/AGUA - MENSUAL
sed: -e expresion #1, character 9: unknown option to `s'
end
Any suggestion?

$> kpi_2=$( echo $kpi | sed 's/\//\\\//g' )
$> echo $kpi_2
ENFRIADORA CONDENSADA POR AIRE\/AIRE-AIRE\/AGUA - MENSUAL

This works for me:
kpi_2=`echo $kpi2 | sed 's:\/:\\\/:g'`

Related

Shell sed - "unterminated `s' command" in Windows / "unescaped newline inside substitute pattern" in MAC

I am trying to fix an old shell script that used to run on MAC to be runnable on Windows... But I am facing errors in both machines WRT one particular sed operation. The code snippet is :
echo "1 :: ${1}"
echo "2 :: ${2}"
sed -i.bak 's/^export \('${1}'=\)\(.*\)/export \1"'"$(sedscape ${2})"'"/' ~/.bash_profile
rm -f ~/.bash_profile.bak
The function sedscape is :
function sedscape() {
echo " >>> sedscape"
echo "1 :: ${1}"
echo ${1//\//\\/} | sed 's/'\''/'\'\\\\\'\''/'
}
This gives me following error on Windows:
1 :: AWS_ACCESS_KEY
2 ::
sed: -e expression #1, char 59: unterminated `s' command
and on MAC:
1 :: AWS_ACCESS_KEY
2 ::
sed: 1: "s/^export (AWS_ACCESS_ ...": unescaped newline inside substitute pattern
.bash_profile has just one line :
export AWS_ACCESS_KEY="aaaa"
As far as I can understand, it is readin every line from .bash_profile starting with "export" and replacing the value of it with whatever is in "2" ... but it fails even before the "sedscape" half (second half) of the expression is evaluated.
Correct me if the understanding of the sed command is wrong.
PLEASE HELP !!!
The echos in sedscape were creating the errors... simply removing them sloved the issue.
echo " >>> sedscape"
echo "1 :: ${1}"

Why sed failed with shell variables but success with their values?

I want to use sed to replace a particular string in yaml file with base64 encoded tls key:
TLS_KEY=`cat tls.key | base64`
sed -i "s/##TLS_KEY##/${TLS_KEY}/" 02-elastic-tls.yaml
Then I get this error:
sed: -e expression #1, char 90: unterminated `s' command
But if I replace ${TLS_KEY} with its value directly in the above command, it will success.
Execute echo ${TLS_KEY}:
LS0tLS1CRUdJTiBQUklWQVRFIEtFWS0tLS0tCk1JSUV2UUlCQURBTkJna3Foa2lHOXcwQkFRRUZB QVNDQktjd2dnU2pBZ0VBQW9JQkFRRENtUHJVa29ibGpKT3oKOGJtMHZodzNwRHdLa0M2TGJaUjd5 enFWVkpuc2VTQkI4Z0E3aExkYmNBRDJTTWp6SDBQUEpEelE1elJ5dnJBYQpSK3N3QU1BeFhwQVdH Ulp5S2RsTkVTTDhuREZvNGJ2ODB1enQrdWZiSEFZTGl2OVByMHpRRnA2cjh5U3BlTXNkCm5xNnh1 K3ZaMHJ1ZWpxdkxhZEVSMFNra0h1VUp4bExGVjY1bzFFREdzYzhKVlBYaXFFUzdRcXhTTnhtS0Nt VzAKdFdMbWd6YUZ2d2tjcFpHbWJjTmpsMFZraG9ZTjFVZk51M0FURHJ3eUJQUmZIYWJvVGFSVlFm WmE5Q3dpSThPZgpXVzR0ZnA3eW9GcFRhSDlURUdxYldzY1VMa3MvamdlbW15eHZPY1Y0bG04SXgw dHhLT2J1R0ZWVGp2dlBnb0JuCnJLb2V5UVMzQWdNQkFBRUNnZ0VBUjEwV3o1SVV5bVh0WE5aUEJr dVp5WVgvUnRodFdWQ01YWUhBTURkZ1kycGcKUEE3bHF1NFcxLytST3NDaWdSWjhKYURpZExIMWVB Y2FTZWtLQlFhaGRQbDYyYi9GallSdlhtWGtjTU5QRTJtRgpLVWlvL3ZVK3hnblc1YkRKeEkxREhD VFBkN3VwUEVWQ0o5Q2p2Q1p3YmVkc1NzK0RqUXQzVXBOa0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UVczZDVlSlR1NFI3CnY2ejNyYkZoTlR3OGVLcVpTdnhBakxybjMwcmdKTG5z cHMxU2c5c2dmUnV4SkwrWG94VnpKdW9FSTdlVE0wUTkKRTdiVVlIQkxHTDNISWRRaGovNHBIOUla S3o4TFVBNzBXZmhKOWRQUkFvR0FjNG5XYnU4Q0tFRzBPSCtmKzYrSAo1bnNmaDc3UUhiZEJLK2Za ZXQybFhrMjREUUlxUGZCVUR6clpINTVxenZPb2Z5T2tRbzI3cGZBT1gwS1l6dHBECm54cUVWd2kr WHhOaTAvRUxSSTdEanc5KzF4TkQ0N3NZTFhyd2dMZHpWQWtRengrSXM4Y3JGQU9iMi92cGNNYm0K RU4wNzJOV1lXUy9LZllzdXQ0Z2ZONVE9Ci0tLS0tRU5EIFBSSVZBVEUgS0VZLS0tLS0K
so why I got this error?
Note the difference between echo ${TLS_KEY} and echo "${TLS_KEY}".
$ x="123
> 456"
$ echo $x
123 456
$ echo "$x"
123
456
I suspect there are newline characters present in your key which breaks sed.
$ echo abc | sed "s/abc/$x/"
sed: -e expression #1, char 7: unterminated `s' command

how delete white space with Command sed

My problem is that I have a script that creates a file containing data separated by a;
The last colonne contains whites (between ; and the values like text.csv) that I wanted to deleted I applied a lot of the command with sed but does not change anything.
the commands I tried:
sed '/[ /]*$/d' test.csv
sed -e '/^; *$/d'
sed '/^; *$/d'
sed -e 's/[ ]*;=[ ]*/;=/g'
sed -e 's/[ ]*;[ ]*/;/g'
sed -e 's/; *;/;/g' ( déja fonctionner sur un autre fichier mais pas celle que je veux)
sed -e 's/;= *;/;/g'
tr -d ' '
my text.csv is :
=("FRO-GEPSA018");=("34212254600225");01-07-2019/31-07-2019; 1789,32
=("FRO-GNVERT01");=("41985346000584");01-07-2019/31-07-2019; 466,84
=("FRO-ESGED01");=("53983181800064");01-07-2019/31-07-2019; 4788,55
=("FRO-SGE") ;=("54209732400017");01-07-2019/31-07-2019; 1587,78
=("FRO-STO") ;=("54209732400025");01-07-2019/31-07-2019; 798,28
=("FRO-ELOC005");=("55204695500985");01-07-2019/31-07-2019; 4585
=("FRO-ELOC002");=("55204695502627");01-07-2019/31-07-2019; 1997,54
=("FRO-IDFTRT08");=("55204695503542");01-06-2019/30-06-2019; -30,45
=("FRO-IDFTRT08");=("55204695503542");01-07-2019/31-07-2019; 507,58
=("FRO-ELOC015");=("55204695503682");01-07-2019/31-07-2019; 1393,28
=("FRO-IDFTRT02");=("55204695504060");01-07-2019/31-07-2019; 10,31
=("FRO-IDFTRT10");=("55204695504581");01-07-2019/31-07-2019; 1035,77
=("FRO-ELSIEGNE");=("55204695506040");01-07-2019/31-07-2019; 2749,91
=("FRO-BUB2B01");=("55204695506065");01-06-2019/30-06-2019; 369,06
=("FRO-SCDC01");=("74542015800024");01-07-2019/31-07-2019; 1231,65
#$CHARG_SQLFIC
QUIT;
EOF
if [ $? -eq 1 ] ; then
print_log "<E> Erreur lors de l'exécution du script SQL."
maj_cr 99
else
print_log " -> Création OK."
print_log " -> Suppression des caractères de mise en forme SQL*Plus..."
sed -e 's/ //g' \
-e 's/ //g' \
$CHARG_FICSOR > $CHARG_FICSOR.tmp
mv $CHARG_FICSOR.tmp $CHARG_FICSOR
sed 's/ *;/;/g' $CHARG_FICSOR >> $CHARG_FICSOR1
sed 's/; */;/g' $CHARG_FICSOR1 >> $CHARG_FICSOR
fi
fi
imp_etat "$CODSOC" "$CHARG_FICSOR" "Charges"
fi
you can try:
sed 's/ //g' text.csv
output
=("FRO-GEPSA018");=("34212254600225");01-07-2019/31-07-2019;1789,32
=("FRO-GNVERT01");=("41985346000584");01-07-2019/31-07-2019;466,84
=("FRO-ESGED01");=("53983181800064");01-07-2019/31-07-2019;4788,55
=("FRO-SGE");=("54209732400017");01-07-2019/31-07-2019;1587,78
=("FRO-STO");=("54209732400025");01-07-2019/31-07-2019;798,28
=("FRO-ELOC005");=("55204695500985");01-07-2019/31-07-2019;4585
=("FRO-ELOC002");=("55204695502627");01-07-2019/31-07-2019;1997,54
=("FRO-IDFTRT08");=("55204695503542");01-06-2019/30-06-2019;-30,45
=("FRO-IDFTRT08");=("55204695503542");01-07-2019/31-07-2019;507,58
=("FRO-ELOC015");=("55204695503682");01-07-2019/31-07-2019;1393,28
=("FRO-IDFTRT02");=("55204695504060");01-07-2019/31-07-2019;10,31
=("FRO-IDFTRT10");=("55204695504581");01-07-2019/31-07-2019;1035,77
=("FRO-ELSIEGNE");=("55204695506040");01-07-2019/31-07-2019;2749,91
=("FRO-BUB2B01");=("55204695506065");01-06-2019/30-06-2019;369,06
=("FRO-SCDC01");=("74542015800024");01-07-2019/31-07-2019;1231,65
If all the substitution patterns you tried (including that in the previous answer) didn't work, then your whites must be something other than spaces, so rather than use \s in the pattern.

Replace substring with sed

I have a string like this :
test:blabla
And with sed, I want to replace what's after the ':' with something else.
I can manage to replace a word, but not the one after the ':'. I searched on the internet for the answer, but I didn't found anything.
Any help ?
Use: sed 's/:.*/:replaceword/'
$ echo test:blabla | sed 's/:.*/:replaceword/'
test:replaceword
Or for the situation test test:blabla test where you only want to replace the word following : use sed 's/:[^ ]*/:replaceword/':
$ echo "test test:blabla test" | sed 's/:[^ ]*/:replaceword/'
test test:replaceword test
# Use the g flag for multiple matches on a line
$ echo "test test:blabla test test:blah2" | sed 's/:[^ ]*/:replaceword/g'
test test:replaceword test test:replaceword
> echo $SER2
test:blabla
> echo $SER2 | sed 's/\([^:]*:\).*/\1replace/g'
test:replace
>

Pipes inside of sed

Is there a way to send the back references of the SED s/// command to pipes? In order to get an entry from the text, change it, and then write it back. I found that a substitution inside of SED works:
$ echo 'Test ....' | sed 's/Test/'$( echo "<\0>" )'/'
<Test> ....
But the first pipe does not:
$ echo 'Test ....' | sed 's/Test/'$( echo "<\0>" | tr 's' 'x' )'/'
<Test> ....
What is the reason? Additionally, I can't understand why this works at all. I thought that $() substitution should be processed before sed (all the more as I broke the quotes).
And how can I insert one s/// command into another using sed? I use bash.
The tr command is operating on the text "<\0>", not on "<Test>". The back reference isn't expanded in sed until after the pipeline completes. Your second example is equivalent to
foo=$( echo "<\0>" | tr 's' 'x' )
echo 'Test ....' | sed 's/Test/'$foo'/'
It's a little easier to see here that tr has no way of seeing "Test" in its input.
You can achieve the effect you're after with GNU sed and the e flag:
echo 'Test ....' | sed 's/Test.*/echo "<\0>" | tr s x/e'
Output:
<Text ....>

Resources