Robert John Martin
Robert John Martin

Reputation: 41

How to print the remaining columns using awk?

Right now I have a command that prints my log file with a delimited | per column.

cat ambari-alerts.log | awk -F '[ ]' '{print $1 "|" $2 "|" $3 "|" $4 "|" $5 "|"}' |
grep "$(date +"%Y-%m-%d")"

Sample of the log file data is this:

2016-02-11 09:40:33,875 [OK] [MAPREDUCE2] [mapreduce_history_server_rpc_latency] (History Server RPC Latency) Average Queue Time:[0.0], Average Processing Time:[0.0]

The result of my command is this:

2016-02-11|09:40:33,875|[OK]|[MAPREDUCE2]|[mapreduce_history_server_rpc_latency]

I want to print the remaining columns. How can I do that? I tried this syntax adding $0, but unfortunately it just prints the whole line again.

awk -F '[ ]' '{print $1 "|" $2 "|" $3 "|" $4 "|" $5 "|" $0}'

Hope you can help me, newbie here in using awk.

Upvotes: 0

Views: 298

Answers (3)

Ed Morton
Ed Morton

Reputation: 204558

This seems to be all you need:

$ awk '{for (i=1;i<=5;i++) sub(/ /,"|")} 1' file
2016-02-11|09:40:33,875|[OK]|[MAPREDUCE2]|[mapreduce_history_server_rpc_latency]|(History Server RPC Latency) Average Queue Time:[0.0], Average Processing Time:[0.0]

Upvotes: 3

Greg Tarsa
Greg Tarsa

Reputation: 1642

Here is some awk that provides a somewhat more generalized approach than brute-forcing the first 5 columns:

awk '{
      for (i = 1; i < 6; i++)
        printf "%s|", $i
      for (i = 6; i < NF; i++)
        printf " %s ", $i
      }' ambari-alerts.log | grep "$(date +"%Y-%m-%d")"

Upvotes: 0

glenn jackman
glenn jackman

Reputation: 247200

This is a bit of a hassle with awk

awk -F '[ ]' '{
    printf "%s|%s|%s|%s|%s|", $1, $2, $3, $4, $5
    for (i=6; i<=NF; i++) printf "%s ", $i
    print ""
}'

or, replace the first 5 spaces:

awk -F '[ ]' '{
    sub(/ /, "|");sub(/ /, "|");sub(/ /, "|");sub(/ /, "|");sub(/ /, "|")
    print
}'

This is actually easier in bash

while IFS=" " read -r a b c d e rest; do
    echo "$a|$b|$c|$d|$e|$rest"
done < file.log

Folding in your grep:

awk -F '[ ]' -v date="$(date +%Y-%m-%d)" '{
    $0 ~ date {
        printf "%s|%s|%s|%s|%s|", $1, $2, $3, $4, $5
        for (i=6; i<=NF; i++) printf "%s ", $i
        print ""
    }
}'

Upvotes: 0

Related Questions