Shell awk Regular Expression matching

Source: Internet
Author: User

Today, we can see that awk in a shell script processes strings, and there is a gsub ("\\. [0-9] + $ "," ", $2), and the $2 format is 00:12:13. 333, this gsub wants to put. 333. The regular expression in the regular expression cannot be matched. it's 333. \ After escaping, it becomes a \, and the original. it is not escaped, so it cannot be truncated. 333, but it can be run correctly, and then remove another \ to try again, it can also be, but the prompt awk: Warning: Escape Sequence '\. 'treated
As plain '.'

I checked it and it should be the problem of escaping multiple times. I have explained it clearly and reprinted it as follows:

Original address: http://pzy84.blogbus.com/logs/80017403.html

For the pattern in the awk program, use "//" to enclose it, such

Mount | awk '/type (ext3 | tmpfs)/{print $1 }'

The field separator also supports regular expressions. It is a variable named Fs in the awk program. You can use the-F parameter in the command line to set the value of the FS variable, such
Awk-F' [:/] ''{print $2 }'

If square brackets are delimiters, for example, to extract the timestamp enclosed by [] in the log, you need to carefully use quotation marks and escape, because shell will escape them first.

Through the experiment, I found that there are three layers of escape, which are executed in sequence:
Shell
Awk
Field separator Processor
To prohibit shell escape, enclose the FS value in single quotes. Otherwise, use double quotation marks or avoid quotation marks (only when the parameter does not contain spaces ).

The escape of an awk cannot be prohibited. Therefore, you can only accumulate the escape to offset its effect, that is, use '\' to express '\'.

Field separator processor is one of my imagination. In short, our goal is to make the FS value exactly the most fundamental regular expression.

The following are some examples. The task of the sample program is to extract "234 ABC" from "[234 ABC] lalala ".

(1) failed. "[\ [\]" is converted to "[[]" by awk.
[Pzy @ VM ~] $ Echo "[234 ABC] lalala" | awk-F' [\ [\] ''{print $2 }'
Awk: Warning: Escape Sequence '\ ['treated as plain '['
Awk: Warning: Escape Sequence '\] 'treated as plain']'

(2) Success: "[\ [\]" is converted to "[\ [\]" by awk, and this is the expected result.
[Pzy @ VM ~] $ Echo "[234 ABC] lalala" | awk-F' [\ [\] ''{print $2 }'
234 ABC

(3) failed. "[\ [\]" is first transferred by Shell to "[\ [\]" and then converted to "[[]" by awk.
[Pzy @ VM ~] $ Echo "[234 ABC] lalala" | awk-F "[\ [\]" '{print $2 }'
Awk: Warning: Escape Sequence '\ ['treated as plain '['
Awk: Warning: Escape Sequence '\] 'treated as plain']'

(4) succeeded. "[\\\\ [\\\]" first converted to "[\\ [\\] by shell", it is converted to "[\ [\]" by awk.
[Pzy @ VM ~] $ Echo "[234 ABC] lalala" | awk-F "[\\\\ [\\\]" '{print $2 }'
234 ABC

(5) successful, same as (4)
[Pzy @ VM ~] $ Echo "[234 ABC] lalala" | awk-f [\\\\ [\\\] '{print $2 }'
234 ABC

(6) I don't understand the principle of success. many variants can be derived from what I see in the Forum.
[Pzy @ VM ~] $ Echo "[234 ABC] lalala" | awk-f [] [] '{print $2 }'
234 ABC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.