Special characters in command arguments


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Special characters in command arguments
# 8  
Old 03-02-2012
Quote:
Originally Posted by aaron0001
Thanks for responding.

I think you're misunderstanding what I'm trying to do.
Perhaps.

Your attempt to wrap everything in single quotes suggests that you would like to pass the arguments verbatim. If that is indeed the case, the sed is too late; pathanme expansion will have already occurred.

Assuming that the current directory has two files, f1 and f2, if you have a line in the file with cmd *, should cmd's first positional parameter be set to *, or should the single * argument expand to two positional parameters, f1 f2? If when invoked cmd should see a literal *, the sed is too late unless globbing is disabled (same goes for other pathname expasnion metacharacters in my example, ?, [...]) because the parsing of the set -A statement will have already expanded the pathnames after it expands $line (since $line must undergo field splitting to yield multiple members for the array, the usual way to prevent pathname expansion, double quoting, cannot be used). So instead of yielding '*', the quoting step will yield 'f1' 'f2'.

For escaping the single quotes and single quoting each word in a line:
Code:
awk -v q=\' '{for(i=1; i<=NF; i++) {gsub(q, q"\\"q q, $i); $i=q$i q}}1'

Regards,
Alister

---------- Post updated at 03:26 PM ---------- Previous update was at 03:17 PM ----------

Quote:
Originally Posted by aaron0001
awk is obviously a swiss army knife. But at this point I would like to know why I can't accomplish a replace of ' with ''\' with sed.
Code:
sed "s/'/'\\\\''/g"

Code:
sed 's/'\''/'\''\\'\'\''/g'

Regards,
Alister

Last edited by alister; 03-02-2012 at 04:40 PM..
# 9  
Old 03-02-2012
Thanks for your help allistar. Using your posts on sed i was able to play around some more and get what I wanted.

However we must be in different shells / environments because my environment works differently than yours.

For example:

Code:
me@server  dir $temp=*
me@server  dir $echo $temp
file1 file2 file3 file4
me@server  dir $echo $temp[1]
file1
me@server  dir$echo $temp[2]
file2
me@server  dir $

