Search code examples
linuxawkfish

How to use awk under fish to split a string by triple dollar signs ($$$) as field separator?


awk -F '$' works well with single-dollar-sign-separated string (a$b$c for example), but when it comes to multiple dollar signs, awk does not work.

The expected output is: 1$23, I have tried the following combinations but in vain:

$ printf '1$23$$$456' | awk -F '$$$' '{print $1}'
1$23$$$456
$ printf '1$23$$$456' | awk -F '\$\$\$' '{print $1}'
1$23$$$456
$ printf '1$23$$$456' | awk -F '\\$\\$\\$' '{print $1}'
1$23$$$456
$ printf '1$23$$$456' | awk -F '$' '{print $1}'
1

I wonder if there is a way to split a string by a sequence of dollar signs using awk?

update

$ awk --version
awk version 20070501
$ echo $SHELL
/usr/local/bin/fish

Solution

  • The problem is due to fish quoting rules. The important difference is that fish, unlike Bash, allows escaping a single quote within a single quoted string, like so:

    $ echo '\''
    '
    

    and, consequently, a literal backslash has to be escaped as well:

    $ echo '\\'
    \
    

    So, to get what in Bash corresponds to \\, we have to use \\\\:

    $ printf '1$23$$$456' | awk -F '\\\\$\\\\$\\\\$' '{print $1}'
    1$23