Reputation: 29
How I can use is awk
delimiter which contains multivalue: "#@$"
I have file like this: Test1#@$Test2#@$Test3#@$Test4
I need to extract 'Test2'.
After I execute this command: awk -F "#@$" '{print $2}'
, nothing is displayed>
And after that awk -F "#@$" '{print $1}'
i get full line
Any ideas?
Upvotes: 0
Views: 273
Reputation: 26471
The issue you are having is that the field separator FS
is considered to be a regular expression. The <dollar>-character ($
) has a special meaning in regular expressions as it represents an anchor for the end-of-the-line. The solution is to escape it twice as the <backslash>-escapes are interpreted twice; once in lexical processing of the string and once in processing the regular expression:
awk -F '#@\\$' '{print $1}'
An extended regular expression can be used to separate fields by assigning a string containing the expression to the built-in variable
FS
, either directly or as a consequence of using the-F
sepstring option. The default value of theFS
variable shall be a single <space>. The following describesFS
behaviour:
- If
FS
is a null string, the behaviour is unspecified.If
FS
is a single character:
- If
FS
is <space>, skip leading and trailing <blank> and <newline> characters; fields shall be delimited by sets of one or more <blank> or <newline> characters.- Otherwise, if
FS
is any other characterc
, fields shall be delimited by every single occurrence ofc
.Otherwise, the string value of
FS
shall be considered to be an extended regular expression. Each occurrence of a sequence matching the extended regular expression shall delimit fields.source: POSIX awk standard
A <dollar-sign> (
$
) outside a bracket expression shall anchor the expression or subexpression it ends to the end of a string; such an expression or subexpression can match only a sequence ending at the last character of a string. For example, the EREsef$
and(ef$)
matchef
in the stringabcdef
, but fail to match in the stringcdefab
, and the EREe$f
is valid, but can never match because thef
prevents the expressione$
from matching ending at the last character.
Upvotes: 1
Reputation: 8711
Just wrap $ in brackets [] to remove its special significance
> cat t1
Test1#@$Test2#@$Test3#@$Test4
> awk -F '#@[$]' '{print $2}' t1
Test2
>
Upvotes: 1