Solved

Non case sensitive searches in Korn Shell

Posted on 2004-09-27
8
339 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Unix/bash: scripted arithmetic 13 103
MarkLogic 1 117
Access_log 17 183
SSH to Solaris server (which uses NIS) 1 50
A metadevice consists of one or more devices (slices). It can be expanded by adding slices. Then, it can be grown to fill a larger space while the file system is in use. However, not all UNIX file systems (UFS) can be expanded this way. The conca…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
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.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Suggested Courses

734 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