awk saves pipes

awk is a powerful tool, but unfortunately I'm yet to learn it, so typically just pipe output to sed to perform substitutions.

Considering the following input:

input='ls: cannot access /home/aws/sshfs/i-3abab67e.log: No such file or directory'

Pipe to sed for substitution:

echo $input | awk '{print$4}' | sed 's|/home/aws/sshfs/||;s|.log:||;'

Or do it right inside of awk:

echo $input | awk '{gsub("/home/aws/sshfs/", ""); gsub(".log:", ""); print$4}'

Though it does take a few more characters, the latter saves a pipe and that feels like the right thing to do..

Leave a comment

NOTE: Enclose quotes in <blockquote></blockquote>. Enclose code in <pre lang="LANG"></pre> (where LANG is one of these).