Recall that jq supports the same datatypes as JSON.
JSON describes strings as:
A string is a sequence of zero or more Unicode characters, wrapped in double quotes, using backslash escapes.
Within a string, use the backslash character to embed "special" characters:
\" a literal quotation mark,\\ a literal backslash,\n, \t, \r, \f, \b
\uAAAA where A is a hex digitTo find the number of characters, use the length function.
$ jq -cn --args '$ARGS.positional[] | length' "Hello world!" "❄🌡🤧🤒🏥🕰😀"
12
7
The slice notation can be used to extract substrings.
The syntax is .[i:j].
The substring returned is of length j - i, returning characters from index i (inclusive) to index j (exclusive).
Either index can be negative, in which case it counts backwards from the end of the string.
Either index can be omitted, in which case it refers to the start or end of the string.
Indexes are zero-based.
"abcdefghij"[3:6] # => "def"
"abcdefghij"[3:] # => "defghij"
"abcdefghij"[:-2] # => "abcdefgh"
Use + to join strings:
"Hello" + " " + "world!"
# => "Hello world!"
Use add when given an array of strings:
["Hello", " ", "world!"] | add
# => "Hello world!"
Splitting uses the split/1 function or the / operator:
"Hello beautiful world!" | split(" ") # => ["Hello", "beautiful", "world!"]
"Hello beautiful world!" / " " # => ["Hello", "beautiful", "world!"]
The inverse of split/1 is join/1.
Converting a string into an array of characters is frequently needed. There are two ways to do this:
split on an empty string
"Hi friend 😀" / "" # => ["H","i"," ","f","r","i","e","n","d"," ","😀"]
explode into an array of codepoints
"Hi friend 😀" | explode # => [72,105,32,102,114,105,101,110,100,32,128512]
implode is the inverse of explode.
Use the index/1 function; the index is zero-based.
"hello" | index("el")' # => 1
If the substring is not in the string, the result is null
"hello" | index("elk") # => null
Within a string, the sequence \(expression) will embed the result of that expression into the string:
"The current datetime is \(now | strflocaltime("%c"))" # => "The current datetime is Wed Nov 16 17:06:33 2022"
Use the ascii_downcase and ascii_upcase functions.
jq has rich support for regular expressions: this will be the topic of a later lesson.
In this exercise you'll be processing log entries.
Each log line is a string formatted as follows: "[<LEVEL>]: <MESSAGE>".
There are three different log levels:
INFOWARNINGERRORYou have three tasks, each of which will take a log line and ask you to do something with it. There are lots of ways to solve these tasks - choose your favourite techniques from the examples above and see what you can come up with.
Implement the message function to return a log line's message:
"[ERROR]: Invalid operation" | message
# => "Invalid operation"
Any leading or trailing white space should be removed:
"[WARNING]: Disk almost full\r\n" | message
# => "Disk almost full"
Implement the log_level function to return a log line's log level, which should be returned in lowercase:
"[ERROR]: Invalid operation" | log_level
# => "error"
Implement the reformat function that reformats the log line, putting the message first and the log level after it in parentheses:
"[INFO]: Operation completed" | reformat
# => "Operation completed (info)"
Sign up to Exercism to learn and master jq with 12 concepts75 exercises, and real human mentoring, all for free.