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

Karl Young karly at kipshouse.org
Tue Apr 11 21:28:56 UTC 2017


Antonio Olivares(olivares14031 at gmail.com)@2017.04.11 15:19:19 -0500:
> 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 formattimestamp.sh` ; do echo "$i " ; date -r "$i" "+%a
> %b %d %Y %I:%M %p"
> 
> the formattimestamp.sh contains items like
> echo '16801 86400 * p' | dc
> echo '16804 86400 * p' | dc
> echo '16808 86400 * p' | dc
> etc..
> 
> 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
> how?
> I have tried piping the output, but cannot get the function to work.

I found this works (on FreeBSD 7.2)

awk  '{  "date -r "$1 | getline $1 ; print $1, $3, $2 }' < input

Tue Apr 11 14:20:13 PDT 2017 awaydata homedata
Tue Apr 11 14:21:13 PDT 2017 moreawaydata morehomedata

-karl

> 
> Thanks,
> 
> 
> Antonio
> _______________________________________________
> freebsd-questions at freebsd.org mailing list
> https://lists.freebsd.org/mailman/listinfo/freebsd-questions
> To unsubscribe, send any mail to "freebsd-questions-unsubscribe at freebsd.org"


More information about the freebsd-questions mailing list