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 a list that i've copy and i want to paste it to the shell to give me just the repeated lines.

1

1

3

2

As i read about bash commands i've maded this:

cat > /tmp/sortme ; diff <(sort /tmp/sortme) <(sort -u /tmp/sortme)

When i write the above command i paste my list and press CTRL+Z to stop cat and it shows me the repeated lines. I don't want to compare files, just pasted input of several rows.

Now to the question: Is there any way to turn that command into script? Because when i try to make it as a script and CTRL+Z stops it.

PS: Please don't laugh. This is my firs time trying. Till now just reading. :)

share|improve this question
1  
Note sort | uniq -d (or -D for all of them) to see duplicated lines. To say "end-of-file" when entering lines in a terminal, type CTRL-D. CTRL-Z is to suspend the current job. –  Stéphane Chazelas May 29 at 14:55

2 Answers 2

up vote 1 down vote accepted
#!/bin/bash

while :
do
  echo Paste some input, then press Control-D:
  cat > /tmp/sortme ; diff <(sort /tmp/sortme) <(sort -u /tmp/sortme)
done
share|improve this answer
    
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. –  hildred Jun 2 at 2:03
1  
@hildred Er. What? –  Michael Mrozek Jun 2 at 2:24

The first issue is the control Z, in a standard setup, it suspends the job, leaving it in the background not running. You will notice this with exit, which will tell you you have stopped jobs. (other job control commands may also) What you wanted was Control D which when typed at a terminal sends an End of File. This is very different from dos and windows.

Secondly, when you see cat in a script 90% of the time it is not needed. There are three ways to do what you are trying to do without the use of cat that come to the top of my head:

  1. tee tee /tmp/sortme | sort | diff - <(sort -u /tmp/sortme) you can also replace the temporary file with a named link.

  2. using uniq -d sort | uniq -d

  3. xclip which allows you to access the clipboard from the command line so you do not have to send a EOF. It can also be combined with the two previous options so that all four of these lines will do what you want.

    xclip -o > /tmp/sortme ; diff <(sort /tmp/sortme) <(sort -u /tmp/sortme)
    diff <(xclip -o|sort) <(xclip -o|sort -u)
    xclip -o|tee  /tmp/sortme | sort | diff - <(sort -u /tmp/sortme)
    xclip -o|sort | uniq -d
    
share|improve this answer

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.