Can't remove spaces with sed when calling it from sh -c


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Can't remove spaces with sed when calling it from sh -c
# 1  
Old 12-12-2014
Can't remove spaces with sed when calling it from sh -c

The following command works
Code:
 echo "some text with spaces" | sh -c 'sed -e 's/t//g''

But this doesn't and should
Code:
 echo "some text with spaces" | sh -c 'sed -e 's/ //g''

Any ideas?
# 2  
Old 12-12-2014
Try
Code:
echo "some text with spaces" | sh -c 'sed -e "s/ //g"'

and/or
Code:
echo "some text with spaces" | sh -c 'sed -e 's/[[:blank:]]//g''

and/or
Code:
echo "some text with spaces" | sh -c "sed -e 's/ //g'"

But why not directly
Code:
echo "some text with spaces" | sed -e 's/ //g'

This User Gave Thanks to junior-helper For This Post:
# 3  
Old 12-13-2014
Quote:
Originally Posted by Tribe
The following command works
Code:
 echo "some text with spaces" | sh -c 'sed -e 's/t//g''

But this doesn't and should
Code:
 echo "some text with spaces" | sh -c 'sed -e 's/ //g''

Any ideas?
A bit of background: sh -c expects a single string, which is interpreted as a command. Anything after that are positional parameters to that command. So by this way of quoting, sh -c gets passed two strings:

Code:
'sed -e 's/

and
Code:
//g''

The first is interpreted as a command:
Code:
sed -e s/

which is a call to sed with an unterminated substitute pattern s/ (s command) and therefore this will fail.


In the first case, instead of a space there is the letter t, so only one string gets passed to sh -c, namely:
Code:
'sed -e 's/t//g''

which gets interpreted as the following command:
Code:
sed -e s/t//g

which is a valid command.

Last edited by Scrutinizer; 12-13-2014 at 01:41 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 12-13-2014
If you really would need a ' within a ' ' string then you need to end the string with ' then follow an escaped ' then another ' to begin a second string.
Code:
echo "some text with spaces" | sh -c 'sed -e '\''s/ //g'\'

This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Calling a find/remove within a script

Greetings all, I am calling a remove from within a script that is used for a cleanup process.. It is not working as expected. Here is what I am doing. I have a config file that lists out a directory name, and the options to run Within the config file DIR1="find... (9 Replies)
Discussion started by: jeffs42885
9 Replies

2. UNIX for Dummies Questions & Answers

Using sed to remove spaces from middle of the line

Hi, I need to correct href portion of the lines to edit out spaces from the line starting with position "<a href=" and ending at "target=" Below are 2 examples of extra space added by the server: <td width=251 colspan=9 rowspan=22> <font size=2 face="courier"><tt><style>{font:7pt Courier ... (4 Replies)
Discussion started by: friedmi
4 Replies

3. Shell Programming and Scripting

remove spaces

Hi folks, I need to remove spaces at the end of each line in a *.txt file. it looks like this word 1 word 2 . . . word n i found some sed commands but any of them didnt work so far thank you for your posts (6 Replies)
Discussion started by: Jimmy7
6 Replies

4. Shell Programming and Scripting

sed remove newlines and spaces

Hi all, i am getting count from oracle 11g by spooling it to a file. Now there are some newline characters and blank spaces i need to remove these. pl provide me a awk/sed solution. the spooled file is attached. i tried this.. but not getting req o/p (6 Replies)
Discussion started by: rishav
6 Replies

5. Shell Programming and Scripting

tr and sed remove spaces. how to stop this?

if the answer is obvious, sorry, I'm new here. anyway, I'm using tr to encrypt with rot-13: echo `cat $script | tr 'a-zA-Z' 'n-za-mN-ZA-M'` > $script it works, but it removes any consecutive spaces so that there is just one space between words. I've had this problem before while using sed to... (5 Replies)
Discussion started by: Trichopterus
5 Replies

6. Shell Programming and Scripting

How to remove spaces using awk,sed,perl?

Input: 3456 565 656 878 235 8 4 8787 3 7 35 878 Expected output: 3456 565 656 878 235 8 4 8787 3 7 35 878 How can i do this with awk,sed and perl? (10 Replies)
Discussion started by: cola
10 Replies

7. Shell Programming and Scripting

Remove Spaces

Hi All, I have a comma seperated file. I wanna remove the spaces from column 2. I mean i don't wanna remove the spaces those are presnt in column 1. ex: emp name, emp no, salary abc k, abc 11, 1000 00 bhk s, bhk 22, 2000 00 the output should be: emp name, emp no, salary abc k, abc11,... (4 Replies)
Discussion started by: javeed7
4 Replies

8. Shell Programming and Scripting

sed - remove spaces before 1rst occurence of string

seems easy but havent found in other posts... i want to delete any spaces if found before first occurence of ${AI_RUN} sed 's/ *\\$\\{AI_RUN\\}/\\$\\{AI_RUN\\}/' $HOME/temp1.dat i think i'm close but can't put my finger on it. :rolleyes: (6 Replies)
Discussion started by: danmauer
6 Replies

9. Shell Programming and Scripting

sed over writes my original file (using sed to remove leading spaces)

Hello and thx for reading this I'm using sed to remove only the leading spaces in a file bash-280R# cat foofile some text some text some text some text some text bash-280R# bash-280R# sed 's/^ *//' foofile > foofile.use bash-280R# cat foofile.use some text some text some text... (6 Replies)
Discussion started by: laser
6 Replies

10. Shell Programming and Scripting

how to remove spaces in a string using sed.

Hello, I have the following to remove spaces from beginning and end of a string. infile=`echo "$infilename" | sed 's/^ *//;s/ *$//` How do I modify the above code to remove spaces from beginning, end and in the middle of the string also. ex: ... (4 Replies)
Discussion started by: radhika
4 Replies
Login or Register to Ask a Question