Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I have the following input file in.txt:

a
b
c
a
b
c

I have the following script test.sh:

# display unique rows
while read line
do 
  echo $line
done < $(cat "$@" | sort | uniq) 

# display all rows
while read line
do 
  echo $line
done < $(cat "$@") 

I know I can run the script using:

sh test.sh in.txt

I also know I can run it using standard input instead of a file. However, this only supplies input to the first while loop. How can I supply the input to both loops without typing the input twice?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

This script accepts stdin and produces both outputs:

#!/bin/sh

echo "Unique rows:"
tee ~/tmpfile$$ | sort | uniq

echo "All Rows:"
cat ~/tmpfile$$

rm ~/tmpfile$$
share|improve this answer
    
I knew I could do it by using /tmp to save the input. I was wondering whether there was some other way that did not require creation of a temp file. Thanks! –  EggHead Jan 19 '14 at 23:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.