awk match shell variable that contains special characters?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk match shell variable that contains special characters?
# 1  
Old 10-01-2014
awk match shell variable that contains special characters?

How to match a shell variable that contains parenthesis (and other special characters like "!")

file.txt contains:

Charles Dickens
Matthew Lewis (writer)

Code:
name="Matthew Lewis (writer)"; 
awk -v na="$name" ' $0 ~ na' file.txt

Ideally this would match $name in file.txt (in this case Matthew Lewis (writer)). But it doesn't work (gives an error) due to the parenthesis in $name. Is it possible to pass a shell variable that contains special characters in Awk's pattern section?

Last edited by Mid Ocean; 10-01-2014 at 01:15 PM..
# 2  
Old 10-01-2014
One way:
Code:
name='Matthew Lewis \\(writer\\)'
awk -v na="$name" '$0 ~ na {print}' file.txt
Matthew Lewis (writer)

# 3  
Old 10-01-2014
Without modifying the shell variable:
Code:
name="Matthew Lewis (writer)"
awk -v na="$name" 'BEGIN {gsub(/\(|\)/,"\\\&", na)} $0 ~ na' file.txt
Matthew Lewis (writer)

This User Gave Thanks to RudiC For This Post:
# 4  
Old 10-01-2014
Hi thanks. This is great:
Code:
awk -v na="Matthew Lewis (writer)" 'BEGIN{gsub(/\(|\)/,"\\\\&", na)} $0 ~ na' file.txt

It is actually four \\\\& not three \\\& .. but now it works. Thanks!

Last edited by Franklin52; 10-01-2014 at 04:55 PM.. Reason: Please use code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep exact match without period or other special characters

If I have a file like the following abc.1 abc abc_1 abc..1 abc*1 abc@1 abc def ghr def...... ddef 5466 def ed def** 123445 I`m trying to find exact words from the list abc def (4 Replies)
Discussion started by: ritakadm
4 Replies

2. Shell Programming and Scripting

Handle special characters in awk -F

Hello Folks, Need to bisect strings based on a subset. Below works good. echo /a/b/c/d | awk -F"/c/d$" '{print $1}' /a/b However, it goes awry with special characters. echo /a/b/c+/d | awk -F"/c+/d$" '{print $1}' /a/b/c+/d Desired output: /a/b Escaping the special characters... (11 Replies)
Discussion started by: vibhor_agarwali
11 Replies

3. Shell Programming and Scripting

Parsing special characters from variable commands

Hi, I am fairly new to unix scripting and recently tasked with some reporting scripts. The reporting checks several batch jobs and this is quite iterative. Now I am trying to minimize script effort and maximize reusability as there are only slight nuances in the repetitive tasks. For... (3 Replies)
Discussion started by: joeniks
3 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

Trouble with sed and substituting a string with special characters in variable

Hey guys, I know that title is a mouthful - I'll try to better explain my struggles a little better... What I'm trying to do is: 1. Query a db and output to a file, a list of column data. 2. Then, for each line in this file, repeat these values but wrap them with: ITEM{ ... (3 Replies)
Discussion started by: ampsys
3 Replies

6. UNIX for Dummies Questions & Answers

Environment Variable with Special characters

Hello all, I am facing with a problem of invoking an environment variable. If I use this command : /bin/ls -lt FILE_NM_?(20120515|20120516)??????_sas.sig | head -n1 | awk '{print $9}' It perfectly outputs the desired result. FILE_NM_20120516000000_sas.sig But if I do this:... (8 Replies)
Discussion started by: sethmj
8 Replies

7. Shell Programming and Scripting

Sed failing for variable with special characters

This has been covered many times earlier but couldnt figure the issue myself. Can you please advise whats wrong on the below code I have a variable with special character ($) and am using that variable to replace another variable in file but however sed is failing to parse it correctly ... (7 Replies)
Discussion started by: sasiharitha
7 Replies

8. Shell Programming and Scripting

Special characters in a bash variable in sed

Hello, I am trying the following: echo __CHANGEME__ >> testfile VAR1="&&&" sed -i "s|__CHANGEME__|${VAR1}|" testfile cat testfile This results in testfile containing __CHANGEME____CHANGEME____CHANGEME__ Whereas I want it to result in &&& I understand that if VAR1="\&\&\&" then... (3 Replies)
Discussion started by: linuxnewbeee
3 Replies

9. Shell Programming and Scripting

Variable Manimpulation - special characters

Hello- I have a variables that contains a string like this usr/pass@SCHEMA I want to extract the usr/pass part and ignore the SCHEMA part, I tried to use this ${dbconn%%@} and apparently it will not work because @ is a special character. I tried \@ and still no go. Any idea how to solve... (1 Reply)
Discussion started by: Nomaad
1 Replies

10. Shell Programming and Scripting

awk/sed with special characters

i have this script that searches for a pattern. However it fails if the pattern includes some special characters. So far, it fails with the following strings: 1. -Cr 2. $Mj 3. H'412 would a sed or awk be more effective? i don't want the users to put the (\) during the search (they... (5 Replies)
Discussion started by: apalex
5 Replies
Login or Register to Ask a Question