Printing all palindromes from text file

grep find palindrome
palindrome in shell script

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;

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?

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

Read Phrases From A File And Print All Palindromes To A Txt File , read phrases from a file and print all palindromes to a txt file. Posted 20 March 2012 - 12:46 PM. A palindrome is a phrase or word which spells the same,  Recommended: Please solve it on " PRACTICE " first, before moving on to the solution. Step 1: Finding all palindromes using modified Manacher’s algorithm: Step 2: Inserting all the found palindromes in a HashMap: Step 3: Printing the distinct palindromes and number of such distinct palindromes:

Your shebang line is wrong. Change:

#!usr/bin/bash

to

#!/usr/bin/bash

or

#!/bin/bash

Identifies and prints out palindromes from a text file, File;. import java.io.FileReader;. import java.io.BufferedReader;. import java.io.​IOException;. public class FindPalindromes. {. public static void main(String args​[])  Here, we are implementing a java program that will read a string and check the palindrome words in string also find the occurrences of words in a given string. A humble request Our website is made possible by displaying online advertisements to our visitors.

How about this (no scripting):

comm -12 <(nl foo) <(rev foo | nl) | cut -f2

Printing all palindromes from text file, I looked at this question: BASH Palindrome Checker. This is what the question answer shows from that thread: grep -E '^[a-z]{3,45}$'  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?

Find and print out all the palindromes in a file Java Program, Print Palindromes in a file Java Program Find and print out all the palindromes in Output: file not found. Case C Input: java PalindromeCounter file1.txt file1.txt: Feel free to copy and paste the latin text above into a text file, and save it as lorem.txt, so that you can run the example code using this file as input. A Python program can read a text file using the built-in open() function.

Program to print all palindromes in a given range, We can run a loop from min to max and check every number for palindrome. If number is palindrome, we can simply print it. C++. filter_none. edit Find and print out all the palindromes in a file, ignoring any strings with numbers in them, and printing them out in alphabetical order. Your program should: 1. Open the file, dealing with possible exceptions. If there is no file name given, print "insufficient arguments given".

Count palindrome words in a sentence, the given condition · Justify the given Text based on the given width of each line Given a string str and the task is to count palindrome words present in the string str. countPalin() function counts the number of palindrome words by extracting every word of the string and passing it to checkPalin() function. print (​count). Brute Force: We check all the numbers from 1 to n whether its decimal representation is palindrome or not. Efficient Approach: We start from 1 and create palindromes of odd digit and even digits up to n. For every number (starting from 1), we append its reverse at end if we need even length palindrome numbers.

Comments
  • "it doesn't work" is not a problem description.
  • Have you error message ?
  • Why do you need to source the bash code if you are just dumping the result to a file? Wouldn't it be better to define as a regular bash/sh script?
  • Cristian Ramon-Cortes - I want to output it to the terminal to begin with so I can check if it works as intended, then I will output it to a file afterwards
  • @Jhonathan, this could be done with awk one-liner (with only clarification of minimum word size processed)
  • Just a couple of questions: 1: with the "$@" so it can accept multiple files, in my code of $1 that would mean it takes one file? 2: With the "{3,}" part, does this mean it accepts words that are ONLY 3 or more long rather than my code than did between 3-45 characters long.
  • 1: That's right, $1 means it only takes one file. 2: It now accepts words of unlimited length rather than limiting them to 45 characters. 3-infinity is better than 3-45.
  • The $* part is nonsense.
  • Why $*is nonsense ? When he call source palindrome *filename* $* can be expanded by a_filename1 another_filename_2 ...
  • So how would that work with filename=$*; grep -E '^[a-z]{3,45}$' "$filename"?