473,839 Members | 1,494 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Script that searches for text file

3 New Member
I am very new to UNIX. I need a script that searches for a text file with most occurrences of a given keyword.
May 11 '07
17 6592
prn
254 Recognized Expert Contributor
Yikes! Homework question!
Yep. That's what it looks like, but OTOH, it's long expired by now so we can have some fun with it. I found Ghostdog's suggestion interesting and challenging and he pointed out a big flaw in my first try and ...

So maybe it's not so bad, especially not anymore.

JHMO,
Paul
May 23 '07 #11
ghostdog74
511 Recognized Expert Contributor
i think its because in the awk script that i wrote, the count variable is not reset once the next file is processed. So i think it accumulated. I had originally written the script to count all occurrences of the search pattern for all *.sh files as total ( while i think OP only wants to count 1 file ?) anyway... "count" variable should be reset to get the correct output. hope it clarifies a bit.
May 23 '07 #12
Motoma
3,237 Recognized Expert Specialist
Yep. That's what it looks like, but OTOH, it's long expired by now so we can have some fun with it. I found Ghostdog's suggestion interesting and challenging and he pointed out a big flaw in my first try and ...

So maybe it's not so bad, especially not anymore.

JHMO,
Paul
Fair enough; however, this is a very common homework question for an Intro to Unix professor to ask. I may end up deleting the thread on the principal of forcing people to actually learn Linux (instead of just feigning it).

Anyway, I'm interested in how this concludes, so finish up the discussion and I will probably bump this off at a later time.
May 23 '07 #13
Motoma
3,237 Recognized Expert Specialist
grep -c counts only matching lines with the search word, but it will not count number of the same word that appears on a line..
eg
Expand|Select|Wrap|Line Numbers
  1. this is a line but this is last part of the line.
  2.  
grep -c "line" "file"
will give count of 1, not 2...
You could always sed the file first, replacing all spaces with newlines.
*shrug*
Just a thought.
May 23 '07 #14
prn
254 Recognized Expert Contributor
I imagine it is something like that, but what puzzles me is why the numbers don't keep increasing in that case. I get numbers going up and down all over the place.

While I'm back and at it, I decided to take the hint in the original (presumably HW) statement of the problem and have:
Expand|Select|Wrap|Line Numbers
  1. for f in $1/*; do c=`grep -wo $2 $f | wc -l`; echo "$c    $f" ;done | sort -nr | head -1 | cut -f2
Not too bad. :)

Paul
May 23 '07 #15
bhumikas
13 New Member
Hi,
I am new to this Perl.
I need a script which searches for a keyword in a Directory which contains the subdirectories. ..The subdirectories will contain the logfiles.
I need to search the logfiles for a keyword and its count in each logfile if found.
I need to display the filename along with th count.
Hope someone will help me....
Thanks in Advance.......
I tried the above perl script but its says read permission denied....
can u plzzzz gudie meeeeeeee
Aug 10 '07 #16
bhumikas
13 New Member
its working if I copy that script in the desired directory and run it.
I need to run for all the sub directories.... Also I need the output in a textfile becoz when it runs,I can only see the results of few logfile in the commadn promptttt
hope can someone help meeeeeee

its kinda urgent....

thanks in advance........ ....
Aug 10 '07 #17
prn
254 Recognized Expert Contributor
Hi bhumikas,

its working if I copy that script in the desired directory and run it.
I need to run for all the sub directories.... Also I need the output in a textfile becoz when it runs,I can only see the results of few logfile in the commadn promptttt
hope can someone help meeeeeee

its kinda urgent....

thanks in advance........ ....
Evidently, you have figured out why you were having problems with read permission, so I won't comment further on that. The perl script I posted above in post #9 of this thread does not dig down into subdirectories. It was posted simply as an illustration, not to solve your specific problem. Notice that I commented that you could call it with a command line like :
Expand|Select|Wrap|Line Numbers
  1. ./count.pl . for
where the "." is a reference to the current directory. If you want to run the same script in the current directory and all of its subdirectories, the simplest modification is simply to call it with:
Expand|Select|Wrap|Line Numbers
  1. find . -type d -exec ./count.pl {} for \;
And, of course, remember that you can always capture the output of ANY unix/linux process by redirecting it to a file. For example:
Expand|Select|Wrap|Line Numbers
  1. find . -type d -exec ./count.pl {} for >foo.txt \;
Naturally, I could equally well modify the perl script, but unix/linux has always had a "tools" approach that makes it unnecessary to rewrite everything just to do normal tasks.

HTH,
Paul
Aug 13 '07 #18

Sign in to post your reply or Sign up for a free account.

Similar topics

1
9080
by: David Thomas | last post by:
Hi there, a while ago, I posted a question regarding reading japanese text from a text file. Well, since I solved the problem, I thought I'd post my solution for the benefit of other people with the same problem. The plan was to make a script to read and display japanese text. I will use it for making a japanese proverb script and for a japanese language study script.
60
49231
by: Julie | last post by:
What is the *fastest* way in .NET to search large on-disk text files (100+ MB) for a given string. The files are unindexed and unsorted, and for the purposes of my immediate requirements, can't be indexed/sorted. I don't want to load the entire file into physical memory, memory-mapped files are ok (and preferred). Speed/performance is a requirement -- the target is to locate the string in 10 seconds or less for a 100 MB file. The...
0
935
by: jpauthement | last post by:
I have an application which searches through a comma-delimited text file which looks similar to this: "012233010","PAMIC 6X8","FA","0.000","0.000" "012233011","PAMIC 8X8","FA","1.000","0.000" The text file is a dump from an asp/400 system. And it is automatically ftp-ed to the website. The question i have is: Is it possible to do a query on the text file similar to
1
1632
by: jshaulis06 | last post by:
ok, heres my problem. im trying to make a program that searches a text file with a key word created by the user and prints it in a list box. sorta like a dictionay program in vb.net heres what i got so far dim sr as system.io.streamreader=file.opentext.fromfile("file.txt") dim def, word as string def=listbox.text word=textbox.text
1
4783
by: Osoccer | last post by:
...to a different folder and in the relocated file concatenates all of the lines in one long string with a space between each line element. Here is a fuller statement of the problem: I need a Visual Basic Script file, call it "Move and Reformat Text File.VBS," that will run from a Windows Script Host command-prompt-based version as follows: C:\> Cscript.exe "Move and Reformat Text File.VBS" The objective of the VBScript file, "Move...
3
2083
by: Gary | last post by:
Hi in a simple application that consists of a couple of user input forms. I'm wondering what the difference is between using a database technology and a plain text file? I've been working on this program for a week or so now and its a hobby project. I have never understood how to work with databases in visual studio (although I have strung together a couple of Microsoft access databases in the past, by trial and error.) As I don't...
8
2000
by: Max Steel | last post by:
Hey gang, I'm new to python coding. I'm trying to find the simplest way to open a text file (on the same server) and display it's content. The text file is plain text (no markup language of any kind). The filename gets found and placed into a variable named I'm using python to find the text files, and a .ezt template to display the list of the files. But I can't figure out how to read the contents of the file, and display it...
1
2038
by: 848lu | last post by:
hi, im trying to do a search on my Array, where a users searches on a HTML screen and then the PHP searches the array for the data under the roice entred by user.....text file data is under the php code <HTML> <BODY> <? $filename = "cdlist.txt"; $filepointer = fopen($filename,"r"); // open for read $cdArray = file ($filename);
0
9696
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10903
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10584
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10645
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10290
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9425
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
4482
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4063
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3131
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.