Grep command is not working

I have made a program that reads a text file and checks for palindromic words and then outputs them. They each appear on a new line with a count of the number of occurences beside each of the words.

Requirements for being classed as palindrome are that the word must have at least 3 letters and at least 2 of them must be different. For example, eee would not be a palindrome but eye would be.

I am having a problem in that my program is picking up some of the words such as eee and iii. I have added the grep command highlighted below to try and fix this but I keep coming up against these errors. I cannot seem to get it to work.

./palindrome: line 5: syntax error near unexpected token `|'
./palindrome: line 5: `    | grep -P -v '^(.)\1*$'' 

or

usage: grep [-abcDEFGHhIiJLlmnOoqRSsUVvwxZ] [-A num] [-B num] [-C[num]]
	[-e pattern] [-f file] [--binary-files=value] [--color=when]
	[--context[=num]] [--directories=action] [--label] [--line-buffered]
	[--null] [pattern] [file ...]
#!/bin/bash
preprocess() {
  tr -d '[[:punct:][:digit:]@]' \
    | tr '[[:space:]]' '\n' \ 
    | grep -P -v '^(.)\1*$'
}

paste <(preprocess <"$1") <(preprocess <"$1" | rev) \
  | awk '$1 == $2 && (length($1) >= 3) { print $1 }' \
  | sort | uniq -c

Can you explain the real-life application for this script? While posted in a general area, this appears similar to a school project or homework assignment. If it is a school/homework assignment, please repost in that section.

There's no real life application for the script. I am simply trying to learn bash shell scripts and this is an exercise to help learn.

From the error message you're getting from grep , one might guess that you're using a macOS or BSD operating system, but I don't see why you would get a syntax error on line 5 from bash with this code on those systems.

One might also guess that you copied this code from someone who was using a different operating system that has a grep utility that has a -P option. (The 2nd error message you're getting says that the grep utility you're using does not have a -P option.)

So, where did you get this code and what operating system was being used by the person who wrote this code? And, what operating system and shell are you using?

If you change the grep to get rid of the unknown option and convert the RE being used to be one that would be accepted by any standard version of grep :

grep -v '^\(.\)\1*$'

you have a grep command that will delete lines where every character in a word is the same character and there are at least two characters in the word.

Making the huge assumption that the shell you're using understands the <(command) syntax in addition to standard shell file redirections, your script with the above modification seems to do what you want.

I am using mac osx.
The person I got the code was using ubuntu.
I am using the mac terminal for this script.

OK.

So, if you replace the line in your code:

    | grep -P -v '^(.)\1*$'

with:

    | grep -v '^\(.\)\1*$'

does your script do what you wanted it to do?

No it doesn't.
It comes up with this error message.

./palindrome ulysses.txt
./palindrome: line 19: syntax error: unexpected end of file
 

That's strange. When I put the code:

#!/bin/bash
preprocess() {
  tr -d '[[:punct:][:digit:]@]' \
    | tr '[[:space:]]' '\n' \
    | grep -v '^\(.\)\1*$'
}

paste <(preprocess <"$1") <(preprocess <"$1" | rev) \
  | awk '$1 == $2 && (length($1) >= 3) { print $1 }' \
  | sort | uniq -c

in a file named palindrome and make it executable with:

chmod +x palindrome

and create a file named file containing:

aaa aia xyzzyx xxxxxx naan shahs palindromes
aaa aia xyzzyx xxyxxyxx naan shahs toot

and I run the command:

./palindrome file

on macOS Sierra (version 10.12.6), I get the output:

   2 aia
   2 naan
   2 shahs
   1 toot
   1 xxyxxyxx
   2 xyzzyx

I also get exactly the same output if I use ksh instead of bash :

$ ksh palindrome file
   2 aia
   2 naan
   2 shahs
   1 toot
   1 xxyxxyxx
   2 xyzzyx
$