awk, swap elements A and B, pipe a command in place of an element

Antonio Olivares olivares14031 at
Tue Apr 11 20:19:21 UTC 2017

Dear folks,

I would like to create a table using awk and we can do this easily

awk ' { print $2 , '\t'  $1 } ' input_file

However if we have 3 elements, namely a

TimeStamp  Home Away
1                  2       3

We can swap 2 and 3 with above code.  However I want to format $1 with
date -r "$i" "+%a %b %d %Y %I:%M %p" and then swap $2 and $3.  With
regular BSD awk strftime command is not available.  We can use for
command to format the timestamp

for i in `sh` ; do echo "$i " ; date -r "$i" "+%a
%b %d %Y %I:%M %p"

the contains items like
echo '16801 86400 * p' | dc
echo '16804 86400 * p' | dc
echo '16808 86400 * p' | dc

this echos the timestamp and formats it.  But I would need to add
columns 2 and 3 but swapping them into place.  Without installing
gawk, and not using perl can it be done with bsd awk and /bin/sh and
I have tried piping the output, but cannot get the function to work.



More information about the freebsd-questions mailing list