pokerface
pokerface

Reputation: 173

unix map function

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?

Upvotes: 11

Views: 13390

Answers (6)

Dennis Williamson
Dennis Williamson

Reputation: 360153

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)

Upvotes: 20

user50049
user50049

Reputation:

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.

Upvotes: 2

Jé Queue
Jé Queue

Reputation: 10637

Edit, didn't see the whole file thing:

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

Upvotes: 1

Oswald
Oswald

Reputation: 31655

Create a function:

foo () {
        for i in $@
        do
                date -d "1970-01-01 $i sec UTC" '+%a_%D'
        done
}

Then you can e.g. send the output to standard error:

echo `foo $dates` >&2

Upvotes: 7

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798764

Your question is a bit vague, but the following may work:

for ...
do
 ...
done | ...

Upvotes: 4

EmeryBerger
EmeryBerger

Reputation: 3966

You could write it to a FIFO -- a "named pipe" that looks like a file.

Wikipedia has a decent example of its use: http://en.wikipedia.org/wiki/Named_pipe

Upvotes: 1

Related Questions