Hello All,
I have to write a C Code to read a word from file and Keep track of the number of word occurrence in each line and total in the file. Maintaining total count is easier but maintaining per line count is what I am struggling to achieve. I thought of maintaining linked list for maintaining line no and and count in that line but its not that efficient if used with files with bigger size. Your help needed.
Do you need to keep track of the line word counts until end of processing, or just can you just output them as you go?
If it's the latter then you only need to maintain your linked list while you're processing any given line, so it shouldn't be a significant performance hit.
Alternatively, you could extend whatever data structure you're using to hold the total count per word to include the per-line data for each word as well. Again, that means your lists won't be too long.
Also, you could use a hash table rather than a linked list.
Dear CarloM,
Thanks man for your input. I eed to keep track of the line word counts until end of processing and then pass it to other thread. I was assuming worse case of finding a word on each line of 10K lined file.
How does your code get the word(s) to search for? Is there only one word, or are there more than one?
That could make a big difference in how you design your code.
Also, what do you know about the file before you start processing it? The more data you have, the better you can make your program perform. Because for a large file, scalability is going to be a problem.
Assuming you get the words to be searched via command line arguments, something like this could search a single line:
That should work to count words in a single line of text.
One thing that's important: having a maximum line length per file. Is that specified? If so, you can use "fgets()" and read one line at a time very simply:
Hello there
so i have a txt file containing word like "one two three four plus four five six".
I want to save every word in the file into a variable, and then use that variable to generate real numbers and apply the arithmetic value on them.
example: the txt files becomes 123 + 456 and... (10 Replies)
Hi All,
Hope you guys had a wonderful weekend
I have a scenario where in which I have to read a file line by line
and check for few words before redirecting to a file
I have searched the forum but,either those answers dint work (perhaps because of my wrong under standing of how IFS... (6 Replies)
how can I know that the word in test file end at this point .... I have an Idea to search on ' ' , '\n' or '\0' but don't know what function can store the string before those characters ..
help please ! (3 Replies)
Hi i am new in scripting
how i can get 2 elements from first line of delimited txt file in shell scripts.
AA~101010~0~AB~8000~ABC0~
BB~101011~0~BC~8000~ABC~
CC~101012~0~CD~8000~ABC0~
DD~101013~0~AB~8000~ABC~
AA~101014~0~BC~8000~ABC0~
CC~101015~0~CD~8000~ABC~
can anyone plse help?... (3 Replies)
Folks,
how to read the second word of the first line from a text file. Text file does not have any delimiters in the line and has words at random locations. Basically the text file is a log and i want to capture a number that is in second position.
Appreciate your help
Venu (1 Reply)
File having data in following format :
file name : file.txt
--------------------
111111;name1
222222;name2
333333;name3
I want to read this file so that I can split these into two paramaters i.e. 111111 & name1 into two different variables(say value1 & value2).
i.e val1=11111 &... (2 Replies)
Hi
I am writing a script that needs to read all file from a directory and print only last word of all file names.
My script:
for file in /documents/files/
do
$shortFile=$(file##.*)
echo $shortFile
done
All my file names in /document/files/ directory are like unix_ubuntu but I need to... (1 Reply)
file1: has all words to be searched.
100007
200999
299997
File2: has all file names to be searched.
C:\search1.txt
C:\search2.txt
C:\search3.txt
C:\search4.txt
Outfile: should have all found lines.
Logic: Read each word in file1 and search each file in the list of File2; if the... (8 Replies)
Hi,
I want to read each word in a file.
start at a particular character say '%' and read till another character say ')' (these two characters form the part of my file).
then i want to delete the whole sentence(that is between '%' and ')' ) and keep the remaining file intact.
Its urgent... (1 Reply)