Removing double quotes and trailing blanks via sed and getting unterminated `s' comma
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.
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.
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"
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.