Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Non case sensitive searches in Korn Shell

Posted on 2004-09-27
8
Medium Priority
?
354 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
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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

Independent Software Vendors: 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!

Question has a verified solution.

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

Hello fellow BSD lovers, I've created a patch process for patching openjdk6 for BSD (FreeBSD specifically), although I tried to keep all BSD versions in mind when creating my patch. Welcome to OpenJDK6 on BSD First let me start with a little …
I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
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.:
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
Suggested Courses

971 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