Finding word and number of lines in Unix - linux

I am trying to find the number of lines that contain the word. I realize this is possible with the wc command and the -l flag, but I'm not 100% sure how to do it. Any ideas?

Instead of wc, you can use grep like this:
grep -c "word" file

Related

Linux counting words in random characters

I have generated a file of random characters for A-Z and a-z, the file has different sizes for example 10000 characters or 1000000 I would like to search in them how many times the word 'cat' or 'dog' appeared Would someone be able to provide the command linux grep... | wc... or any other command that can handle this task.
grep has a -c command that will count the number of matches found.
So
grep -c "cat\|dog" <file name>
add -i if you want a case insensitive count
You can use grep with the flag -o. For example:
grep -o "dog\|cat" <filename> | wc -l
About the flag -o, according to man grep: «Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.»
This solution will work in several situations: multiple lines, a single line, the word surrounded with whitespaces or other characters, etc.

How to print the longest word in a file by using combination of grep and wc

iam trining to find the longest word in a text file.
i tried it and find out the no of characters in the longest word in a file
by using the command
wc -L
i need to print the longest word By using this number and grep command .
If you must use the two commands give, I'd suggest:
grep -E ".{$(wc -L < test.txt)}" test.txt
The command substitution is used to build the correct brace expression to match the line(s) with exactly the given number of characters. -E is needed to enable extended regular expression support; otherwise, the braces need to be escaped: grep ".\{...\}" test.txt.
Using an awk command that makes a single pass through the file may be faster.

from Linux command line, find number of lines in which a string occurs

I have a file in the location /home/someuser/sometext.txt . I want to count the number of lines in which a particular string occurs. What's the way to do that from Linux command line?
grep with -c switch is what you need:
grep -c "pattern" /home/someuser/sometext.txt
Alternate solution using awk:
awk '/regex/{c++}END{print c+0}' /home/someuser/sometext.txt
You're looking for the grep command. Here's a basic tutorial. It's extremely useful for string searching in files. It also has support for regular expressions.
It looks like you'll do something like this:
grep -c "mystring" /home/someuser/sometext.txt
The -c argument is short for --count and tells grep to print out the number of lines that contain the string.

How to display the first line that contains a certain string in a text file unix?

Was hoping someone can help out with this. I am trying to figure out how to display the first line that contains a certain string i.e. "computer" (first occurrence of "computer" in a txt file). I would prefer to do this using grep.
I know grep "computer" somefile.txt
would display all of the lines including "computer".
I am eager to learn and if anyone has alternative ways I would like to hear!
Thx everyone
Use the match count option of grep
grep -m 1 "computer" somefile.txt
Note that grep is non standard across un*x's so while http://www.gnu.org/software/grep/ supports this, if your distro or unix does not this will not work.
Pipes are your friend:
grep "computer" somefile.txt | head -n1
Is this homework?
grep -v "computer" somefile.txt | head -n 1
Comes to mind the quickest.

Terminal command to find lines containing a specific word?

I was just wondering what command i need to put into the terminal to read a text file, eliminate all lines that do not contain a certain keyword, and then print those lines onto a new file. for example, the keyword is "system". I want to be able to print all lines that contain system onto a new separate file. Thanks
grep is your friend.
For example, you can do:
grep system <filename> > systemlines.out
man grep and you can get additional useful info as well (ex: line numbers, 1+ lines prior, 1+lines after, negation - ie: all lines that do not contain grep, etc...)
If you are running Windows, you can either install cygwin or you can find a win32 binary for grep as well.
grep '\<system\>'
Will search for lines that contain the word system, and not system as a substring.
below grep command will solve ur problem
grep -i yourword filename1 > filename2
with -i for case insensitiveness
without -i for case sensitiveness
to learn how grep works on ur server ,refer to man page on ur server by the following command
man grep
grep "system" filename > new-filename
You might want to make it a bit cleverer to not include lines with words like "dysystemic", but it's a good place to start.

Resources