Sponsored Content
Top Forums Shell Programming and Scripting Special characters in command arguments Post 302604034 by alister on Friday 2nd of March 2012 03:26:44 PM
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..
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 04:56 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy