Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unix map function

Tags:

bash

unix

sh

map

I have an array of values $dates that I'm transforming:

for i in $dates
do
  date -d "1970-01-01 $i sec UTC" '+%a_%D' 
done

Is there a way to save the result of this operation so I can pipe it to something else without writing it to a file on disk?

like image 653
pokerface Avatar asked Jan 25 '11 03:01

pokerface


2 Answers

Since you say "transforming" I'm assuming you mean that you want to capture the output of the loop in a variable. You can even replace the contents of your $dates variable.

dates=$(for i in "$dates"; do date -d "@$i" '+%a_%D'; done)
like image 199
Dennis Williamson Avatar answered Oct 08 '22 22:10

Dennis Williamson


If using bash, you could use an array:

q=0
for i in $dates
do
  DATEARRAY[q]="$(date -d "1970-01-01 $i sec UTC" '+%a_%D')"
  let "q += 1"
done

You can then echo / pipe that array to another program. Note that arrays are bash specific, which means this isn't a portable (well, beyond systems that have bash) solution.

like image 32
Tim Post Avatar answered Oct 09 '22 00:10

Tim Post