Why am I Getting Different Answers from Grep -Oc "Foo" File and Grep -O "Foo" File | Wc -L ? Which Is Correct

grep -c not working

grep -c counts lines matches, while -o shows the match only. i.e. with input:

abcabc
abcabc

Using grep -o 'a' on the above will output:

a
a
a
a

And grep -c 'a' will give

2

You can pipe to wc -l to count number of matches:

$ grep -o 'a' demo.txt | wc -l
4

Bash Script To Find Dollar Words Not As Fast As Was Hoping

As @thatotherguy pointed out in a comment, there are two big problems here. First, the way you're reading lines from the file reads the entire file every line. That is, to read the first line you run sed -n "1"p Words.txt, which reads the entire file and prints only the first line; then you run sed -n "2"p Words.txt, which reads the entire file again and prints only the second line; etc. To fix this use a while read loop:

while read word; do
...
done <Words.txt

Note that if anything inside the loop tries to read from standard input, it'll steal some of the input from Words.txt. In that case you can send the file over FD #3 instead of standard input with while read -u3 ... done 3<Words.txt.

The second problem is this bit:

occurences["$i"]=$(echo $word | grep ${letter["$i"]} -o | wc -l)

...which creates 3 subprocesses (echo, grep, and wc), which isn't too bad except that this runs 26 times for each and every word in the file. Creating processes is expensive compared to most shell operations, so you should do your best to avoid it especially in loops that run many many times. Try this instead:

matches="${word//[^${letter[i]}]/}"
occurences[i]="${#matches}"

This works by replacing all characters that aren't ${letter[i]} with "", then looking at the length of the resulting string. The parsing happens entirely in the shell process, so it should be much faster.

How can you find and replace text in a file using the Windows command-line environment?

A lot of the answers here helped point me in the right direction, however none were suitable for me, so I am posting my solution.

I have Windows 7, which comes with PowerShell built-in. Here is the script I used to find/replace all instances of text in a file:

powershell -Command "(gc myFile.txt) -replace 'foo', 'bar' | Out-File -encoding ASCII myFile.txt"

To explain it:

  • powershell starts up powershell.exe, which is included in Windows 7
  • -Command "... " is a command line arg for powershell.exe containing the command to run
  • (gc myFile.txt) reads the content of myFile.txt (gc is short for the Get-Content command)
  • -replace 'foo', 'bar' simply runs the replace command to replace foo with bar
  • | Out-File myFile.txt pipes the output to the file myFile.txt
  • -encoding ASCII prevents transcribing the output file to unicode, as the comments point out

Powershell.exe should be part of your PATH statement already, but if not you can add it. The location of it on my machine is C:\WINDOWS\system32\WindowsPowerShell\v1.0

Update
Apparently modern windows systems have PowerShell built in allowing you to access this directly using

(Get-Content myFile.txt) -replace 'foo', 'bar' | Out-File -encoding ASCII myFile.txt

grep count all matches in one line of single pattern

With grep that supports -o option:

$ grep -o 'int' ip.txt | wc -l
3

With ripgrep:

$ rg -oc 'int' ip.txt
3

Count number of occurrences of a pattern in a file (even on same line)

To count all occurrences, use -o. Try this:

echo afoobarfoobar | grep -o foo | wc -l

