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