Find all occurrences of a word in .txt files with GIT Bash on Windows [duplicate] - bash

This question already has an answer here:
Using regex in Grep for Windows command line
(1 answer)
Closed 2 years ago.
Trying to find all occurrences of a word in a range of different .txt files in a directory.
I'm looking for commands that will give me an exact count if possible.
so far I have tried:
$ grep -w 'string' *
and:
$ grep --include=\*.{txt} -rnw desktop/testfiles/ -e "string"
The first outputs its entire contents and the second doesn't seem to do anything.
Any ideas?

simply :
grep -w "string" *.txt | wc -l
also GNU awk allows it to be done in single command
awk -v w="string" '$1==w{n++} END{print n}' RS=' |\n' *.txt

Related

How to manipulate a string with the grep command? [duplicate]

This question already has answers here:
How can I remove the extension of a filename in a shell script?
(15 answers)
Closed 11 months ago.
I have a filename with the format yyyymmdd.txt. How can I output only yyyymmdd without the .txt extension?
Example
20220414.txt (before output)
20220414 (after the output)
basename has an option to remove a suffix:
basename -s .txt 20220414.txt
gives:
20220414
Or, if your filename is stored in a variable, bash can help:
a=20220414.txt
echo ${a%.*}
gives:
20220414
You can user awk with flag -F to specify the separator . and then print the first part with $1
echo "20220414.txt" | awk -F "." ' {print $1}'
output
20220414
grep doesn't manipulate anything, it shows what you have in a file. So, you can't modify that file using grep, but you can modify what it shows, using the -o switch, as you can see here:
Prompt> echo "20220414.txt" | grep -o "[0-9]*"
20220414
The [0-9]* means "a list of integers, going from character '0' to character '9'.

How can I delete empty line from my ouput by grep? [duplicate]

This question already has answers here:
Remove empty lines in a text file via grep
(11 answers)
Closed 4 years ago.
Exists way to remove empty lines with cat myfile | grep -w #something ?
I looking for simple way for remove empty lines from my output like in the way the presented above.
This really belongs on the codegolfing stackexchange because it's not related to how anyone would ever write a script. However, you can do it like this:
cat myfile | grep -w '.*..*'
It's equivalent to the more canonical grep ., but adds explicit .*s on either side so that it will always match the complete line, thereby satisfying the word boundary conditions imposed by -w
You can pipe your output to awk to easily remove empty lines
cat myfile | grep -w #something | awk NF
EDIT: so... you just want cat myfile | awk NF?
if you have to use grep, you can do grep myfile -v '^[[:blank:]]*$'

Unix: Perform grep command for every line from inputfile and save output to file [duplicate]

This question already has answers here:
How to make "grep" read patterns from a file?
(2 answers)
Closed 6 years ago.
I'm trying to write a small unix script to search one file for the line that contain a certain term, found in a different file. I would like to save the output of this command to a new file.
I have a file containing terms (terms.txt) which has a term on each line:
term1
term2
term3
term4
For each of these terms, I want to find the line that contains this term in another file (scores.txt) and append the output of this to a new file (output.txt).
The script I have come up with thus far:
#!/bin/bash
for f in `cat terms.txt`;
do
grep -i $f scores.txt >> output.txt;
done
Somehow this does not seem to work properly.
Running just the grep command with the term hard coded does indeed give me the right line I'm searching for:
grep -i "term1" scores.txt
Also, a simple echo does give me the right terms:
for f in `cat terms.txt`; do echo $f; done
However, when I try to repeat this with the $f variable, to repeat the same command for every term in my terms.txt, it does not work.
Could someone help me out on this one?
can you try:
grep -if terms.txt scores.txt > output.txt
basically grep's option f treats the strings in the terms.txt file as patterns to search for in scores.txt
If your terms.txt has CRLF line endings, try this:
grep -if <(tr -d '\r' < terms.txt) scores.txt > output.txt

How to sort files by mtime and pass to grep? [duplicate]

This question already has answers here:
grep without showing path/file:line
(3 answers)
Closed 4 years ago.
I try to parse log files - get some values from strings and write it into file
First, I get the list of files sorted by mtime.
find . -name log* -printf '%Tm%Tm%Td%TH%TM%TS %p\n'| sort | awk '{print $2}'
it works correctly and prints list of files
For example
./2015195/log/log.08
./2015486/log/log.10
./2015418/log/log.13
./2015415/log/log.14
./2015015/log/log.18
./2015715/log/log.19
./2015115/log/2015-09-10/log.21
...
Next, pass through this list and print words from lines with specific pattern
grep 'pattern' $(find . -name log* -printf '%Tm%Tm%Td%TH%TM%TS %p\n'| sort | awk '{print $2}') | awk '{print $1" "$4}' > prsd.txt
It works but it adds file name to every output line like
./2015195/log/log.08:02:01:09,811 12345ABCD
./2015195/log/log.08:02:02:01:09,975 12345CDEF
./2015195/log/log.08:12:02:02:01:09,978 12345EFGF
./2015195/log/log.08:02:02:01:10,223 12345LJIG
./2015195/log/log.08:02:01:10,275 12345IIUY
...
Here the problem, how to delete those additions?
thanks in advance.
From man grep,
-h, --no-filename
Suppress the prefixing of file names on output. This is the
default when there is only one file (or only standard input)
to search.

How to use grep command to extract number in a text file [duplicate]

This question already has answers here:
Extract number from a line with awk/sed
(2 answers)
Closed 7 years ago.
I have a cookie file,I just want to extract the number before the .jpg extension using grep command ..how can i do it
USERSTAFFPHOTO 09480106177557.jpg
I want to copy 09480106177557
echo "USERSTAFFPHOTO 09480106177557.jpg" | grep -oE '[0-9]{1,}'
You should chain 2 grep commands, the first one to get the file name along with the .jpg extension and the second one to get only the number from the file name:
grep -oE '[0-9]+\.jpg' | grep -oE '[0-9]+'
Otherwise, you would get all the numbers that exist in the file with or without the .jpg extension.
Why restrict yourself to a particular tool? Do you want the number, or do you want to use grep? If the format is fixed, I would do this with tr:
echo USERSTAFFPHOTO 09480106177557.jpg | tr -cd [0-9]
If this is one line in a file and you need to filter it out, try awk:
awk '/USERSTAFFPHOTO/{print $2}' FS=' |\\.' input

Resources