Solved

Non case sensitive searches in Korn Shell

Posted on 2004-09-27
8
328 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
CPU monthly average? 12 133
auto mounter on solaris 1 69
Unix Question 19 50
check unix curl command return value 7 154
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…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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…

820 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