LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-04-2014, 01:57 AM   #1
novicunix
LQ Newbie
 
Registered: Jan 2013
Posts: 18

Rep: Reputation: Disabled
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
 
Old 11-04-2014, 03:53 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,864
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Please us [code] and [/code] tags:
[code]
Code:
sed '/^#element \/abc\/xyz\/mtn...$/d'
[/code]
 
1 members found this post helpful.
Old 11-04-2014, 05:05 AM   #3
novicunix
LQ Newbie
 
Registered: Jan 2013
Posts: 18

Original Poster
Rep: Reputation: Disabled
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?
 
Old 11-04-2014, 06:23 AM   #4
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
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
 
Old 11-04-2014, 09:02 AM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,864
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
I wouldn't use sed for this. How about awk?
Code:
FILTER='#element /abc/xyz/mtn...'
awk -vfilter="$FILTER" '{ if (filter != $0) print $0; }'
 
Old 11-04-2014, 09:03 AM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,864
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
I wouldn't use sed for this. How about awk?
Code:
FILTER='#element /abc/xyz/mtn...'
awk -vfilter="$FILTER" '{ if (filter != $0) print $0; }'
 
Old 11-04-2014, 09:17 AM   #7
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by NevemTeve View Post
I wouldn't use sed for this. How about awk?
Also worth considering: grep -v.

Daniel B. Martin
 
Old 11-04-2014, 09:43 AM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,864
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
(The problem is that the 'filter' part might contain grep meta-characters)
 
Old 11-04-2014, 11:30 AM   #9
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082
Quote:
Originally Posted by NevemTeve View Post
(The problem is that the 'filter' part might contain grep meta-characters)
grep -Fv
 
1 members found this post helpful.
Old 11-04-2014, 11:40 AM   #10
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,864
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
(The problem is that the whole line should match as in ^filter$)
 
Old 11-04-2014, 01:08 PM   #11
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082
Quote:
Originally Posted by NevemTeve View Post
(The problem is that the whole line should match as in ^filter$)
grep -Fxv
 
Old 11-04-2014, 01:35 PM   #12
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Quote:
Originally Posted by novicunix View Post
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.)
 
2 members found this post helpful.
  


Reply

Tags
sed



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] sed "expression #1, char 2: extra characters after command" TheBigMing Linux - Newbie 9 03-26-2013 12:19 PM
[SOLVED] sed gives :sed: -e expression #1, char 1: unknown command: `'' samasat Linux - Newbie 10 06-09-2012 05:31 PM
[SOLVED] Sed giving "extra characters after command" error theonislair Programming 3 06-09-2012 03:42 AM
sed error message:extra characters after the command wmh830621 Programming 4 08-14-2006 07:13 PM
sed error message: extra characters after the command. nano_mag Linux - General 3 05-15-2005 01:00 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration