sed command throwing error while deleting a line from a file

Hi all,

I ahve a program which has to delete a line in a file... if i run the sed command through shell prompt it works fine. But if run it using code its throwing error. May i know where i am doing wrong.

the file has 3 lines

[root@ ~]# cat /root/.ssh/known_hosts
|1|MXOU9X7fgx6AHKiY334LC42jZ1Q=|cWBsRbmCKgYzDotT1TuVS1iyc3s= ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAzwqooVvj4pkERPlaxfCqbhOeuha3bbv3/YLLy6+drGn8W2mV/sS2vrOfHnYfU3xGdWNt6GFoA7PC5dEdU/MWlkMcxq4y7ceBVVojl9Q1qJntHpEvzW6oKNMg+Usx4BiuAOBqpCgcdequBCBzsvFdO7boJXckwanEVLAkI91Nlo/8bafQYCiOcxIx7D15UPFvVEUd+H7PSs8YC7FySymXTOuq45ppQfHV3ztlF9LWUasbTDKdUhmysV0dos49GzhMUkyrbph+NC+J/jWAMXAtHTiIbi9Sy4tVXpcM8Aq+8isDMpBKtIjESsF7Lei4xD9GKQUdz7sp4MgK3EjL0/hJww==
|1|j5o3eNW9A4j5UKZ8xUCQqCVlym4=|paupRZ08Js918aoLZO1mwQRgUYE= ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAt9rMbOvoPnaL0TJdENoYA1ZOrALwXTEbjyi67cxNOYAQzdjbNgm3bUXkAjumhgjGLwSz04PbjXFv+W+bTffh2EDFuQSW6tEiG1ITTsckOymG6My0A5zSLPsaIAGIWgo9F+mw8x6URAY6yBT+ja187CpjhVoUMnJrE5ght+3PZaRJcFdSWlT3izj3++4khpS+Q4cxNogCuEslGxN5uAxBFbQLxkp3qFEJGCAOl1zrU5gcmTLeIO1Q3+fBEX3F1u3y0/+/vcZ55ZOSkHUvxOVUZ0u3RdWId0sAL5yNrexj8jrsgziJnjWrHhNOjtH0Ou33VXOxyBTCvWmtxex2plTgyw==
|1|RcXETg1pFCqRpgvAB9tVWoL0pl8=|uW/xYTXcf+O22U8Xam4dT35C0gE= ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAt9rMbOvoPnaL0TJdENoYA1ZOrALwXTEbjyi67cxNOYAQzdjbNgm3bUXkAjumhgjGLwSz04PbjXFv+W+bTffh2EDFuQSW6tEiG1ITTsckOymG6My0A5zSLPsaIAGIWgo9F+mw8x6URAY6yBT+ja187CpjhVoUMnJrE5ght+3PZaRJcFdSWlT3izj3++4khpS+Q4cxNogCuEslGxN5uAxBFbQLxkp3qFEJGCAOl1zrU5gcmTLeIO1Q3+fBEX3F1u3y0/+/vcZ55ZOSkHUvxOVUZ0u3RdWId0sAL5yNrexj8jrsgziJnjWrHhNOjtH0Ou33VXOxyBTCvWmtxex2plTgyw==
[root@ ~]#

The error thrown is as below...

here 1 : lineno: 1
'ed: -e expression #1, char 2: unknown command: `
here 2
'ed: -e expression #1, char 2: unknown command: `

below is the part of the code where its going wrong.

echo "here 1 : lineno: $lineno"
sed -i "${lineno}d" /root/.ssh/known_hosts
echo "here 2"
sed -i ''$lineno'd' '/root/.ssh/known_hosts'

can anyone give me a quick fix for this. any help is deeply appreciated. thanks

What are the exact contents of lineno? copy-paste this and post the output:

printf "[%s]\n" "${lineno}"

Also beware that sed -i copies and replaces the file, which may mess up permissions for security-sensitive files like ssh keys. It'd be better to

sed ... < /root/.ssh/known_hosts > /root/.ssh/$$
cat < /root/.ssh/$$ > /root/.ssh/known_hosts
rm /root/.ssh/$$

Hi Corona, i have posted the contents of lineno using echo. it has "1"

Printf output is coming as below

here 1 : lineno: 1
'ed: -e expression #1, char 2: unknown command: `
here 2
'ed: -e expression #1, char 2: unknown command: `
print f output is below:
]1

Humor me... We don't know that it's not 1 or 1\r or what have you. In fact, do it like this:

printf "[%s]\n" "${lineno}" | hexdump -C

Your error messages in particular make me very very suspicious that a carriage return got into your statement somehow:

'ed: -e expression #1, char 2: unknown command: `

Look at that "ed". It said 'sed' until a carriage return kicked the cursor back to the beginning of the line, then overwrite the s with the ending '. I'd interpret that as sed: -e expression #1, char 2: unknown command: `\r'

Where do you get the value for your ${lineno} variable?

1 Like

Here is the output for the printf and hexdump

[root@ ~]# lineno=`grep "/root/.ssh/known_hosts:" /tmp/dateOutputError.txt | awk -F' ' '{print $4}' | cut -d':' -f2`
[root@ ~]# printf "[%s]\n" "${lineno}"
]1
[root@ ~]# echo $lineno
1
[root@ ~]# printf "[%s]\n" "${lineno}" | hexdump -C
00000000  5b 31 0d 5d 0a                                    |[1.].|
00000005
[root@ ~]#


---------- Post updated at 09:29 PM ---------- Previous update was at 09:15 PM ----------

I think its working now..

did below workaround to fix trailing carriage return.. thanks Corona

[root@~]# lineno=`grep "/root/.ssh/known_hosts:" /tmp/dateOutputError.txt | awk -F' ' '{print $4}' | cut -d':' -f2 | sed 's/\r$//' `
[root@ ~]#

[root@ ~]# printf "[%s]\n" "${lineno}" | sed '/^$/d'                                                                          
[1]
[root@ ~]#

1 Like