Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 218
  • Last Modified:

Search in linux

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
PALKTA
Asked:
PALKTA
1 Solution
 
akshayxxCommented:
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
 
akshayxxCommented:
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
 
sebbbCommented:
are u waiting a shell-script ?
a C program ?

do you fill it's not better ?
# filter <file> <range> <string>
0
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!

 
akshayxxCommented:
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
 
akshayxxCommented:
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
 
jmcgOwnerCommented:
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
 
sebbbCommented:
there is a other shell-script
# cat <file> | grep <string>

but i dont know how include the <range> parameter...
0
 
manpreets7Commented:
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
 
sebbbCommented:
there is a other shell-script
# cat <file> | grep <string>

but i dont know how include the <range> parameter...
0
 
wide_awakeCommented:
$ 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now