BASH Finding palindromes in a .txt file

后端 未结 1 746
小鲜肉
小鲜肉 2021-01-26 11:16

I have been given a .txt file in which we have to find all the palindromes in the text (must have at least 3 letters and they cant be the same letters e.g. AAA)

it should

相关标签:
1条回答
  • 2021-01-26 11:41
    # modify this to your needs; it should take your input on stdin, and return one word per
    # line on stdout, in the same order if called more than once with the same input.
    preprocess() {
      tr -d '[[:punct:][:digit:]@]' \
        | sed -E -e '/^(.)\1+$/d' \
        | tr -s '[[:space:]]' \
        | tr '[[:space:]]' '\n'
    }
    
    paste <(preprocess <"$1") <(preprocess <"$1" | rev) \
      | awk '$1 == $2 && (length($1) >= 3) { print $1 }' \
      | sort | uniq -c
    

    The critical thing here is to paste together your input file with a stream that has each line from that input file reversed. This gives you two separate columns you can compare.

    0 讨论(0)
提交回复
热议问题