Posts

Showing posts with the label Awk

Converting Hex To Decimal In Awk Or Sed

Answer : Here's a variation on Jonathan's answer: awk $([[ $(awk --version) = GNU* ]] && echo --non-decimal-data) -F, ' BEGIN {OFS = FS} { $6 = sprintf("%d", "0x" substr($4, 11, 4)) $5 = sprintf("%d", "0x" substr($4, 7, 4)) $4 = substr($4, 1, 6) print }' I included a rather contorted way of adding the --non-decimal-data option if it's needed. Edit Just for the heck of it, here's the pure-Bash equivalent: saveIFS=$IFS IFS=, while read -r -a line do printf '%s,%s,%d,%d\n' "${line[*]:0:3}" "${line[3]:0:6}" "0x${line[3]:6:4}" "0x${line[3]:10:4}" done IFS=$saveIFS The "${line[*]:0:3}" (quoted * ) works similarly to AWK's OFS in that it causes Bash's IFS (here a comma) to be inserted between array elements on output. We can take further advantage of that feature by inserting array elements as follows ...

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...