Find word in a line and output in which line the word occurs / no. of times it occurred


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find word in a line and output in which line the word occurs / no. of times it occurred
# 1  
Old 08-15-2014
Find word in a line and output in which line the word occurs / no. of times it occurred

I have a file: file.txt, which contains the following data in it.
HTML Code:
    This is a file, my name is Karl, what is this process, karl is karl junior, file is a test file, file's name is file.txt
    My name is not Karl, my name is Karl Joey
    What is your name?
    Do you know your name and what it is?

When I'm running the following command to get line# of a word "is" in this file using the following command, it's giving me the output like:
Code:
 
$ grep -now "is" file.txt

HTML Code:
    1:is
    is
    is
    is
    is
    is
    2:is
    is
    3:is
    4:is
What command can I run to get the following output:

HTML Code:
    1:is
    1:is
    1:is
    1:is
    1:is
    1:is
    2:is
    2:is
    3:is
    4:is
OR

Code:
 
    is:1,1,1,1,1
    is:2,2
    is:3
    is:4

# 2  
Old 08-15-2014
Is this a homework assignment?
# 3  
Old 08-15-2014
Quote:
Originally Posted by Don Cragun
Is this a homework assignment?
NOPE
# 4  
Old 08-16-2014
Your title for this this thread is "Find word in a line and output in which line the word occurs / no. of times it occurred", but neither of the outputs you say you want includes the "no. of times it occurred".

Did you give up on getting output like:
Code:
1:is:5
2:is:2
3:is:1
4:is:1

or:
Code:
is:1:5,2:2,3:1,4:1

or is the thread title just misleading?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Egrep find word that occurs twice in a row

Hi there I am trying to figure out and understand what the syntax would be to egrep lines that have a word occur twice in a row. the two words obviously should have a space between them and also it has to be case sensitive which I believe grep is by deffault. the closest I have come is... grep... (7 Replies)
Discussion started by: spo_2138
7 Replies

2. Shell Programming and Scripting

Read a File line by line and split into array word by word

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)
Discussion started by: Kingcobra
6 Replies

3. UNIX for Dummies Questions & Answers

Find EXACT word in files, just the word: no prefix, no suffix, no 'similar', just the word

I have a file that has the words I want to find in other files (but lets say I just want to find my words in a single file). Those words are IDs, so if my word is ZZZ4, outputs like aaZZZ4, ZZZ4bb, aaZZZ4bb, ZZ4, ZZZ, ZyZ4, ZZZ4.8 (or anything like that) WON'T BE USEFUL. I need the whole word... (6 Replies)
Discussion started by: chicchan
6 Replies

4. Shell Programming and Scripting

Print word 1 in line 1 and word 2 in line 2 if it matches a pattern

i have a file in this pattern MATCH1 word1 IMAGE word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH1 word1 IMAGE word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1... (7 Replies)
Discussion started by: bangaram
7 Replies

5. UNIX for Dummies Questions & Answers

regular expression for replacing the fist word with a last word in line

I have a File with the below contents File1 I have no prior experience in unix. I have just started to work in unix. My experience in unix is 0. My Total It exp is 3 yrs. I need to replace the first word in each line with the last word for example unix have no prior experience in... (2 Replies)
Discussion started by: kri_swami
2 Replies

6. Shell Programming and Scripting

Need to replace the first word of a line if it occurs again in the next line(shell)

Hi folks, have a look into the attachment, i am not familiar with unix, can you please help me in this regard. thanks in advance, :) regards, Geeko (4 Replies)
Discussion started by: geeko
4 Replies

7. Shell Programming and Scripting

Looking for a single line to count how many times one character occurs in a word...

I've been looking on the internet, and haven't found anything simple enough to use in my code. All I want to do is count how many times "-" occurs in a string of characters (as a package name). It seems it should be very simple, and shouldn't require more than one line to accomplish. And this is... (2 Replies)
Discussion started by: Shingoshi
2 Replies

8. UNIX for Dummies Questions & Answers

Find a word and delete the line

Hi I have a text file like this name today.txt the request has been accepted the scan is successful at following time there are no invalid packages 5169378 : map : Permission Denied the request has been accepted Now what i want do is I want to search the today.txt file and if i... (1 Reply)
Discussion started by: gsusarla
1 Replies

9. Shell Programming and Scripting

TO find the word which occurs maximum number of times

Hi Folks !!!!!!!!!!!!!!!!!!! My Requirement is............. i have a input file: 501,501.chan 502,502.anand 503,503.biji 504,504.raja 505,505.chan 506,506.anand 507,507.chan and my o/p should be chan->3 i.e. the word which occurs maximum number of times in a file should be... (5 Replies)
Discussion started by: aajan
5 Replies

10. Shell Programming and Scripting

Can a shell script pull the first word (or nth word) off each line of a text file?

Greetings. I am struggling with a shell script to make my life simpler, with a number of practical ways in which it could be used. I want to take a standard text file, and pull the 'n'th word from each line such as the first word from a text file. I'm struggling to see how each line can be... (5 Replies)
Discussion started by: tricky
5 Replies
Login or Register to Ask a Question