Find the number of occurences of certain string sequences - linux

I want to count the number of occurences of the IP address 192.168.1.10 in a text file using grep | wc.
The command I use is:
cat ./capture.txt|grep "192.168.1.10"|wc -w
which returns 0, and I don't know why.
Here is the content of my .txt file:

give this a try:
grep -Fwo '192.168.1.10' file|wc -l
-F makes the grep take your pattern as literal string instead of regex
-w excludes 192.168.1.101 or 192.168.1.100
-o lists each match in a line. grep does line based match, if your pattern matched twice in a line, the result of occurrence count may be wrong.

cat ./capture.txt | grep "\b192\.168\.1\.10\b" -c
\. search for dot, not any character
\b match at the beginning or end of a word
-c return the number of occurrences

Related

grep perfect string match matches dash in the prefix

I am trying to match the rows where there is just "glasses" or something like "blabla,glasses,blabla", using grep.
for example, in this file:
1 this-is-fake-glasses
2 glasses
3 abc,glasses
4 glasses,abc
5 abc,glasses,abc
my expected output includes just the last two lines.
Following what I tried
grep -w "glasses$" t.txt # matches first and last
grep -o "glasses" t.xt # matches all
grep -E '(^|\s)glasses($|\s)' t matches just last
According to your constraints, the following output only last two rows..
grep -v "glasses$" t.txt
As you don't want glasses to the ending word in the line?

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.

To grep string from file and read first matching string with full variable name

I have file named file.txt, i am trying to read string first search which matches my pattern search from the file. The problem here is with my command entire line is printed. where i am looking for that variable which matches with search pattern with its full variable name, in this example it is warning_duration=""; where my search pattern is duration *=.i have posted the command i tried to read result also with expected result.
Please help !!!
file.txt
warning_type="";warning_threshold="";warning_duration="";oemhp_power_micro_ver="";previous_warning_threshold="";
duration=19;
duration =1;
commands i tried :
cat file.txt | grep -m1 "duration *="
warning_type="";warning_threshold="";warning_duration="";oemhp_power_micro_ver="";previous_warning_threshold="";
cat file.txt | grep -oP -m1 "duration *="
duration=
expected result:-
warning_duration="";
You may use this grep command:
grep -m1 -woE "[_[:alnum:]]*duration *=[^;]*" file
warning_duration=""
Details:
-o: Only show matches
-E: Enable extended regex
-w: Word search
[_[:alnum:]]*: Match 0 or more of a _ or alphanumeric characters
duration *=: Match duration followed by 0 or more spaces and =
[^;]*: Match 0 or more of any character that are not ;
Could you please try following, written and tested with shown samples. Simple explanation would be; using match function to match regex ;[_[:alnum:]]+duration="" to get required value by OP eg--> warning_duration=""
awk 'match($0,/;[_[:alnum:]]+duration=""/){print substr($0,RSTART+1,RLENGTH-1)}' Input_file
With GNU grep and with your shown samples you could try following.
grep -oP '.*?;.*?;\K[_[:alnum:]]+duration=""' Input_file
Explanation: using GNU grep's -oP option to match exact match and to enable PCRE regex here. In regex mentioning non-greedy matches to match till 2nd semi-colon and forgetting(removing) matched values by \K option and matching alphanumeric9with _) ` or more occurrences along with duration="" to get the matched value in current line.

grep date from string

I'm trying to print any date value from a string. For example:
echo "08/08/2018 text here" | grep '/(0\d{1}|1[0-2])\/([0-2]\d{1}|3[0-1])\/(19|20)\d{2}/'
This returns no result. I want to print out only the date value, excluding the text here.
grep doesn't use / delimiters around the regexp, and doesn't need you to escape embedded /.
You need to use the -P option to use a PCRE regexp with GNU grep, so it will recognize \d for digits.
You should put \b around the regexp, to match word boundaries. Otherwise, if the input contains 108/08/2018 it will match the date that starts after 1.
You need the -o option to print only the part of the line that matches, rather than the whole matching line.
echo "08/08/2018 text here" | grep -Po '\b(0\d{1}|1[0-2])/([0-2]\d{1}|3[0-1])\/(19|20)\d{2}\b'

Using grep to get 12 letter alphabet only lines

Using grep
How many 12 letter - alphabet only lines are in testing.txt?
excerpt of testing.txt
tyler1
Tanktop_Paedo
xyz2#geocities.com
milt#uole.com
justincrump
cranges10
namer#uole.com
soulfunkbrotha
timetolearnz
hotbooby#geocities.com
Fire_Crazy
helloworldad
dingbat#geocities.com
from this excerpt, I want to get a result of 2. (helloworldad, and timetolearnz)
I want to check every line and grep only those that have 12 characters in each line. I can't think of a way to do this with grep though.
For the alphabet only, I think I can use
grep [A-Za-z] testing.txt
However, how do I make it so only the characters [A-Za-z] show up in those 12 characters?
You can do it with extended regex -E and by specifying that the match is exactly {12} characters from start ^ to finish $
$ grep -E "^[A-Za-z]{12}$" testing.txt
timetolearnz
helloworldad
Or if you want to get the count -c of the lines you can use
$ grep -cE "^[A-Za-z]{12}$" testing.txt
2
grep supports whole-line match and counting, e.g.:
grep -xc '[[:alpha:]]\{12\}' testing.txt
Output:
2
The [:alpha:] character class is another way of saying [A-Za-z]. See section 3.2 of the the info pages: info grep 'Regular Expressions' 'Character Classes and Bracket Expressions' for more on this subject. Or look it up in the pdf manual online.

Resources