Difficulties in matching left bracket as literal in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Difficulties in matching left bracket as literal in awk
# 1  
Old 08-08-2016
Difficulties in matching left bracket as literal in awk

I need to work with records having #AX in the EXP1 , please see my data sample and my attempt below:

Code:
$ cat xx
08:30:33 KEY1 (1255) EXP1 [#AX0010X001] VAL:20AX0030006
08:30:33 KEY1 (1255) EXP1 [#0200000001] VAL:20AX0030006
08:30:33 KEY1 (1255) EXP1 [#AX0020X002] VAL:20AW0030006
08:30:33 KEY1 (1255) EXP1 [#A02210X001] VAL:20AW0030006
$ gawk '{ if($0 ~ "\[#AX") print; }' xx
gawk: cmd. line:1: warning: escape sequence `\[' treated as plain `['
gawk: cmd. line:1: (FILENAME=xx FNR=1) fatal: Unmatched [ or [^: /[#AX/

I tied other way specify left bracket an it worked:
Code:
gawk '{ if($0 ~ "[[]#AX") print; }' xx
08:30:33 KEY1 (1255) EXP1 [#AX0010X001] VAL:20AX0030006
08:30:33 KEY1 (1255) EXP1 [#AX0020X002] VAL:20AW0030006

I tried awk, nawk and gawk, same results, although gawk is more helpful in spelling out which file and line caused the problem when I used '\[' escape sequence.

My question is why awk dies? and why gawk shows this warning about '\[' treated as plain '['?

Thanks in advance.
# 2  
Old 08-08-2016
This is because you are passing an RE via a string, the string is parsed first and one level of escape characters are stripped. Then the result is processed by the RE parser and another level of escape characters are removed.


Imagine looking for a double quote character - these are not special to the RE so don't need to be escaped there and you could do:
Code:
gawk '{ if($0 ~ "\"AX") print; }' xx
..or..
gawk '{ if($0 ~ /"AX/) print; }' xx

But as [ means something to the RE parser it must arrive there escaped - i.e. double escaped in the string (or single as an RE):

Code:
gawk '{ if($0 ~ "\\[AX") print; }' xx
..or..
gawk '{ if($0 ~ /\[AX/) print; }' xx


Last edited by Chubler_XL; 08-08-2016 at 09:42 PM.. Reason: Reworded to help clarify - English is so vague
These 3 Users Gave Thanks to Chubler_XL 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

Matching number of syllables on right-hand and left side

I am developing a database for translating names. I have mapped through a rule engine syllables in English to syllables in Indic, delimited by an equal to sign. An example will illustrate this ra m=रा म ku ma r=कु मा र mo=मो la l=ला ल gi ta=गी ता ka la va ti=कa ला वa ती However it so... (3 Replies)
Discussion started by: gimley
3 Replies

2. Shell Programming and Scripting

Awk: sed help: How to put bracket in the last character.

Hi Experts, A quick question. - How to put a 2nd bracket in the last character of the input string. a=blah or a=1234 or a=anything Desired output to be : bla or 123 or anythin What I have tried is not working: echo $a| sed 's/$//' blah Thanks a lot. (3 Replies)
Discussion started by: rveri
3 Replies

3. Shell Programming and Scripting

awk to substitute ip without zero left padding

Hello All, I have this script to awk IP to new file. #awk '/myip|yourip/ {sub(/...\....\....\..../, newip)}1' newip=$IP existing.txt > new.txt When existing.txt has myip=192.168.123.123 and $IP has 192.168.12.12, the awk script is not working. But while I add zero left padding to $IP i.e,... (3 Replies)
Discussion started by: Shaan_Shaan
3 Replies

4. Shell Programming and Scripting

left join using awk

Hi guys, I need AWK to merge the following 2 files: file1 1 a 1 1 2 b 2 2 3 c 3 3 4 d 4 4 file2 a a/a c/c a/c c/c a/a c/t c c/t c/c a/t g/g c/c c/t desired output: 1 a 1 1 a/a c/c a/c c/c a/a c/t 2 b 2 2 x x x x x x 3 c 3 3 c/t c/c a/t g/g c/c c/t 4 d 4 4 x x x x x x (2 Replies)
Discussion started by: g1org1o
2 Replies

5. Shell Programming and Scripting

left join using awk

Hi guys, I need to use awk to join 2 files file_1 A 001 B 002 C 003 file_2 A XX1 B XX2 output desired A 001 XX1 B 002 missing C 003 XX2 thank you! (2 Replies)
Discussion started by: g1org1o
2 Replies

6. Shell Programming and Scripting

ksh, difference between double bracket and single bracket

Can somebody tell me the difference between double brackets and single brackets, when doing a test. I have always been acustomed to using single brackets and have not encountered any issues to date. Why would somebody use double brackets. Ie if ] vs if Thanks to... (2 Replies)
Discussion started by: BeefStu
2 Replies

7. Shell Programming and Scripting

Unix Cut or Awk from 'Right TO Left'

Hello, I want to get the User Name details of a user from a file list. This list can be in the format: FirstName_MiddleName1_LastName_ID FirstName_LastName_ID FirstName_MiddleName1_MiddleName2_LastName_ID What i want it to return is FirstName_MiddleName1_LastName of a user. I... (6 Replies)
Discussion started by: limamichelle
6 Replies

8. Shell Programming and Scripting

Left join on files using awk

nawk 'NR==FNR{a;next} {if($1 in a) print $1,"Found" else print}' OFS="," File_B File_A The above code is not working help is appreciated (6 Replies)
Discussion started by: pinnacle
6 Replies

9. Shell Programming and Scripting

trying to cope with awk difficulties

The data we are searching is populated in this way: ----IP---------DAY----MONTH----DATE--------TIME---------YEAR 12.3234.34-----Fri------Nov-------15-------18:05:14 GMT---2008 I want the user to be able to search for the data according to month and year. However, I cannot quite figure out... (8 Replies)
Discussion started by: amatuer_lee_3
8 Replies

10. UNIX for Dummies Questions & Answers

Select text within matching ( ) bracket

Hi, I am looking for a simple command to select text within a open bracket "(" and a matching close bracket ")" and output the within-bracket-text to a file. This function is similar to the common vi select a range of text with "(" to ")" but not sure how to run the same function in command... (4 Replies)
Discussion started by: cursive
4 Replies
Login or Register to Ask a Question