Redirect output to memory instead of an external file

I use the following as an example. I find myself always doing this and I believe my scripts would run much faster if I put the sed results into a place in memory rather than writing to files. Again, its not about sed, its about redirecting to a place in memory rather than an external file.

#!/bin/bash
sed -r 's/\s+/ /g' vts.txt > vts2
while read x y z
do 
echo $y
done < vts2 

Now the following obviously doesnt work, I provide it for illustration

#!/bin/bash
vts2=`sed -r 's/\s+/ /g' vts.txt`
while read x y z
do
 echo $y
done < $vts2

Research into creating a ramdisk for your particular platform.

Try searching these forums first...

Like this?

#!/bin/bash

sed 's/\s+/ /g' vts.txt |
while read x y z; do
    echo $y
done

or do you want a function?

second_col ()
{
   sed 's/\s+/ /g' "$1" |
        while read x y z; do
            echo $y
        done 
}
second_col vts.txt

If I missed the point, ignore it.

That's exactly the reason why pipes were invented...

I hoping this is a vastly simplified example. Did you know that the read built-in will automatically chomp multiple whitespace charactersso you could have simply done:

while read x y z
do
    echo $y
done < vts.txt

But, assuming you are doing some sort of more complex sed or grep the read loop: pipes are the way to go.

Of course if you find yourself doing sed ... | grep ... | sed ... | kitchen-sink it's probably time to consider using a more powerfull text processing command, like awk script.

It seems like one better sed script would suffice ( add 's/^[^ ]+ \(.*\) [^ ]+$/\1/' ), or a better bash script extending the set in $IFS and using just 'while read'.