0

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. :)

  • 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 '15 at 14:55
1
#!/bin/bash

while :
do
  echo Paste some input, then press Control-D:
  cat > /tmp/sortme ; diff <(sort /tmp/sortme) <(sort -u /tmp/sortme)
done
1

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
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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