Script to compare 2 words (first and last letter only)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to compare 2 words (first and last letter only)
# 1  
Old 12-14-2010
Script to compare 2 words (first and last letter only)

Hello,
I need a script to do the following:
I have a file filled of lines like:

valu -> value
confirmaton -> confirmation

I need a script to compare the first and last letters of the words, for example for the line:
valu -> value

compare "v" to "v" and "u" to "e" and print the line if the letters are different
# 2  
Old 12-14-2010
This link will help u.
R0H0N
# 3  
Old 12-14-2010
Bug try this..

Code:
echo "Printing Unmatched lines ..."
echo "---------------------------------"
while read line
do
  fst1=`echo $line|cut -c 1`
  lst2=`echo $line|sed -e "s/^.*\(.\)$/\1/"`
  fst2=`echo $line|awk -F" " '{print $3}'|cut -c 1`
  lst1=`echo $line|awk -F" " '{print $1}'|sed -e "s/^.*\(.\)$/\1/"`
  if [ $fst1 != $fst2 ] || [ $lst1 != $lst2 ]; then
    echo $line
  else
    echo "Great !! Variables matched for this line"
  fi
done < datafile
$ cat datafile
valu -> value
confirmaton -> confirmation


Try this code...

Last edited by Franklin52; 12-14-2010 at 04:33 AM.. Reason: Please use code tags and indent your code, thanks
# 4  
Old 12-14-2010
Try:
Code:
perl -n -e '/^\s*(\w)\w*(\w)\s*->\s*(\w)\w*(\w)\s*$/;print if ( $1 ne $3 || $2 ne $4) ;' infile

# 5  
Old 12-14-2010
Code:
gawk -F "->" 'function fe(n) {return gensub(/(.).*(.)/,"\\1\\2","g",n)}
{a=$0;gsub(/ /,""); if (fe($1)!=fe($2)) print a}' infile

# 6  
Old 12-14-2010
Code:
 
cat fileName | grep -v "\(.\).*\(.\) -> \1.*\2"

This will print all lines in file where 1st to 1st AND last to last characters of 1st and 2nd words (separated by [ -> ]) doesn't match.
# 7  
Old 12-14-2010
Quote:
Originally Posted by anurag.singh
Code:
 
cat fileName | grep -v "\(.\).*\(.\) -> \1.*\2"

This will print all lines in file where 1st to 1st AND last to last characters of 1st and 2nd words (separated by [ -> ]) doesn't match.
Very good thinking, but I think below change is required for exact pattern matching.

Code:
 cat fileName | grep -v "^\(.\).*\(.\) -> \1.*\2$"

R0H0N
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove repeated letter words

Hi, I have this text file with these words and I need help with removing words with repeated letter from these lines. 1 ama 5 bib 29 bob 2 bub 5 civic 2 dad 10 deed 1 denned 335 did 1 eeee 1 eeeee 2 eke 8... (4 Replies)
Discussion started by: crepe6
4 Replies

2. Shell Programming and Scripting

Replace specific letter in a file by other letter

Good afternoon all, I want to ask how to change some letter in my file with other letter in spesific line eg. data.txt 1 1 1 0 0 0 0 for example i want to change the 4th line with character 1. How could I do it by SED or AWK. I have tried to run this code but actually did not... (3 Replies)
Discussion started by: weslyarfan
3 Replies

3. Shell Programming and Scripting

Counting all words that start with a capital letter in a string using python dictionary

Hi, I have written the following python snippet to store the capital letter starting words into a dictionary as key and no of its appearances as a value in this dictionary against the key. #!/usr/bin/env python import sys import re hash = {} # initialize an empty dictinonary for line in... (1 Reply)
Discussion started by: royalibrahim
1 Replies

4. UNIX for Dummies Questions & Answers

Delete all words not containing letter /s/

I have a word file that looks like: pens binder spiral user I want to delete all the words without the letter /s/, so output looks like: pens spiral user I tried using sed: sed '//d' infile.txt > out.txt (5 Replies)
Discussion started by: pxalpine
5 Replies

5. Shell Programming and Scripting

Make all words begin with capital letter?

I need to use bash to convert sentences where all words start with a small letter into one where all words start with a capital letter. So that a string like: are utilities ready for hurricane sandy becomes: Are Utilities Ready For Hurricane Sandy (10 Replies)
Discussion started by: locoroco
10 Replies

6. Shell Programming and Scripting

Color Underline and Bold letter in shell script

Hi All, This is my first port..... I am using AIX 5L, installed 10g database. On daily basis we takes rman backup. This backup status info strored in a log file. I write a script to know the status of back means I will fire this script and this script will send a mail to me. #!/bin/bash... (16 Replies)
Discussion started by: mcagaurav
16 Replies

7. 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

8. Shell Programming and Scripting

compare strings, words in different order

Hi, Would anyone know how to compare two strings, and only throw an error if there were different words, not that the same words were in a different order? e.g "A B C" vs "B C A" ->OK "A B C" vs "A D C" -> BAD Thanks! (2 Replies)
Discussion started by: rebelbuttmunch
2 Replies

9. UNIX for Advanced & Expert Users

How to filter the words, if that word contains the expected letter

Hi, I am trying to filter the words from a file which contain 'abc'. But I am unable to. Could any one help me. For eg: The file contents are 123ab 12hnj1 123abc456 123cgbcahjkf23 23134abchfhj43 gc32abc abc1 2abc3 sd uiguif fhwe 21242 uh123 jkcas124d123 u3hdbh23u ffsd8 Output... (3 Replies)
Discussion started by: venu_eie
3 Replies

10. Shell Programming and Scripting

how to find capital letter names in a file without finding words at start of sentence

Hi, I want to be able to list all the names in a file which begin with a capital letter, but I don't want it to list words that begin a new sentence. Is there any way round this? Thanks for your help. (1 Reply)
Discussion started by: kev269
1 Replies
Login or Register to Ask a Question