grep a character/word from a string and count number of occurrences in Linux - linux

I like to a grep a character from a string then count it, I don't see from google search. Please advise. I might miss search for it.
node_count=`echo "test1|test2|test3" | grep "|" |wc -l`|echo $node_count
output is always 1 to me.
1
Remember that I don't grep from a file but a line of string. Grep from a file is easy.

You might want to use option -o of grep:
$ node_count=`echo "test1|test2|test3" | grep "|" -o |wc -l` && echo $node_count
# 2

Related

How to get the no of matched occurrences using grep command in linux?

If we use grep -c option it will give you the each occurrences only once per line. But I need the total no of matched occurrences not line count.
Use this
grep -o pattern path | wc -l
You can use -o flag to output only the matched part and then pipe it to wc -w to get word count.
Eg: ls ~ | grep -o pattern | wc -w

Counting lines starting with a certain word

How to count the number of lines in a text file starting with a certain word?
I do not want to use sed and then wc -l. Any better solution?
Just grep your word and then use wc -l to count the lines... like this
grep '^your_word' /path/to/file | wc -l
Try this:-
awk '/^yourwordtofind/{a++}END{print a}' file
grep -c "pattern" <filename>
For example: If you want to search for a pattern more in a file test.txt, then below is the command:
grep -c "more" test.txt
cat <filename> | grep <word> | wc -l

Shell script to get count of a variable from a single line output

How can I get the count of the # character from the following output. I had used tr command and extracted? I am curious to know what is the best way to do it? I mean other ways of doing the same thing.
{running_device,[test#01,test#02]},
My solution was:
echo '{running_device,[test#01,test#02]},' | tr ',' '\n' | grep '#' | wc -l
I think it is simpler to use:
echo '{running_device,[test#01,test#02]},' | tr -cd # | wc -c
This yields 2 for me (tested on Mac OS X 10.7.5). The -c option to tr means 'complement' (of the set of specified characters) and -d means 'delete', so that deletes every non-# character, and wc counts what's provided (no newline, so the line count is 0, but the character count is 2).
Nothing wrong with your approach. Here are a couple of other approaches:
echo $(echo {running_device,[test#01,test#02]}, |awk -F"#" '{print NF - 1}')
or
echo $((`echo {running_device,[test#01,test#02]} | sed 's+[^#]++g' | wc -c` - 1 ))
The only concern I would have is if you are running this command in a loop (e.g. once for every line in a large file). If that is the case, then execution time could be an issue as stringing together shell utilities incurs the overhead of launching processes which can be sloooow. If this is the case, then I would suggest writing a pure awk version to process the entire file.
Use GNU Grep to Avoid Character Translation
Here's another way to do this that I personally find more intuitive: extract just the matching characters with grep, then count grep's output lines. For example:
echo '{running_device,[test#01,test#02]},' |
fgrep --fixed-strings --only-matching # |
wc -l
yields 2 as the result.

Find line number in a text file - without opening the file

In a very large file I need to find the position (line number) of a string, then extract the 2 lines above and below that string.
To do this right now - I launch vi, find the string, note it's line number, exit vi, then use sed to extract the lines surrounding that string.
Is there a way to streamline this process... ideally without having to run vi at all.
Maybe using grep like this:
grep -n -2 your_searched_for_string your_large_text_file
Will give you almost what you expect
-n : tells grep to print the line number
-2 : print 2 additional lines (and the wanted string, of course)
You can do
grep -C 2 yourSearch yourFile
To send it in a file, do
grep -C 2 yourSearch yourFile > result.txt
Use grep -n string file to find the line number without opening the file.
you can use cat -n to display the line numbers and then use awk to get the line number after a grep in order to extract line number:
cat -n FILE | grep WORD | awk '{print $1;}'
although grep already does what you mention if you give -C 2 (above/below 2 lines):
grep -C 2 WORD FILE
You can do it with grep -A and -B options, like this:
grep -B 2 -A 2 "searchstring" | sed 3d
grep will find the line and show two lines of context before and after, later remove the third one with sed.
If you want to automate this, simple you can do a Shell Script. You may try the following:
#!/bin/bash
VAL="your_search_keyword"
NUM1=`grep -n "$VAL" file.txt | cut -f1 -d ':'`
echo $NUM1 #show the line number of the matched keyword
MYNUMUP=$["NUM1"-1] #get above keyword
MYNUMDOWN=$["NUM1"+1] #get below keyword
sed -n "$MYNUMUP"p file.txt #display above keyword
sed -n "$MYNUMDOWN"p file.txt #display below keyword
The plus point of the script is you can change the keyword in VAL variable as you like and execute to get the needed output.

egrep: find lines with no characters

I have a text file and I need to search that file and figure how many blank lines are in the file. A blank line is a line with no characters.
I must use egrep.
[aman#localhost ~]$ cat >try
sldjjsd
dkfjkjdf
dfkjdf
[aman#localhost ~]$ egrep '^$' try|wc -l
4
This will do.
egrep '^$' blankfile -c
Another way, without egrep.
echo $(($(cat blank | wc -l)-$(cat blank | tr -s "\n" | wc -l)))

Resources