[Solved] Disappearing backslash

Let's say I have a text file called process.out that contains:

cn=long\, ann,cn=users
cn=doe\, john,cn=users

I need to have the following appended in the beginning

ldapdelete -h $OIDHOST

So the final output looks like:

ldapdelete -h $OIDHOST "cn=long\, ann,cn=users"
ldapdelete -h $OIDHOST "cn=doe\, john,cn=users"

The problem is that I need the 'backslash' in the command and whatever I try, it gets filtered out. I tried this:

cat process.out  | while read CMD; do echo ldapdelete -h $OIDHOST \"$CMD\"; done

Which results in:

ldapdelete -h $OIDHOST "cn=long, ann,cn=users"
ldapdelete -h $OIDHOST "cn=doe, john,cn=users"

Any thoughts?

That is a useless use of cat.

That aside, read is eating your backslashes here. By default it tries to interpret them. Use read -r to avoid that problem:

while read -r CMD
do
        echo ldapdelete -h $OIDHOST \"$CMD\"
done < inputfile
1 Like

As always, in command arguments put $var in quotes!

while read -r CMD
do
        echo ldapdelete -h "$OIDHOST" \""$CMD"\"
done < inputfile

Special characters like backslash are preserved.

1 Like

Hello,

Following may help too.

awk -vs1="ldapdelete -h $OIDHOST" -vs2="\"" '{print s1" "s2$0s2}' file_name

Output will be as follows.

ldapdelete -h  "cn=long\, ann,cn=users"
ldapdelete -h  "cn=doe\, john,cn=users"

Thanks,
R. Singh

Thanks all! You have been very helpful as usual!