LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   getting error "/bin/sed: -e expression #1, char 13: extra characters after command" (https://www.linuxquestions.org/questions/programming-9/getting-error-bin-sed-e-expression-1-char-13-extra-characters-after-command-4175524270/)

novicunix 11-04-2014 01:57 AM

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...")

Please help me out
Thanks

NevemTeve 11-04-2014 03:53 AM

Please us [code] and [/code] tags:
[code]
Code:

sed '/^#element \/abc\/xyz\/mtn...$/d'
[/code]

novicunix 11-04-2014 05:05 AM

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?

danielbmartin 11-04-2014 06:23 AM

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.

Daniel B. Martin

NevemTeve 11-04-2014 09:02 AM

I wouldn't use sed for this. How about awk?
Code:

FILTER='#element /abc/xyz/mtn...'
awk -vfilter="$FILTER" '{ if (filter != $0) print $0; }'


NevemTeve 11-04-2014 09:03 AM

I wouldn't use sed for this. How about awk?
Code:

FILTER='#element /abc/xyz/mtn...'
awk -vfilter="$FILTER" '{ if (filter != $0) print $0; }'


danielbmartin 11-04-2014 09:17 AM

Quote:

Originally Posted by NevemTeve (Post 5264345)
I wouldn't use sed for this. How about awk?

Also worth considering: grep -v.

Daniel B. Martin

NevemTeve 11-04-2014 09:43 AM

(The problem is that the 'filter' part might contain grep meta-characters)

ntubski 11-04-2014 11:30 AM

Quote:

Originally Posted by NevemTeve (Post 5264375)
(The problem is that the 'filter' part might contain grep meta-characters)

grep -Fv

NevemTeve 11-04-2014 11:40 AM

(The problem is that the whole line should match as in ^filter$)

ntubski 11-04-2014 01:08 PM

Quote:

Originally Posted by NevemTeve (Post 5264444)
(The problem is that the whole line should match as in ^filter$)

grep -Fxv

rknichols 11-04-2014 01:35 PM

Quote:

Originally Posted by novicunix (Post 5264263)
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:
Code:

x="#element /abc/xyz/mtn..."
/bin/sed -i ";$x;d" <file>

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.)


All times are GMT -5. The time now is 07:33 AM.