Sed command garbled question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed command garbled question
# 1  
Old 06-21-2012
Sed command garbled question

Code:
for j in $(cat ${list_B})
do
to_replace_2=$(grep $j ${useralias}_2)
sed "s/^${j}/${to_replace_2}/p" ${entries} > ${entries}_2
mv ${entries}_2 ${entries}
done

Hi,
I've the above sed command running in a script. Its basically looping through a file and replacing its beginning of line grep'd match in another file.. It works 90% of the time but sometimes i'll get a garbled error like the one below.. The var ${to_replace_2} can contain any random string of characters. I've also tried to use different separators but that didn't work either..

Not sure why it works for some and not others..

sed: command garbled: s/^CCMAIL/CCMAIL=wa990097


Can anyone help?

Last edited by Jazmania; 06-21-2012 at 06:46 AM..
# 2  
Old 06-21-2012
You know that some characters are special to seds command language, don't you? For instance:

Code:
x="abc" ; y="def"
sed "s/$x/$y/" /path/to/input > /path/to/output

will work as expected, but:

Code:
x="ab/c" ; y="def"
sed "s/$x/$y/" /path/to/input > /path/to/output

will not (and changing the delimiters will only move the problem around, because now some other character will cause the problem). The following even will work, but not in the expected way:

Code:
x="ab*c" ; y="def"
sed "s/$x/$y/" /path/to/input > /path/to/output


The only real solution to this is to escape the problematic characters and this is done by prepending them with backslashes:

Code:
x="ab/c" ; y="def"
x="$(sed 's/\/\\&/')"
sed "s/$x/$y/" /path/to/input > /path/to/output

Here is a quick (and unrefined!) solution which escapes the problematic characters. Use at your own risk:

