Monday, April 29, 2024
2
rated 0 times [  2] [ 0]  / answers: 1 / hits: 431  / 3 Years ago, thu, september 30, 2021, 11:16:46

first of all, thank you for your contribute to this forum! I switched to ubuntu a few months ago for working reasons and these pages are a great help!



Now, getting to the point:
I was trying to count a specific pattern in a text file:
grep -c [pattern] [filename]
Now: the pattern was: 'greater than' sign, i.e.: >
thus the command I gave was: grep -c > [filename]
I got this message:
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.



But now the text file was empty! (fortunately I had a backup...).
Why? Did I mess up in some previous operation or was it the last command I gave? I am just trying to make sense of this.


More From » command-line

 Answers
6

The previous answer is technically accurate, but I think is circumventing the user's overall knowledge of what they were trying to do. Let's focus on what you did wrong, and then rewrite the command so you can actually find the entries of ">" in the file instead.



(Explanation of why the file got dumped)
If you use > at the end of a line (try "ls > directory.txt") you'll wipe out directory.txt [if it exists], and input the results of the command. If you use >>, it simply appends (adds to the end) of any existing file. "grep" didn't realize you were actually searching for ">" instead of wanting to pipe the output into the file you intended to search. As a result, it simply dumped nothing into the file you had data in.



In your case, you probably wanted to use this instead:



grep -c -- '>' filename.ext


Let us know if that fixes your problem!


[#31203] Friday, October 1, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
egantfis

Total Points: 406
Total Questions: 108
Total Answers: 108

Location: Austria
Member since Thu, Jan 7, 2021
3 Years ago
;