I'd like to use sed
or any command line tool to replace parts of lines by the output of shell commands. For example:
- Replace linux epochs by human-readable timestamps, by calling
date
- Replace hexa dumps of a specific protocol packets by their decoded counterparts, by calling an in-house decoder
sed
seems best fitted because it allows to match patterns and reformat other things too, like moving bits of matches around, but is not mandatory.
Here is a simplified example:
echo "timestamp = 1234567890" | sed "s/timestamp = \(.*\)/timestamp = $(date -u --d @\1 "+%Y-%m-%d %T")/g"
Of course, the $(...)
thing does not work. As far as I understand, that's for environment variables.
So what would the proper syntax be? Is sed
recommended in this case ? I've spent several hours searching... Is sed
even capable of this ? Are there other tools better suited?
Edit
I need...
- Pattern matching. The log is full of other things, so I need to be able to pinpoint the strings I want to replace based on context (text before and after, on the same line). This excludes column-position-based matching like
awk '{$3
... - In-place replacement, so that the reste of the line, "Timestamp = " or whatever, remains unchanged. This exclused sed's 'e' command.