Comparing text in unix script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Comparing text in unix script
# 1  
Old 09-24-2010
Comparing text in unix script

Hi Gurus,

I need to compare text with a variable in my script but I want to make a comparison which is not case sensitive. For eg:

Code:
if [ $ins = insert ].......
if [ $ins = Insert ].......
if [ $ins = insErt ].......
if [ $ins = INSERT ].......
if [ $ins = iNseRt ].......


all of the above should be treated as equal....case insensitive.....

Last edited by Scott; 09-24-2010 at 08:28 AM.. Reason: Code tags, please...
# 2  
Old 09-24-2010
Code:
newins=$(echo $ins |tr [:upper:] [:lower:])

then compare newins with "insert" only
# 3  
Old 09-24-2010
bash 4.1

Feature in bash (i didn't know it till few weeks ago)
From: GNU Bash-4.1 manual 2009 December 29
Code:
${parameter^pattern}
${parameter^^pattern}
${parameter,pattern}
${parameter,,pattern}
   Case  modification.   This  expansion modifies the case of alphabetic characters in parameter.  The pattern is expanded to produce a pattern just as in pathname expansion.
   The ^ operator converts lowercase letters matching pattern to uppercase;
   The  , operator converts matching uppercase letters to lowercase.  The ^^ and ,, expansions convert each matched character in the expanded value; 
the ^ and , expansions match and convert only the first character in the expanded value..  If  pattern  is  omitted,  it  is treated like a ?, which matches every character.  
If parameter is @ or *, the case modification operation is applied to each positional parameter in turn, and the expansion is the resultant list.  
If  parameter  is  an array  variable  subscripted  with @ or *, the case modification operation is applied to each member of the array in turn, and the expansion is the resultant list.

so this should work:
Code:
if [ ${ins^^} = INSERT ].......

# 4  
Old 09-24-2010
Try...
Code:
case "$ins" in
     [Ii][Nn][Ss][Ee][Rr][Tt]) echo true ;;
     *) echo false ;;
esac

# 5  
Old 09-24-2010
ksh only:
ksh code:
  1. typeset -l ins
  2. if [[ $ins = insert ]] ...
# 6  
Old 09-24-2010
Code:
#!/bin/bash
a=insert
b=Insert
c=insErt
d=INSERT
e=iNseRt
 
for i in $a $b $c $d $e
   do
     if  ( echo $i | grep -i INSERT >/dev/null  ) ; then
       echo Ok
     fi
done

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How can i run sql queries from UNIX shell script and retrieve data into text docs of UNIX?

Please share the doc asap as very urgently required. (1 Reply)
Discussion started by: 24ajay
1 Replies

2. Shell Programming and Scripting

Need Help in comparing 2 text files in shell script

Hi All, I have 2 files like below vi f1 frog elephant rabit zebra dog vi f2 rabit dog ============== Now i want to comapre two files and the result will be frog (8 Replies)
Discussion started by: kumar85shiv
8 Replies

3. Shell Programming and Scripting

Script for Comparing directories and file from a text file

Hello all, I need to write a script which has following requirement: Need to read the filenames from text file and then search for the above read files in the required directory and if match found backup them in a backup folder. And also need to compare and verify whether the files in the... (7 Replies)
Discussion started by: saurau
7 Replies

4. Shell Programming and Scripting

Help with Comparing 2 strings from text

Hey guys how do I compare 2 strings from the text file, and check for duplication? For example, I add an item call Laptop, it will record to the textfile call file. If it detects duplicate it will say the record record exist? file.txt contains Laptop:Sony:1000 Phone:Apple:30 A head... (4 Replies)
Discussion started by: aLHaNz
4 Replies

5. Shell Programming and Scripting

Comparing 2 huge text files

I have this 2 files: k5login sanwar@systems.nyfix.com jjamnik@systems.nyfix.com nisha@SYSTEMS.NYFIX.COM rdpena@SYSTEMS.NYFIX.COM service/backups-ora@SYSTEMS.NYFIX.COM ivanr@SYSTEMS.NYFIX.COM nasapova@SYSTEMS.NYFIX.COM tpulay@SYSTEMS.NYFIX.COM rsueno@SYSTEMS.NYFIX.COM... (11 Replies)
Discussion started by: linuxgeek
11 Replies

6. Shell Programming and Scripting

comparing to text files

Hi All, I have two files of the following formats file 1 - this is a big file >AB_1 gi|229194403|ref|ZP_04321208.1| group II intron reverse transcriptase/maturase gdfjafhlkhlnlklaklskckcfhhahgfahajfkkallalfafafa >AB_2 gi|229194404|ref|ZP_04321209.1| gfksjgfkjsfjslfslfslhf >AB_3... (1 Reply)
Discussion started by: Lucky Ali
1 Replies

7. Shell Programming and Scripting

Execute unix shell script to text file using the script

Hi all, I am beginner in UNIX...I want to use unix shell script to create text.file...I know how to use using by command...can anybody tell me for the script? Thanks i changed the threads title from "tex file" to "text file", because "tex" would probably be misunderstood as reference to... (4 Replies)
Discussion started by: mastercar
4 Replies

8. AIX

comparing within text files

hi! some looping problem here... i have a 2-column text file 4835021 20060903FAL0132006 4835021 20060904FAL0132006 4835021 20060905FAL0132006 4835023 20060903FAL0132006 4835023 20061001HAL0132006 4835023 ... (3 Replies)
Discussion started by: d3ck_tm
3 Replies

9. UNIX for Dummies Questions & Answers

comparing text files

I am comparing text files where there are number of rows of numbers from window to unix box Is there any way of checking lets say 4 document of text file and seeing the difference only (or missing rows of numbers) with simple commands with lets say a batch file FROM ABSOULTE... (2 Replies)
Discussion started by: sjumma
2 Replies
Login or Register to Ask a Question