inchik
inchik

Reputation: 11

Is there any wildcard character for awk FS?

Is there any wildcard character for awk FS? In my awk script, I can’t print the values with “^R” separators (I don’t know what type of character is this). On the other hand, it can print those with FS=”*” and others. Like below,

awk 'BEGIN {FS="*"; i=0; ORS=""}
but it can’t do with
awk 'BEGIN {FS="^R"; i=0; ORS=""}

Would appreciate for any help.

Upvotes: 0

Views: 695

Answers (1)

John1024
John1024

Reputation: 113994

^R is octal 022 or hex 12. It is the ASCII DC2 (device control 2) character.

Octal

In awk, use \022 to match ^R as a field separator:

$ echo $'one\022two\022three' 
onetwothree
$ echo $'one\022two\022three' | awk 'BEGIN {FS="\022"; i=0; ORS=""} {printf "1=%s; 2=%s; 3=%s\n",$1,$2,$3}'
1=one; 2=two; 3=three

Regular Expression

It is possible to use regular expressions for field separators. In regular expressions, as opposed to shell globs, the period is the wildcard:

$ echo $'one\022two\022three' | awk -F'.t' '{printf "1=%s; 2=%s; 3=%s\n",$1,$2,$3}'
1=one; 2=wo; 3=hree

Here, the period (wildcard) in .t happens to match the ^R.

Hexadecimal

awk also supports hex notation:

$ echo $'one\022two\022three' | awk -c -F'\x12' '{printf "1=%s; 2=%s; 3=%s\n",$1,$2,$3}'
1=one; 2=two; 3=three

Upvotes: 2

Related Questions