?
Solved

Search in linux

Posted on 2003-03-11
10
Medium Priority
?
217 Views
Last Modified: 2010-04-21
I need some help on starting this code on the linux server!!

I need it to read a text file and search for a specific word.

The user would enter which word is to be serarched from what file and the line numbers to search through

typed in on the command line would be

filter 10 20 and file1

the above command searches in "file1" from the lines 10 to 20 for the word 'and'.

The output would be that each line from 10 to 20 which contains the word 'and' would be printed out!!

This is very hard for me as i do not have much understanding of this!!

Thanking you in advance
0
Comment
Question by:PALKTA
[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
10 Comments
 
LVL 8

Expert Comment

by:akshayxx
ID: 8110096
if u can confirm this that its not a home assignment .. then we can help u out ..
also
whats the expected maximum size of ur input file
like it wonr be larger than 1 mb .. or can it be of the order of GB .. or is it simple text file within few KBS
that will help in suggesting the efficient solution
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8110101
Also if u have idea that any line of ur input file wont go beyond certain length . .. then also its helpful to suggest u exact and easy to implement/understand solution..
0
 

Expert Comment

by:sebbb
ID: 8110586
are u waiting a shell-script ?
a C program ?

do you fill it's not better ?
# filter <file> <range> <string>
0
Stack Overflow Podcast - Developer Story

Welcome to the Stack Overflow podcast recorded Thursday July 20 at Stack Overflow Headquearters in NYC. Your hosts today are podcast regulars Jay Hanlon, David Fullerton, and Ilana Yitzhaki, plus the quite irregular Matt Sherman (Stack Overflow Engineering Manager extraordinaire)

 
LVL 8

Expert Comment

by:akshayxx
ID: 8110616
shell script can only invoke the two programs .. it wont allow the two different programs to share the variables ..
they still will be individual programs..
what he wants is making single program out of his existing two programs..

lets hear from him what exactly does he have .. and what he wants
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8110623
sorry this comment wasnt meant for this question ..
i apologize ..
still  i wud say
lets hear from him what exactly does he have .. and what he wants
0
 
LVL 20

Accepted Solution

by:
jmcg earned 1000 total points
ID: 8125519
Here's a shell script, using 'sed', that might work as your 'filter' program:

#! /bin/ksh

sed -n "$1,$2s/$3/&/p" $4

This confines the search to the line range specified by the first two arguments. It attempts to substitute the word(s) of the third argument with themselves, printing the resulting line when a successful substitution occurs. And the fourth argument is taken for the name of the file to be searched.

Before you turned this loose on ordinary users, you might like to beef it up with some error checking. You'll also find that it currently finds partial word matches.
0
 

Expert Comment

by:sebbb
ID: 8126227
there is a other shell-script
# cat <file> | grep <string>

but i dont know how include the <range> parameter...
0
 

Expert Comment

by:manpreets7
ID: 8126291
The following is simple and will work:
sed -n "$1,$2p" $4 | grep $3

However, this works only when the file (arg 4) is in the current directory. If it can be anywhere down the current directory use the following:

find . -name $4 | xargs sed -n "$1,$2p" | grep $3

This will find the file down the path, look at lines between 'argument 1' and 'argument 2' and search for the word given by 'argument 3'.

PS: If you want to do a case insensitive search for the word, use 'grep -i' in place of 'grep'.
0
 

Expert Comment

by:sebbb
ID: 8126302
there is a other shell-script
# cat <file> | grep <string>

but i dont know how include the <range> parameter...
0
 
LVL 3

Expert Comment

by:wide_awake
ID: 8171961
$ head -20 filename | tail -10 | grep and

That'll do it for you.

$ head -A B | tail -C | grep D

A == last line to search
B == filename to search
C == number of lines above A to start seeking (last - first)
D == pattern to search for.


-Mark.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Question has a verified solution.

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

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

777 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