This question already has answers here:
Grep inside all files created within date range
(3 answers)
Closed 5 years ago.
I have a file with timestamps and I want to grep it so that I can see the lines matching 12:30:00 - 12:32:00 . I know that in bash there is the {0..2} for doing something similar but I dont know in grep how to do it?
At the moment I am doing grep 2015-01-12 12:3{0..2} but it doesnt work?
you can use the following for this.
grep '2015-01-12 12:3[0-2]'
or
grep '2015-01-12 12:3[0,1,2]'
I hope it helps.
Related
This question already has answers here:
How can I use grep to show just filenames on Linux? [closed]
(3 answers)
Closed 3 years ago.
I want to know which files in a folder contain a certain string. There are many answers on here showing how to grep through many files. However, is there a way to print out the file names that have the string present?
I want to use something like:
grep -E '275322' *.txt
Can I use echo to print the file currently being looked at if the string is present?
Any help is appreciated.
If I understand the question correctly, you only want the filenames?
grep -l '275322' *
Check the manpage -
-l, --files-with-matches print only names of FILEs containing matches
This question already has answers here:
How to show only next line after the matched one?
(14 answers)
grep: show lines surrounding each match
(14 answers)
Read from a endless pipe bash [duplicate]
(1 answer)
Closed 4 years ago.
Should I use grep to filter a real time output? I'm not sure if this is what I should use for a real time output.
Example: command -option | grep --color 'string1\|string2'
If so, how to get also the lines after string1 and string2?
As #shellter mentioned, from man grep:
-A num, --after-context=num
Print num lines of trailing context after each match. See also the -B and -C options.
so you would use command -option | grep -A 1 --color 'string1\|string2' to print matched lines and the line right after them.
There are plenty of other options in the manual for grep, and most other command-line programs, so I suggest getting used to running man cmd as a quick first check.
This question already has answers here:
How can I use a file in a command and redirect output to the same file without truncating it?
(14 answers)
How to sort a file in-place?
(7 answers)
Closed 4 years ago.
I am trying to sort a file with following command : sort ${filename} > ${filename} , and I get as result - an empty file.
Can someone explain why I get this result and how can I fix it?
What you are trying to do is a inplace sort so which may not be supported, try instead.
sort ${filename} > temp_file && mv temp_file ${filename}
In short, because Bash truncates the file before sort can read from it. Apart from #RavinderSingh13’s answer, which is the standard way to fix it, you could also use sponge from the moreutils package:
sort ${filename} | sponge ${filename}
This question already has answers here:
How can I grep recursively, but only in files with certain extensions?
(12 answers)
Closed 7 years ago.
I'm looking for any .pro file that contains the string "xxxxx.h"
I tried doing:
grep -Iir "xxxxx.h" . | grep "*.pro"
But that didn't work. Is this even possible with just the grep command? If not, how else would I do it?
You can use --include option:
grep -Iir --include='*.pro' "xxxxx\.h" .
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Join lines based on pattern
I have the following file:
test
one
My
two
Hi
three
i need a way to use cat and sed to give the following output:
testone
Mytwo
Hithree
How can i achieve this in a single command?
Where the file "foo.txt" contains your text:
cat foo.txt | sed -e 'N;s/\n//'