Solved

Non case sensitive searches in Korn Shell

Posted on 2004-09-27
8
347 Views
Last Modified: 2010-04-21
I have a tab delimited file that has the following line in it:

Tires   $69.95   InStock

!#/usr/bin/ksh

echo "Enter the item you wish to search for: \n"
      read item

echo "Enter the file to be used: \n"
      read file

echo "Searching for $item from file $file"

grep "$item" $file


I need to to accept upper or lower case on first column, then out put should be a combination of the three fields:

The filed tires you searched for is InStock and costs $69.95.

Thanks.
0
Comment
Question by:elwayisgod
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 12166619
Hi elwayisgod,

use -i option with grep

-i, --ignore-case
Ignore case distinctions in both the PATTERN and the input files

Sunnycoder
0
 
LVL 2

Expert Comment

by:jonkreisler
ID: 12172468
How about:

found=`grep -i $item $file`
if [ "$found" != "" ]
then
 echo $found |awk '  {printf "The field " $1 " you searched for is " $3 " and costs " $2 "\n"}'
else
 echo "The field" $item "was not found"
fi
0
 

Author Comment

by:elwayisgod
ID: 12175921
That worked perfectly. Just had to put a space after the | before the awk.  Now If I have the file data.hw4 and I need to delete a item from it and re-create it, I get stuck.  Not sure to use awk etc.. Can't find a delet row....Any help is appreciated.

Sam
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 2

Expert Comment

by:jonkreisler
ID: 12180252
Sorry about the typo, I was typing quickly.

The "v" option of grep will allow you to find all lines EXCEPT those containing string.
So, if the item you want to delete is in the variable "item"...

grep  -iv   $item   data.hw4   >some.temporary.file

(safer to put the output in some other file and then move it back later than to output directly to the same file.)
0
 

Author Comment

by:elwayisgod
ID: 12181618
Here's my code: It deletes my data file, which is a problem.  

#!/bin/ksh

echo "Enter the source data file you wish to manipulate: \n"
      read filename

echo "Enter the destination data file you wish to create: \n"
      read newfile

echo "What is the product you want to delete: \n"
      read product

grep -iv $product $filename >$newfile

echo "New database contains the follwing items: \n"
cat $newfile
0
 
LVL 2

Accepted Solution

by:
jonkreisler earned 250 total points
ID: 12181987
The only way I can see that happening is if both "filename" and "newfile" are identical.

after "read newfile":

if [ "$filename" = "$newfile" ]
then
 echo "source and destination files cannot be the same file!"
 exit
fi
0
 

Author Comment

by:elwayisgod
ID: 12182641
It works great. Thanks. I'm just learning this and get stuck. I'm a damn Accountant for god sakes :)

I appreciate it.

Sam
elwayisgod@hotmail.com
0
 
LVL 2

Expert Comment

by:jonkreisler
ID: 12183359
You are very welcome, Sam.
That's why we're here; to help when it's needed.

Jon
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Suggested Courses

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question