getting error "/bin/sed: -e expression #1, char 13: extra characters after command"
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
getting error "/bin/sed: -e expression #1, char 13: extra characters after command"
Hi,
I am trying to delete a line from a file. This line contains '#' and '/' like below.
#element /abc/xyz/mtn...
When I am using a sed to delete the command, I am getting extra characters after command.
The command I used is as below.
/bin/sed -i '/#element /abc/xyz/mtn.../d' <file>
I tried using a different delimiter as below. The error is gone but the line is not deleted.
sed -i '/\($x\)/d' <file> ( Here x="#element /abc/xyz/mtn...")
Hi,
Thanks for the reply. But I am using the string '#element /abc/xyz/mtn...' as variable and using that variable in the sed command.
What can I do in that case?
Help us to help you. Provide a sample input file (10-15 lines will do). Construct a sample output file which corresponds to your sample input and post both samples here. With "Before and After" examples we can better understand your needs and also judge if our proposed solution fills those needs.
Hi,
Thanks for the reply. But I am using the string '#element /abc/xyz/mtn...' as variable and using that variable in the sed command.
What can I do in that case?
You need to use a delimiter character that can never appear in the string. For example, if you know that the string can never contain a semicolon:
If there is no character that you can rule out, then you would have to modify the search string to escape each instance of that character. For example, using "/" as the delimiter:
Code:
x="#element /abc/xyz/mtn..."
xmod="$(echo "$x" | sed 's;/;\\/;g')"
sed -i "/$xmod/d" <file>
You still have to worry about characters such as "." in your example that are special in a regular expression. You can escape those at the same time you are escaping the delimiter character:
Code:
xmod="$(echo "$x" | sed 's;[[/^$.*\\];\\&;g')"
(I think that covers all the cases needed for a basic regular expression.)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.