Code:
sed 's/\[/\\&/g
     s/\]/\\&/g
     s/[/*?{}]/\\&/g'

I hope this helps.

bakunin
# 3  
Old 06-21-2012
Quote:
Here is a quick (and unrefined!) solution which escapes the problematic characters. Use at your own risk:

Code:
sed 's/\[/\\&/g
     s/\]/\\&/g
     s/[/*?{}]/\\&/g'

I hope this helps.

bakunin
Hi thanks for the response.. I do understand that sed has some characters that are special to the command language..

I don't quite see how your above example would fit into my for loop?

Are you saying it should look like the below?

Code:
for j in $(cat ${list_B})
do
to_replace_2=$(grep $j ${useralias}_2)
to_replace_2="(sed 's/\/\\&/')"
sed "s/^${j}/${to_replace_2}/p" ${entries} > ${entries}_2
mv ${entries}_2 ${entries}
done

# 4  
Old 06-21-2012
Quote:
Originally Posted by Jazmania
Are you saying it should look like the below?

Code:
for j in $(cat ${list_B})
do
to_replace_2=$(grep $j ${useralias}_2)
to_replace_2="(sed 's/\/\\&/')"
sed "s/^${j}/${to_replace_2}/p" ${entries} > ${entries}_2
mv ${entries}_2 ${entries}
done

Almost. In fact you have to process the variables contents before using them in a sed statement:

Code:
for j in $(cat ${list_B}) ; do
     to_replace_2="$( grep $j ${useralias}_2 |\
                      sed 's/\[/\\&/g
                           s/\]/\\&/g
                           s/[/*?{}]/\\&/g' \
                    )"
     j="$( echo $j |\
           sed 's/\[/\\&/g
                s/\]/\\&/g
                s/[/*?{}]/\\&/g' \
         )"
     sed "s/^${j}/${to_replace_2}/p" ${entries} > ${entries}_2
     mv ${entries}_2 ${entries}
done

I hope this helps.

bakunin

PS: i just noticed you can now use sed instead of grep: instead of

Code:
to_replace_2="$( grep $j ${useralias}_2 |\
                 sed 's/\[/\\&/g
                      s/\]/\\&/g
                      s/[/*?{}]/\\&/g' \
               )"

You just use:

Code:
to_replace_2="$(sed -n '/'"${useralias_2}"'/ {
                              s/\[/\\&/g
                              s/\]/\\&/g
                              s/[/*?{}]/\\&/g
                              p
                              q
                        }'
               )"


Last edited by bakunin; 06-21-2012 at 11:20 AM..
# 5  
Old 06-21-2012
Hi bakunin,

I tried the first way you gave and it still doesn't seem to fix the issue.. (Although it did help me out further down my script..) Can't figure out why it stalls on one particular file.. It's formatted the exact same way as everything else..

Also tried your sed suggestion but I ended up just getting a load of the following:

Quote:
First RE may not be null
My code is as follows but still getting a garbled command

Code:
for j in $(cat ${list_B})
do
     to_replace_2="$( grep $j ${useralias}_2 |\
                      sed 's/\[/\\&/g
                           s/\]/\\&/g
                           s/[/*?{}]/\\&/g' \
                    )"
     j="$( echo $j |\
           sed 's/\[/\\&/g
                s/\]/\\&/g
                s/[/*?{}]/\\&/g' \
         )"
     sed "s/${j}/${to_replace_2}/1" ${entries} > ${entries}_2
     mv ${entries}_2 ${entries}
done


sed: command garbled: s/CCMAIL/CCMAIL=wa990097
# 6  
Old 06-21-2012
useless use of backticks

Code:
while read FILENAME
do
...
done < inputfile

Try replacing sed with echo just to make absolutely positive you're giving it the parameters you think you are.
# 7  
Old 06-21-2012
Quote:
Originally Posted by Corona688
useless use of backticks

Code:
while read FILENAME
do
...
done < inputfile

Try replacing sed with echo just to make absolutely positive you're giving it the parameters you think you are.
Hi,
the garbled command is showing what i need.. I'm trying to replace

CCMAIL with CCMAIL=wa990097 but i don't know why it won't do it.. It works for files I'm working with..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Command garbled when using sed

Hi everyone, Can anyone help me in question below? I want to cron a job to add partition every month but get the error sed: command garbled: s/YYYYMM/ . > echo $YYYYMON_NEW 201304 > echo $YYYYMON_OLD 201208 > echo $YYYY_MM_NEW 2013-05 This is my base script: ALTER TABLE STS.ADMIN ADD... (1 Reply)
Discussion started by: fenocean
1 Replies

2. Shell Programming and Scripting

sed help - Command garbled

Hi, First post for a noob so please go easy with me :) I have a XML block that is originally like this: <SETNAME>somecrap/THIS</SETNAME> and I would like it be replaced with, in the original file: <SETNAME>THIS</SETNAME> I tried to use: sed... (4 Replies)
Discussion started by: zhuanyi
4 Replies

3. Shell Programming and Scripting

sed: command garbled error

Hi all, Suppose that I want to update the db.password entry in the below properties file, db.username=admin db.password=qhKkBno2U5GEq5k/dnmGPA== //I want this line to be replaced by: "db.password=abc/123/" db.host=server db.port=22 db.sid=database However when... (2 Replies)
Discussion started by: isaacniu
2 Replies

4. Shell Programming and Scripting

Help needed - ksh: sed: command garbled:

Hi all, What am I doing wrong here? $ cat test_sed.ksh #!/usr/bin/ksh var="sed -e \'6s/9/6/\' testfile.txt > testfile.txt.2" $var $ ./test_sed.ksh sed: command garbled: \'6s/9/6/\' Thank you! (4 Replies)
Discussion started by: ejianu
4 Replies

5. Shell Programming and Scripting

sed command garbled error

sed: command garbled: s/ri="*"/ri=" what i did is you can see below sed "s/ri=\"*\"/ri=\"$newri\"/" $2 > output how to solve this (2 Replies)
Discussion started by: pasricha.kunal
2 Replies

6. Shell Programming and Scripting

sed: command garbled

Hi, I have a file1 as : A=/home/user B=/home/user1 C=/home/user2 D=/home/aacsms E=/home/user1/temp F=/home/user1/area1 and my script as: a=`cat /home/aacsms7/file1 | grep -i e` b=`user2` sed 's/'$a'/"E=/home/'$b'/temp"/g' < file1 > file2 sed: command garbled:... (3 Replies)
Discussion started by: yesmani
3 Replies

7. Shell Programming and Scripting

Sed: command garbled :s/

Hi, I really need some help, I am using a very basic script to proess a text file. This script has been used many times but all of a sudden all on it's own it's stopped working. The line in the script is: sed 's/ //g' $ORGFILE > $NEWFILE and the error is Sed: command garbled :s/ All... (3 Replies)
Discussion started by: heidi.lightfoot
3 Replies

8. Shell Programming and Scripting

Passing a variable in SED getting command garbled

I fairly new to SED. I have tried many different variations of this line of code and even breaking it down into its components and running them separately. They work individually without variables but when I place the $todbname variable it will either inserts the text "connect to $todbname"... (3 Replies)
Discussion started by: edewerth
3 Replies

9. Shell Programming and Scripting

sed: command garbled error.....

Dear friends, please give me the solution to the following query. If a file contains multiple tags of same name, then how to get the required string between the tags, in which the string begins with "W/X" i.e., file1.txt contains following text(please note that all the following tags are in... (1 Reply)
Discussion started by: swamymns
1 Replies

10. Shell Programming and Scripting

sed: command garbled

Im getting this error message when trying to substitute filepaths in a sed search and replace string #!/usr/bin/ksh ORACLE_SID=PH3 ORACLE_ADMIN=/data01/app/oracle/admin/$ORACLE_SID DATAFILE_DIR=/asterisk/oradata/$ORACLE_SID sed -e s/DBNAME/$ORACLE_SID/g < initPH2.ora | sed -e... (1 Reply)
Discussion started by: blakmk
1 Replies
Login or Register to Ask a Question