LinuxQuestions.org
Help answer threads with 0 replies.
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 10-18-2012, 07:16 PM   #1
rubster
LQ Newbie
 
Registered: Oct 2012
Posts: 4

Rep: Reputation: Disabled
Removing double quotes and trailing blanks via sed and getting unterminated `s' comma


How to convert all occurrences of schema "BLUES " (which has three, in this case, trailing blanks)to BLUES, so no trailing blanks nor double quotes.
I have set a variable
sed='sed 's/"EMPDW[ ]*"/EMPDW/g''
So if I issue
cat filename | $sed
I get:
sed: -e expression #1, char 8: unterminated `s' command
cat: write error [Broken pipe]



I don't know if it's a Linux thing or what.
I have set -x on at the clp.
If I issue
cat filename | sed 's/"EMPDW[ ]*"/EMPDW/g'
+ 's/"EMPDW[ ]*"/EMPDW/g'
-ksh: s/"EMPDW[ ]*"/EMPDW/g: cannot execute

-ksh: cat: write error [Broken pipe]


I just am looking for a workable solution.
Thanks in advance.
 
Old 10-18-2012, 07:33 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
I think you are making things overly complicated.

First, one does not normally run command strings by putting them into a variable. Assuming that it is possible to get it working in that way---I recommend saving it until you get your basic SED command working.

Second, your SED command does not seem to have anything to do with the problem statement. You said:
Quote:
convert all occurrences of schema "BLUES " (which has three, in this case, trailing blanks)to BLUES, so no trailing blanks nor double quotes.
I would think something like this (file BLU created to test):
Code:
[mherring@herring_desk play]$ more BLU
"BLUES "abc
"BLUES  "abc
"BLUES   "abc
BLUES    xyz
BLUES abc
"BLUES  fgh"
[mherring@herring_desk play]$ sed 's/\"BLUES *\"/BLUES/g' BLU
BLUESabc
BLUESabc
BLUESabc
BLUES    xyz
BLUES abc
"BLUES  fgh"
 
Old 10-18-2012, 07:48 PM   #3
rubster
LQ Newbie
 
Registered: Oct 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
The reason for the variable is that it will be in a ksh script.
 
Old 10-18-2012, 08:09 PM   #4
rubster
LQ Newbie
 
Registered: Oct 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
However, when I substituted BLUES with ${schema} , the command made NO changes. Since this is in a ksh script and ${schema} is unknown, this is my dilemma.
 
Old 10-19-2012, 02:17 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I think we are at odds with information here.

1. pixellany's reference to a command in a variable was the following line:
Code:
sed='sed 's/"EMPDW[ ]*"/EMPDW/g''
Which does not matter what shell you are in, this is not required.

2. When using a variable in sed you need to have the variable in, at most, double quotes as in single quotes it will not be interpreted.

So assuming I am following correctly, something like the following should work:
Code:
schema=BLUES

sed -r "s/\"($schema) *\"/\1/g" filename
 
Old 10-19-2012, 11:55 AM   #6
rubster
LQ Newbie
 
Registered: Oct 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
I've tried the above suggestions without getting desired success.

When I issue a
sed='sed 's/"$schema[ ]*"/$schema/g''
cat filename | $sed
or
cat filename | eval $sed

I get the following:
sed: -e expression #1, char 8: unterminated `s' command and
cat: write error [Broken pipe]


What's strange is that if I vi the file and issue
:%s/"EMPDW " /EMPDW/g

the task gets done. So what's going on with sed?
 
Old 10-19-2012, 12:51 PM   #7
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,776

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Quote:
Originally Posted by rubster View Post
When I issue a
sed='sed 's/"$schema[ ]*"/$schema/g''
cat filename | $sed
or
cat filename | eval $sed

I get the following:
sed: -e expression #1, char 8: unterminated `s' command and
cat: write error [Broken pipe]
What happens is that after doing the substitution for $sed, the shell next does word splitting, and what you intended as a single argument gets split into two separate arguments: 's/EMPDW[' and ']*/EMPDW/g'. The first of those is an unterminated 's' command.

It is very difficult to use shell variables to store complete commands because the line gets parsed multiple times, and getting the quoting and word splitting right can be next to impossible. Use a shell function to hold your command.
Code:
do_sed() {
sed "s/\"$1[ ]*\"/$1/g"
}
.
.
.
cat filename | do_sed $schema
    # or, avoiding a pointless use of cat
do_sed $schema <filename

Last edited by rknichols; 10-19-2012 at 12:56 PM.
 
  


Reply



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] how do i add a line with double quotes in sed? Beandip408 Linux - Newbie 1 02-01-2012 03:22 PM
[SOLVED] bash/sed/awk to convert comma's not in quotes in a line with many comma's oly_r Programming 23 01-25-2012 08:53 AM
Removing the double quotes from the file globally abhimumtd Linux - Newbie 2 01-11-2012 10:52 AM
How to delete Comma in a comma separated file with double quotes as quote character pklcnu Linux - Newbie 2 03-24-2009 05:50 PM
Using single quotes vs double quotes in PHP strings vharishankar Programming 6 07-11-2005 11:41 AM

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

All times are GMT -5. The time now is 02:59 AM.

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