fex - flexible token-field extraction

What is fex?

Fex is a powerful field extraction tool. Fex provides a very concise language for tokenizeing strings and extracting fields.

The basic usage model is that you provide a series of delimiter and field selection pairs. Tokens can be any character, while field selections have a specific syntax.


Releases are available on GitHub.

Sometimes simpler than cut and awk

cut(1) from GNU coreutils (on Linux) does not support negative offsets, so you cannot ask cut to only show you the Nth field from the end. Additionally, to cut by multiple fields, you end up having to write cut ... | cut ... | cut ...

awk lets you select negative offsets using the NF variable. Get the 2nd to last field with $(NF - 1). However, cutting multiple times still requires multiple awk invocations or using awk’s split() function multiple times.

Field selection

There are a few ways to specify field selections. * Just a number, picks the Nth field. * Comma-separated list inside curly braces selects individual fields: {1,3,7} * Colon-delimited range, inside curly braces: {N:M}. Examples: {1:3}, {1:}, or {:3}. If no M is specified, {N:}, then the range is from N to the end. If no N is specified, {:M}, then N is assumed to be 1 (start of the string). If no N or M is specified, {:}, it behaves as selecting the entire string * You can combine any of these together, such as this syntax: {1,4:7} picks field 1 and 4 through 7 inclusive

Notes: * Negative numbers treated as a negative offset against the end of the string * The number ‘0’ is special and means the entire string, as is {:} * Fields selected in multiple will be joined by the original field separator. That is, selecting /{1,4,7} will result in fields 1, 4, and 7 output with a ‘/’ delimiter in between.

Tokenizing behavior

The default behavior is to ignore empty fields. That is, a string “foo…bar” would only have two fields when split by “.” rather than four. If you want fex to not ignore empty fields, you should prefix your field selection with “?”

Greedy (default)

% echo "foo.....bar..baz.fizz" | fex .2


% echo "foo.....bar..baz.fizz" | fex '.{?6}'

Command line arguments

You can specify multiple, independent field selectors on the command line. Each argument is treated as a standalone field selector. Selectors are split by spaces on output (though I am open to changing this).

For example, output the IP and URL from an apache request log:

echo ' - - [22/Aug/2007:23:39:05 -0400] "GET /svnweb/logwatch/tags/?pathrev=420 HTTP/1.0" 200 3595' \
| fex 1 '"2 2' /svnweb/logwatch/tags/?pathrev=420 

Usage Examples

Simple splitting

With input text: /usr/local/bin/firefox

Command Result
fex /1 “usr”
fex /{2:3} “local/bin”
fex /{1,-1} “usr/firefox”
fex /-1 “firefox”
fex /{:} ”/usr/local/bin/firefox/
fex /0 ”/usr/local/bin/firefox/

Greedy vs nongreedy splitting

Input text: a:b::c:::d

Command Result
fex :{1:3} “a:b:c”
fex :{?1:3} “a:b:”
fex :{3} “c”
fex :{?3} ”” (empty result)

Real world uses

Parse IP and URL from apache logs

% fex 1 '"2 2' < /b/logs/access| head / /icons/blank.gif /icons/folder.gif /favicon.ico /semicomplete/ /style.css /images/semicomplete.png / /style.css /images/semicomplete.png

Show paths from strace

Show files found with strace:

% strace -e trace=file cat /etc/motd |& fex '"2'

Show home directory root paths

Here’s a simple example, to find which root directories contain home directories:

% fex ':-2/1' < /etc/passwd | sort | uniq -c
      3 bin
      1 dev
      4 home
      2 nonexistent
      1 root
      2 usr
     14 var

The string ‘0:-21’ means:

The output is essentially the root directory for everyone’s home directories. Doing this in awk, cut, perl, or any other tool would be much more typing.

You can also specify multiple field extractions on a single invocation:

# Take the first and 2nd to last token split by colon
% fex :1 :-2 < /etc/passwd
root /root
daemon /usr/sbin
bin /bin

# Alternatively, {x,y,z,...} syntax selects multiple tokens
# note that the output is joined by colons.
# Again, this is a feature unavailable in xapply's subfield extraction
% fex ':{1,-2}' < /etc/passwd