shell script to print words having first and last character same.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting shell script to print words having first and last character same.
# 1  
Old 04-21-2011
shell script to print words having first and last character same.

Hi

I want to write a shell script to print only those words from a file whose beginning
and last character are same.
Please help.

Thanks,
vini
# 2  
Old 04-21-2011
What have you tried? Is this homework?
# 3  
Old 04-21-2011
The script I have tried is:
Quote:
if [ $# -ne 1 ]
then
echo "Please enter a filename as argument"
exit 1
fi


cat $1 | while read TEXT
do
LEN=${#TEXT}
firstchar = `echo ${#TEXT} | cut -c 1`
lastchar = `echo ${#TEXT} | rev | cut -c 1`
if($firstchar -eq $lastchar)
echo ${#TEXT}
done
# 4  
Old 04-22-2011
Are you trying to match the first and last character of each line? If so I fixed a few syntax errors you had and so I believe this should work for you now.

Code:
#!/bin/ksh
 
if [ $# -ne 1 ]
then
    echo "Please enter a filename as argument"
    exit 1
fi

cat $1 | while read TEXT
do
    LEN=${#TEXT}
    # Removed '#' because we want to compare the character
    #   not the length of the text
    firstchar=`echo ${TEXT} | cut -c 1`
    lastchar=`echo ${TEXT} | rev | cut -c 1`
 
    # Fixed the syntax for 'if' statement and changed 'eq' to '='
    if [ "$firstchar" = "$lastchar" ]
    then
        echo ${#TEXT}
    fi
done

Enjoy.
This User Gave Thanks to 2pugs For This Post:
# 5  
Old 04-22-2011
A few points to fix in your script:
- Remove LEN=... (it's not used in your script)
- Assignation does not take [space] before and after =
firstchar and lastchar are wrong (you use the lengh of the text (#), not the text itself)
- If you use bash, you can use:
Code:
firstchar=$(echo ${TEXT:0:1})
lastchar=$(echo ${TEXT:(-1)})

- The test is wrong: -eq, -lt, ... are used only for arithmetic purpose, you want to compare strings:
Code:
if [ "$firstchar" = "$lastchar" ]; then
    echo "$TEXT"
fi

This User Gave Thanks to tukuyomi For This Post:
# 6  
Old 04-22-2011
Code:
awk -F "" '$1==$NF' RS="[ \n]" infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Stemming of words that contained affixes by using shell script

I just learning shell script. Need your shell script expertise to help me. I would like to stemming the words by matching the root words first between both files and replace all words by "I" character but replace "B" character after root words and "E" before root words in affix_words.txt. ... (18 Replies)
Discussion started by: paranrat
18 Replies

2. UNIX for Advanced & Expert Users

Shell script to convert words to Title case

Hi :) I have a .txt file with thousands of words. I was wondering if i could use a simple sed or awk command to convert / replace all words in the text file to Title Case format ? Example: from: this is line one this is line two this is line three to desired output: This Is Line... (8 Replies)
Discussion started by: martinsmith
8 Replies

3. Shell Programming and Scripting

Shell script to read words into an array

Hello, I have output like below: ----------------------------------------------------------------------------- Group 'group1' on system 'system01' is running. ----------------------------------------------------------------------------- Group 'group2' on system 'system01' is running.... (4 Replies)
Discussion started by: sniper57
4 Replies

4. Shell Programming and Scripting

How to remove words that contain 3+ of the same character in a row?

Hello, I am looking for a way to remove words from a list that contain 3 or more of the same character. For example lets say the full list is as follows ABCDEF ABBHJK AAAHJD KKPPPP NAUJKS AAAHJD & KKPPPP should be removed from this list as obviously they contain AAA and PPPP... (7 Replies)
Discussion started by: colinireland
7 Replies

5. Shell Programming and Scripting

[AWK script]Counting the character in record and print them in condition

.......... (1 Reply)
Discussion started by: Antonlee
1 Replies

6. Shell Programming and Scripting

Shell script to find out words, replace them and count words

hello, i 'd like your help about a bash script which: 1. finds inside the html file (it is attached with my post) the code number of the Latest Stable Kernel, 2.finds the link which leads to the download location of the Latest Stable Kernel version, (the right link should lead to the file... (3 Replies)
Discussion started by: alex83
3 Replies

7. Shell Programming and Scripting

how to shift few words of filenames at a time using shell script

Hello everybody, I have some files in directory. I want to shift 3 characters of filenames to the right at a same time. for example, I have filenames like $ls -l 01_2000.G3.input.txt 02_2000.G3.input.txt ..., ..., 04_2010.G3.input.txt I want to change the filenames like... (3 Replies)
Discussion started by: yogeshkumkar
3 Replies

8. Shell Programming and Scripting

read the text file and print the content character by character..

hello all i request you to give the solution for the following problem.. I want read the text file.and print the contents character by character..like if the text file contains google means..i want to print g go goo goog googl google like this Using unix Shell scripting... without using... (1 Reply)
Discussion started by: samupnl
1 Replies

9. Shell Programming and Scripting

how to retrieve the word between 2 specific words with shell script

Hi, I have a file content like: : : <span class="ColorRed"> 1.23</span><br> : : the value 1.23 will be changed from time to time, and I want to use a shell script command, e.g grep or sed, to retrieve only the value, how to do it? Thanks! Victor (6 Replies)
Discussion started by: victorcheung
6 Replies

10. Shell Programming and Scripting

Need to change two words in a line using shell script.

Hi, i have a line tftp dgram udp wait nobody /usr/sbin/tcpd in.tftpd /tftpboott in /etc/inet.conf file. ineed to replace nobody with root and /tftpboott with /flx/boot. i tried using sed ,but i could not change both of them. can you please help me to do this. Edit:... (7 Replies)
Discussion started by: vprasads
7 Replies
Login or Register to Ask a Question