# 10  
Old 03-02-2012
With ksh93, a couple of parameter expansion and assignment operations can also accomplish the task:
Code:
$ line="cmd opt'1 o'p't2"
$ set -A x $line         
$ echo ${x[*]}           
cmd opt'1 o'p't2
$ x=(${x[*]//\'/\'\\\'\'})
$ echo ${x[*]}            
cmd opt'\''1 o'\''p'\''t2
$ x=(${x[*]//(*)/\'\1\'})
$ echo ${x[*]}           
'cmd' 'opt'\''1' 'o'\''p'\''t2'

Note that this does not sidestep the possible issue of pathname expansion during the set statement.

Regards,
Alister

---------- Post updated at 04:08 PM ---------- Previous update was at 04:04 PM ----------

Quote:
Originally Posted by aaron0001
However we must be in different shells / environments because my environment works differently than yours.

For example:

Code:
me@server  dir $temp=*
me@server  dir $echo $temp
file1 file2 file3 file4
me@server  dir $echo $temp[1]
file1
me@server  dir$echo $temp[2]
file2
me@server  dir $

That's typical POSIX-compliant pathname expansion behavior. Given identical commands, my shell would produce identical output.

I believe you may have missed my point regarding pathname expansion.

In any case, happy to have been of help.

Regards,
Alister
# 11  
Old 03-02-2012
Quote:
Originally Posted by aaron0001
I think we are still speaking different languages. Your first few sentences agree with me while saying you don' agree =/.
Did you check the program I posted which precisely matched your requested output?

Scratch that, I understand your point.

It's not just a different language though, I'm using a totally different logic, which could apply just as well to sed as awk.

Why bother splitting it into arguments before you've substituted for special chars? Why bother reading it into the shell, splitting, feeding it into sed piece by individual piece, getting it back out, etc, etc, etc when a single use of sed can escape the entire file in one whack and save you all the complication of dealing with the shell's own substitution and read's own escaping. It also means less chance of errors -- you don't need to armor-plate a substitution that you don't need to feed into sed in the first place. That's also way more efficient.

Last edited by Corona688; 03-02-2012 at 05:26 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using CURL command with special characters in URL

Hi. I 'm trying to hit a REST api and retrieve a JSON feed, and the URL has special characters in it. Something like: Example Domain The below curl command is failing curl -X GET https://www.example.com/?sample=name&id=1001 saying bad command at id=1001 I am going to use this as part... (3 Replies)
Discussion started by: kumarjt
3 Replies

2. Shell Programming and Scripting

Tcsh script - sed command for special characters

Hi, I am using sed command to replace following line in gz file- sed -n 's/""COS_12_TM_1" ( 1101110101001001010011110100000010110100010010000000100000000010XX010000000 )"/""COS_12_TM_1" ( 110111010100100101001111MM00000010110100010010000000100000000010XX010000000 )"/g' filename.gz $x=... (4 Replies)
Discussion started by: Preeti Chandra
4 Replies

3. Shell Programming and Scripting

How to escape all special characters?

I have an application which I am integrating with that accepts the password via a CLI. I am running in to issues with passwords that contain special characters. I tried to escape them all, but I ran in to an issue where I cannot escape the characters ' ] My attempt is as follows: $... (2 Replies)
Discussion started by: AMG1978
2 Replies

4. Shell Programming and Scripting

A test command parameter is not valid, when special characters are tried to match

Hi I have a scenario where hyphen(-) from file should be ignored I used the following code if && ; then if ; then pow=$LINE echo $pow > history.txt flag=1 fi fi I get the following output ./valid.sh: -: 0403-012 A test... (7 Replies)
Discussion started by: Priya Amaresh
7 Replies

5. Shell Programming and Scripting

Need help in replacing special characters

I am writing a ksh script. I need to replace a set of characters in an xml file. FROM="ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÛÚÜÝßàáâãäåçèéêëìíîïðñòóôõö¿¶ø®"; TO="AAAAAAACEEEEIIIIDNOOOOOOUUUUYSaaaaaaceeeeiiiionooooo N R" I have used the code- sed 's/$FROM/$TO/g'<abc.xml But its not working. Can anyone tell me the code to do this? (3 Replies)
Discussion started by: saga20
3 Replies

6. Shell Programming and Scripting

Replace special characters with Escape characters?

i need to replace the any special characters with escape characters like below. test!=123-> test\!\=123 !@#$%^&*()-= to be replaced by \!\@\#\$\%\^\&\*\(\)\-\= (8 Replies)
Discussion started by: laknar
8 Replies

7. Shell Programming and Scripting

special characters

Hey guys, I'm trying to replace "]Facebook" from the text but sed 's/]Facebook/Johan/g' is not working could you please help me with that? (6 Replies)
Discussion started by: Johanni
6 Replies

8. UNIX for Dummies Questions & Answers

How to see special characters?

Hi all, I was wondering how can i see the special characters like \t, \n or anything else in a file by using Nano or any other linux command like less, more etc (6 Replies)
Discussion started by: gvj
6 Replies

9. Shell Programming and Scripting

Special characters

When I open a file in vi, I see the following characters: \302\240 Can someone explain what these characters mean. Is it ASCII format? I need to trim those characters from a file. I am doing the following: tr -d '\302\240' ---------- Post updated at 08:35 PM ---------- Previous... (1 Reply)
Discussion started by: sid1982
1 Replies

10. UNIX for Dummies Questions & Answers

special characters

I have one file which is named ^? ( the DEL character ) I'd like to know how to rename or copy the file by using its i-node number TYIA (2 Replies)
Discussion started by: nawnaw
2 Replies
Login or Register to Ask a Question