Solved

Non case sensitive searches in Korn Shell

Posted on 2004-09-27
8
299 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 2

Expert Comment

by:jonkreisler
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:elwayisgod
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
You are very welcome, Sam.
That's why we're here; to help when it's needed.

Jon
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

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…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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.

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now