从文本文件打印所有回文 [英] Printing all palindromes from text file

查看:94
本文介绍了从文本文件打印所有回文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看了这个问题: BASH Palindrome Checker .这是问题从该线程显示的内容:

I looked at this question: BASH Palindrome Checker. This is what the question answer shows from that thread:

grep -E '^[a-z]{3,45}$' /usr/share/dict/words | while read -r word;
    do [ $word == `echo $word | rev` ] && echo $word;
done;

我知道这是从单词"中读取内容,但是尝试将其修改为从文本文件而不是/usr/share/dict/words中读取时遇到了麻烦.我想让它读取我请求的任何文本文件,所以我可以输入:

I understand that this is reading from "words" but have trouble trying to modify it to read from a text file instead of /usr/share/dict/words. I want to have it to read any text file I request, so I can put:

source palindrome *filename*

,这将打印出在控制台文件中找到的回文.稍后再拥有它,以便我可以输出到输出文件:

and this will print out the palindromes found in the file in the console. Also later to have it so I can output to a output file:

source palindrome *filename* >> output.txt

我已经尝试执行此操作,但是它不起作用,我真的不确定要更改它才能读取文件:

I have tried to do this but it doesn't work and I am really not sure what I have to change to get it to read my file:

#!usr/bin/bash

function search
{
    filename=$1

    grep -E '^[a-z]{3,45}$' "$filename" | while read -r word;
        do [ $word == `echo $word | rev` ] && echo $word;
    done;
}
search $1

如果提供任何解决方案,它们的格式是否相似?我还没有学到太多其他技术.如果给出了更复杂的解决方案,您能否解释一下给出的代码.

If any solutions are given could they be in a similar format? I haven't learned too many other techniques yet. If more complicated solutions are given could you explain the code given a little please.

输入文件来自一本电子书,它非常长,因此有一个小片段:(我确实意识到这并没有显示出该片段中的回文,但只是显示了它是哪种文本文件)

The input file is from an eBook, it is extremely long so a small snippet is: (I do realise this doesn't show off palindromes in the snippet but it is just to show what kind of text file it is)

O and that lotion mustn't forget.
Fever near her mouth. Your head it simply. Hair braided over: shell with
seaweed. Why do they hide their ears with seaweed hair?

运行源回文 filename 时,没有错误消息.我按Enter键,终端使我可以再次输入任何内容.看起来好像不是正在通过脚本运行

When running source palindrome filename there is no error message. I press enter and the terminal lets me type anything I want in again. It doesn't look as if it is running through the script

推荐答案

#!/bin/bash

function search
{
    grep -oiE '[a-z]{3,}' "$@" | tr '[:upper:]' '[:lower:]' | while read -r word; do
        [[ $word == $(rev <<< "$word") ]] && echo "$word"
    done
}

search "$@"

为此原始代码编写的字典每行只有一个小写单词.要解析每行包含多个大小写混合单词的文本文件,您需要进行一些修改:

The dictionary this original code was written for had a single lowercase word on each line. To parse a text file with multiple mixed case words per line you need a few modifications:

  • 从正则表达式中删除^$锚,以找到行上任何地方的单词.
  • 使用grep -o打印出匹配的单词.
  • 使用grep -i匹配大写和小写.
  • 使用tr将大写字母切换为小写字母.
  • Remove the ^ and $ anchors from the regex to find words anywhere on a line.
  • Use grep -o to print out the matching words.
  • Use grep -i to match both upper and lowercase.
  • Use tr to switch uppercase letters to lowercase.

其他修复程序:

  • 将shebang行更改为#!/bin/bash.它应该是一条绝对路径,两个首选形式是#!/bin/bash#!/usr/bin/env bash.
  • £word更改为$word.
  • 使用"$@",以便search()可以接受多个文件名.
  • Changed the shebang line to #!/bin/bash. It ought to be an absolute path, and the two preferred forms are either #!/bin/bash or #!/usr/bin/env bash.
  • Changed £word to $word.
  • Use "$@" so search() can accept multiple file names.

改进:

  • 没有特别的理由将单词限制为45个字符. {3,}删除上限.
  • 双括号[[ ]] 比单括号更好 [ ].
  • $(cmd)`cmd`好.
  • rev <<< "$word"echo "$word" | rev好.
  • There's no particular reason to limit words to 45 characters. {3,} removes the upper limit.
  • Double brackets [[ ]] are better than single brackets [ ].
  • $(cmd) is better than `cmd`.
  • rev <<< "$word" is better than echo "$word" | rev.

这篇关于从文本文件打印所有回文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