cat file.txt | grep -x "\d*"
grep: \Documents and Settings: Is a directory
I want to search file.txt for any lines that are numbers only but grep seems to be viewing \d* as a wildcard for files and not the pattern. How can I specify that it's the pattern and it should use stdin for what to grep over?
The file is full of lines of datetime stamps, some end with a letter, some don't.
20140110122200
20131208041510M
...
I'm trying to only get the lines that don't end in a letter.
EDIT: I've also tried setting the filename instead of piping it with cat. Not much different.
C:\long\path>grep -ex "\d*" -f file.txt
grep: \Dell: Is a directory
grep: \Documents and Settings: Is a directory
Why are you using cat to pass the file to grep? Why not just give grep the filename directly?
grep -x '\d*' file.txt
I think the actual problem you're seeing is that the * wildcard is being expanded. That's why grep is giving you errors that mention actual directories (beginning with 'd') on your system.
Related
Say I have multiple .gz files that I want to search a keyword in them. I can do this by piping zcat result to a grep like this:
zcat some.file.* | grep "keyword_1" | ... | grep "keyword_n"
The output of this command though will be just the matching line and won't have the file name in it. Is there any way I can attach the file name to the zcat output?
Try zgrep instead of zcat:
zgrep -H keyword some.file.*
And if you want to use egrep to get pattern matching:
export GREP=egrep
zgrep -H -e "(keyword1|keyword2)" some.file.*
I have a bunch of files: some contain the word star, some contain the word start, some contain both.
I'd like to grep for files that contain the word star, but not the word start.
How can this be accomplished using only grep?
grep has some options for inverting the matches at the line or file level. You want the latter option, with the -L switch. The following will print the names of all the files in a folder that don't contain the text start:
grep -LF start *
-F tells grep that start is a literal string and not a regex. It's optional here, but might speed things up a tiny bit.
You can use the resulting list to search for files that contain star:
grep -lF star $(grep -LF start *)
-l prints only the names of files containing a match, not any line-by-line or match-by-match details. If this is not exactly what you want, man grep is your friend.
This uses an additional shell construct to run the inverted match, but it technically doesn't call any additional programs that aren't grep.
Update
Since you mention wanting to look through all the files starting with a given root folder, change -LF to -LFr. Replace * with your root folder if you don't want to change working directories.
-r tells grep to recurse into directories, and search every file it finds along the way.
With GNU grep for -w:
$ cat file
foo star bar
oof start rab
$ grep -w star *
foo star bar
or if you just want the names of the files containing star:
$ grep -lw star *
file
and to just find files to look in:
$ find . -maxdepth 1 -type f -exec grep -w 'star' {} \;
foo star bar
I would like to find all the matches of the text I have in one file ('file1.txt') that are found in another file ('file2.txt') using the grep option -f, that tells to read the expressions to be found from file.
'file1.txt'
a
a
'file2.txt'
a
When I run the command:
grep -f file1.txt file2.txt -w
I get only once the output of the 'a'. instead I would like to get it twice, because it occurs twice in my 'file1.txt' file. Is there a way to let grep (or any other unix/linux) tool to output a match for each line it reads?
Thanks in advance.
Arturo
Grep works as designed, giving only one output line.
You could use another approach:
while IFS= read -r pattern; do
grep -e $pattern file2.txt
done < file1.txt
This would use every line in file1.txt as a pattern for the grep, thus resulting in the output you're looking for.
When you use
grep -f pattern.txt file.txt
It means match the pattern found in pattern.txt in the file file.txt.
It is giving you only one output because that is all is there in the second file.
Try interchanging the files,
grep -f file2.txt file1.txt -w
Does this answer your question?
I am trying to exclude lines from a file containing some words from another file. For example in lines.txt I have:
my dog
my cat
my fish
and in words.txt:
cat
Unfortunately, the command grep -vif words.txt lines.txt doesn't work, but the command grep -vi cat lines.txt would give me the correct result, so I guess there is a problem with the -f option. I have no idea what could be the problem with such a simple syntax, and there are no empty lines in words.txt or lines.txt, I've already checked.
Okay I have a file that contains numbers like this:
L21479
What I am trying to do is use grep (or a similar tool) to find all the strings in a file that have the format:
L#####
The # will be the number. SO an L followed by 5 numbers.
Is this even possible in grep? Should I load the file and perform regex?
You can do this with grep, for example with the following command:
grep -E -o 'L[0-9]{5}' name_of_file
For example, given a file with the text:
kasdhflkashl143112343214L232134614
3L1431413543454L2342L3523269ufoidu
gl9983ugsdu8768IUHI/(JHKJASHD/(888
The command above will output:
L23213
L14314
L35232
If it is just in a single file, you can do something along the lines of:
grep -e 'L[0-9]{5}' filename
If you need to search all files in a directory for these strings:
find . -type f | xargs grep -e 'L[0-9]{5}'