awk: Force "escaped" representation of a field


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk: Force "escaped" representation of a field
# 1  
Old 08-04-2011
awk: Force "escaped" representation of a field

Is there any way to get the escaped version of a field in awk? For those that don't understand the question, here is a clarifying example. Lets say a field $1 gives me the string "(dumb'" (I've changed the delimiter to be something other than whitespace). If i use that value in a command in awk as follows... awk{system("echo " $1)}' I get an error Because of the parenthesis and the apostrophe. What I want is to get an escaped version of $1 (which would be \(dumb\' in this case). Is this possible?
# 2  
Old 08-04-2011
Try to use this awk function (I've found it here: Safely escape variables in awk \1)
Code:
function escape_pattern(pat,   safe) {
  safe = pat
  gsub(/[][^$.*?+{}\\()|]/, "\\\\&", safe)
  return safe
}

This User Gave Thanks to yazu For This Post:
# 3  
Old 08-04-2011
Thanks; I had to modify it a bit to get it to do exactly what i wanted, but it worked out.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

What does "force devmap reload" as in "multipath -r" means for my system and stability of my system?

Cannot present unpresented disks back again. On a test server tried this as a solution "multipath -r" and it worked. Too worried to try it in production before I know all the information. Any info would be appreciated! Also some links to the documentation on this specific issue could help a... (1 Reply)
Discussion started by: jsteppe
1 Replies

2. Shell Programming and Scripting

Awk,sed : change every 2nd field ":" to "|"

Hi Experts, I have a string with colon delimited, want 2nd colon to be changed to a pipe. data: 101:8:43:4:72:14:41:69:85:3:137:4:3:0:4:0:9:3:0:3:12:3: I am trying with sed, but can change only 1 occurance: echo "101:8:43:4:72:14:41:69:85:3:137:4:3:0:4:0:9:3:0:3:12:3:" | sed 's/:/|/2'... (5 Replies)
Discussion started by: rveri
5 Replies

3. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

4. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

5. UNIX for Dummies Questions & Answers

Explanation of "total" field in "ls -l" command output

When I do a listing in one particular directory (ls -al) I get: total 43456 drwxrwxrwx 2 root root 4096 drwxrwxrwx 3 root root 4096 -rwxrwxr-x 1 nobody nobody 3701594 -rwxrwxr-x 1 nobody nobody 3108510 -rwxrwxr-x 1 nobody nobody 3070580 -rwxrwxr-x 1 nobody nobody 3099733 -rwxrwxr-x 1... (1 Reply)
Discussion started by: proactiveaditya
1 Replies

6. Shell Programming and Scripting

bash ignores escaped $ and says "bad substitution"

Can someone tell me how to get the version of bash that I am running? I'm running cygwin bash on Windows XP at home and cygwin bash on Vista at work. Is this the version number for bash? $ uname -a CYGWIN_NT-6.0 US-SEA-L3BER9K 1.5.25(0.156/4/2) 2008-06-12 19:34 i686 Cygwin This is the... (2 Replies)
Discussion started by: siegfried
2 Replies

7. Shell Programming and Scripting

cat $como_file | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g'

hi All, cat file_name | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g' Can this be done by using sed or awk alone (4 Replies)
Discussion started by: harshakusam
4 Replies

8. Shell Programming and Scripting

Additional question to "awk to replace particular field"

I guess it was getting a little messy on the other post so here goes: Link to previous post for Question: https://www.unix.com/shell-programming-scripting/111338-awk-replace-particular-field.html Continuation of Question hey i was messing around a bit ... made me wonder... If the... (1 Reply)
Discussion started by: VGR
1 Replies

9. UNIX for Advanced & Expert Users

Postfix force sender ("From:" field)

Hello, We have Linux post server with Postfix running on it and 5 domains. Everything is working fine, except one thing. I was wondering how can I force user X to be able to send email ONLY as X@domain1.com, but NOT as X@domain2.com for example. I.e. how can I create map user --> email , and... (0 Replies)
Discussion started by: +Yan
0 Replies

10. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question