Posts

Showing posts with the label Text Processing

Convert Json Table Arrays To Objects With Jq

Answer : Answering my own question: jq 'to_entries|map(.key) as $keys| (map(.value)|transpose) as $values |$values|map([$keys, .] | transpose| map( {(.[0]): .[1]} ) | add)' Explanation: Extract keys ["IdentifierName", "Code"] and values as [ [ "A", 5 ], [ "B", 8 ], [ "C", 19 ] ] Then to index from keys to values, take json-seq of key-tuple with (each) value tuple and transpose and zip them in pairs. echo '[[ "IdentifierName", "Code" ], [ "C", 19 ] ]'|jq '.|transpose| map( {(.[0]): .[1]} ) | add' Combining both gives solution. This will work for any number of elements (0 and 1 are just key and value, not first and second). $ jq '[.IdentifierName, .Code] | transpose | map( { "IdentifierName": .[0], "Code": .[1] } ) ' file.json [ { "IdentifierName": "A", "Code": 5 }, { "IdentifierN...

Convert Unix Timestamp To Human-readable Time

Answer : If the line starts with the unix timestamp, then this should do it: perl -pe 's/^(\d+)/localtime $1/e' inputfilename perl -p invokes a loop over the expression passed with -e which is executed for each line of the input, and prints the buffer at the end of the loop. The expression uses the substition command s/// to match and capture a sequence of digits at the beginning of each line, and replace it with the local time representation of those digits interpreted as a unix timestamp. /e indicates that the replacement pattern is to be evaluated as an expression. If your AWK is Gawk, you can use strftime : gawk '{ print strftime("%c", $1) }' will convert the timestamp in the first column to the current locale’s default date/time representation. You can use this to transform the content before viewing it: gawk '{ print gensub($1, strftime("%c", $1), 1) }' inputfile As Dan mentions in his answer, the Gnu date(1) command can be give...

Country Name Mashup Generator

Answer : Jelly, 74 73 bytes Já¹–XṬk⁸ḢḢFṪ;ƲƭF)jṪḢƭ€á¹€$$ ḢṖ; ṪḢṪ;ÆŠá¹­ Fe€Ã˜cá¹–TXṬkḢḢṪƭ) e€⁾ -k)ẈỊḄ‘ƲĿ Ḣ,2KƊÇE? Try it online! A full program that takes a list of two strings as its argument and implicitly outputs the mashed up country name. The handling of hyphens is relatively costly, particularly since they are included whichever side of the split they fall. Explanation Helper link 1 Handles case where both countries have multiple words ) | For each country: J | - Sequence along words á¹– | - Remove last X | - Pick one at random Ṭ | - Convert to a boolean list with a 1 at that index k⁸ | - Split list of words after that point Æ­ | - Alternate between: Ḣ | - Head (first set of words for the first country) Ʋ | - Following as a monad (for the second country) Ḣ | - Head (f...