And man grep of course (:

Update

Some suggest to use just grep -co foo instead of grep -o foo | wc -l.

Don't.

This shortcut won't work in all cases. Man page says:

-c print a count of matching lines

Difference in these approaches is illustrated below:

1.

$ echo afoobarfoobar | grep -oc foo
1

As soon as the match is found in the line (a{foo}barfoobar) the searching stops. Only one line was checked and it matched, so the output is 1. Actually -o is ignored here and you could just use grep -c instead.

2.

$ echo afoobarfoobar | grep -o foo
foo
foo

$ echo afoobarfoobar | grep -o foo | wc -l
2

Two matches are found in the line (a{foo}bar{foo}bar) because we explicitly asked to find every occurrence (-o). Every occurence is printed on a separate line, and wc -l just counts the number of lines in the output.

Count occurrences with grep

What you show should work as expected. How does it fail?

In any case, a better way to count the occurrences of each of a list of words in anoter file would be:

#!/usr/bin/env bash

while read word
do
grep -oc "$word" source.txt >> output
done < <(sed 's/\r//' list.txt)

Here, I am using the while loop to read the file after removing windows style line endings. Then, I use grep's -c option to count the occurrences. When run on your example files, this produces:

$ cat output 
1
1
1

To count multiple occurrences in a single line, you could try this:

while read word
do
export word;
perl -lne '@c=/$ENV{"word"}/g; $f=scalar @c; print $f if $f>0' source.txt
done < list.txt

Command to delete all pods in all kubernetes namespaces

There is no command to do exactly what you asked.

Here are some close matches.

Be careful before running any of these commands. Make sure you are connected to the right cluster, if you use multiple clusters. Consider running. kubectl config view first.

You can delete all the pods in a single namespace with this command:

kubectl delete --all pods --namespace=foo

You can also delete all deployments in namespace which will delete all pods attached with the deployments corresponding to the namespace

kubectl delete --all deployments --namespace=foo

You can delete all namespaces and every object in every namespace (but not un-namespaced objects, like nodes and some events) with this command:

kubectl delete --all namespaces

However, the latter command is probably not something you want to do, since it will delete things in the kube-system namespace, which will make your cluster not usable.

This command will delete all the namespaces except kube-system, which might be useful:

for each in $(kubectl get ns -o jsonpath="{.items[*].metadata.name}" | grep -v kube-system);
do
kubectl delete ns $each
done

Refactoring legacy C code - using extern declarations to help split up modules (potential linking and run time issues)

I tried your to compile your files into shared libs and then use them (I use cygwin).

Here's for Foo:

cm@Gregor-PC ~/src
$ gcc -I. -c --shared -o libFoo.so Foo.c

With the bin util nm you can check the symbols (grep for 'foo' to limit the output)

cm@Gregor-PC ~/src
$ nm libFoo.so | grep foo
0000000a T _foo
U _foobar

Where it gives an offset and says 'T' for terminated that tells you the symbol is defined in the lib.

Now the FooBar lib has to linked against Foo in order to have the foo symbol

cm@Gregor-PC ~/src
$ gcc -I. -L. --shared -o libFooBar.so FooBar.c libFoo.so

cm@Gregor-PC ~/src
$ nm libFooBar.so | grep foo
61f0111e T _foo
61f010e0 T _foobar

With this I can compile against FooBar only and get foo as a known symbol:

cm@Gregor-PC ~/src
$ gcc -I. -o tst1 tst1.c libFooBar.so
cm@Gregor-PC ~/src
$ ./tst1
Function returned: 42

So it seems to work OK.

You can improve on your approach to modularize your C code by having header files that contain only public data types, exported function prototypes (declared as extern) and maybe even public global variables or constants. Such a header file declares the interface to the module and would have to be included where the module is used.

This is explained with much more detail in the wonderful book 'Functional C' (Hartel, Muller, 1997, Addison Wesley, link ) in the chapter on modules.

The benefit is that your dependencies are clearer to see (as includes in the source files) and you don't have to have that unsightly extern declaration in the Foo source.

For your example:

/*  Foo.h */
extern int foo(int id); /* exported to FooBar */

/* FooBar.h */

extern int foobar(); /* export to Foo */
extern void do_something_else(); /* export to tst1 */

/* Foo.c */
#include <Foo.h>
#include <FooBar.h>

int do_something() {
return 11;
}

int foo(int id){
int var;

switch (id){
case 1:
var = do_something();
break;

case 2:
/* the module that gets here, has the required functions defined in it */
var = foobar();
}

return var;
}

/* FooBar.c */

#include <stdio.h>
#include <Foo.h>
#include <FooBar.h>

int foobar(){
return 42;
}

void do_something_else(){
int ret = foo(2);
printf("Function returned: %d", ret);
}


Related Topics



Leave a reply



Submit