Tagged Questions

grep is a command-line tool for searching text patterns in files

learn more… | top users | synonyms

4
votes
5answers
2k views

grep for multiple strings in files, and then list the files in the order of size

I am in a folder with lots of .txt file, I would like to find all the files which contain both stringA and stringB (not necessarily on the same line), then list these files in the order of the size ...
5
votes
1answer
2k views

Piping search term (not filename) to grep

I want to pick out certain lines containing a given number from a file. The file I want to search is called os_clusters/piRNA_clusters.bed. awk '{if (a[$0]++ == 0) {split($0,b,"."); ...
4
votes
2answers
193 views

Printing lines from one file if part of them appears in another. Both files are millions of lines long

I have two files, let's call them 123.txt and 789.txt. 123.txt is 2.5M lines long, and 789.txt is 65M lines long. Is there any way to use grep or similar to keep any lines from 789.txt that contain ...
4
votes
3answers
1k views

How to find first match in multiple files

Is there a way for the find command to search for the first match or occurrence of a string or pattern within each of multiple files? I've been using the usual syntax: find dir -iname '*.ext' -exec ...
1
vote
4answers
75 views

Extract data from line and prepend to the line?

Ok, I have some data in the form: <a href="/praha/jaktovidi/_zprava/jak-to-vidi-rektor-richard-hindls-5-cervna--1220102"> <a ...
1
vote
2answers
159 views

Tell a regex expression to skip the beginning of a line before matching

If I had a string like 45mb ./aaaa/bbbb/cccc/dddd and I use a regex like [^\/]*\/ it will match 45mb ./aaaa/bbbb/cccc/ I only want it to match ./aaaa/bbbb/cccc/ but I can't figure out how to do ...
1
vote
4answers
535 views

using simple regex quantifier expression in grep

Does anyone know why ls | xargs -n 1 basename | grep -E '[0-9]{1}' does not only show occurrences of a single digit between 0-9? For example if I have: touch 1 touch 22 touch 333 touch test_file ...
1
vote
1answer
59 views

How to use GNU tools to extract and merge two substrings of the same string?

Suppose I get the following result after doing a grep "emailed" /home/John/logs/2013-07-10.171222-0400EDT.html:I have emailed Mark. Thanks I want to extract the date from this so that my output ...
3
votes
2answers
273 views

How to grep recursively in order of last modified time?

grep -r does a recursive search in all files in a particular directory. However, if I want these files to be read by grep in the order of their last modified time, how do I do that?
1
vote
1answer
84 views

grep curly-bracketed entries from text file

I have a file with many curly bracketed words, ex. {test1}, {test2} - how would I grep for these words and output a distinct list of them? output example: {test1} {test2} I've tried this which ...
1
vote
3answers
162 views

Cat 2 files and avoid duplication of lines

I have the file1 with the following content {"name":"clio5", "value":"13"} {"name":"citroen_c4", "value":"23"} {"name":"citroen_c3", "value":"12"} {"name":"golf4", "value":"16"} {"name":"golf3", ...
1
vote
3answers
139 views

grep for strings that were found in a previous statement

I'm trying to find out if an address definition is used inside of fortigate configuration file. I extracted the section out of the configuration file, that contains the addresses. In that section the ...
1
vote
1answer
3k views

Reading grep patterns from a file

I have a couple of big text files and in the file UNIQS.txt I have a list of strings to grep from another file. The code I use is grep -f UNIQS.txt EEP_VSL.uniqs.sam > UNIQ_templates.sam which ...
1
vote
2answers
171 views

Is grep part of coreutils? (If not, why?)

I have read in a few websites that grep comes with coreutils (e.g. https://wiki.archlinux.org/index.php/Core_Utilities) However, after building them, I noticed that grep is missing. I wonder if I ...
5
votes
3answers
3k views

How can I grep for this or that (2 things) in a file?

I have a file that has "then"'s and "there"'s. I can $ grep "then " x.x x and then some x and then some x and then some x and then some and I can $ grep "there " x.x If there is no blob none ...
5
votes
3answers
3k views

How to grep for text in a file and display the paragraph that has the text?

Below is the text in the file: Pseudo name=Apple Code=42B state=fault Pseudo name=Prance Code=43B state=good I need to grep for "42B" and get the output from the above text like: Pseudo ...
4
votes
1answer
93 views

grep --include acts like --exclude

I'm trying to make use of the --include option of grep, but it doesn't behave as I expect. Consider this simplified test: Setup me@de31:~/tmp$ cat file.h This is a .h file me@de31:~/tmp$ cat file.c ...
2
votes
2answers
2k views

How can I filter those “Permission denied” from find output? [duplicate]

I started using sed recently. One handy way I use it is to ignore unimportant lines of a log file: tail -f example.com-access.log | sed '/127.0.0.1/d;/ELB-/d;/408 0 "-" "-"/d;' But when I try to ...
1
vote
3answers
723 views

When grepping a file, grep is returning true instead of the lines, why?

I am running a grep on a file that is about 13G in size. It is returning Binary file file.xml matches I was not expecting this, I thought it would return each line with my string so that I could ...
0
votes
1answer
59 views

Is there a grep switch that reads a file for filter terms?

Assuming file a.txt has: 1a 2b 3c 4d so grep "1" a.txt would return: 1a and grep "3" a.txt would return: 3c what if i want to put a criteria file named crit.txt with the following ...
10
votes
2answers
354 views

What is the difference between “*.pl” and *.pl in grep? Why does quoting change the result?

What is the difference between: grep "string" . -r --include *.pl and grep "string" . -r --include "*.pl" The latter includes files in subdirectoried while the former not. Why?
1
vote
2answers
76 views

Grep and regex, Why does the following return only a specific portion?

Why in the following: echo this is a line. | grep -o -E "[a-z]+\." The result is line. and not this is a line.? There is no word boundary or any other restriction on the regex.
0
votes
1answer
50 views

Is -r mandatory for grep?

In a directory I have a file that contains the word string. When I do: grep "string" . no results are returned. But when I do grep -r "string" . the file is returned. Why is -r option needed? It does ...
1
vote
3answers
9k views

How to find a space in a text. using grep?

How to grep one space in text ? cat a.txt| grep ' ' or cat a.txt| grep '\s '
2
votes
1answer
928 views

Issue with real-time log inspecting piping tail, grep and cut

I have to inspect a growing log in real time, and I found an issue that makes me miss some lines (I can't understand exactly what lines) using tail -f or tailf, grep and cut. I use grep because I ...
2
votes
2answers
475 views

Add a suffix string to end of each line? [duplicate]

I have two files: file01: line1 line2 line3 file02: A B C And I want to get line1 A line2 B line3 C I could possibly write a Perl script but it is better if I don't need to. I have tried ...
0
votes
4answers
162 views

Use grep with or [duplicate]

How do I search in a textfile with grep for the occurrence of a word or another word? I want to filter the apache log file for all lines including "bot" or "spider" cat ...
3
votes
5answers
393 views

How to get the lines that contain -R with grep

How can I find "-R" in files with grep? I tried grep "-R" *.GNU to get the lines that contain -R, but it returns nothing.
0
votes
4answers
5k views

Extract substring using regular expression on a Unix file

I have file with below contents . /ABC/RTE/AD_900_VOP_123/OPP /ABC/RTE/TRE/AD_900_VOP_145/BBB /ABC/RTE/AN_900_VFP_124/FBF /ABC/RTE/HD_900_FOP_153/WEW /ABD/RDV/AD_900_VOP_123/OPP ...
12
votes
2answers
605 views

“Ungrep” - which patterns aren't matched

I'm looking for a command or script to do the following - given: file1.txt: abcd efgh ijkl mnop file2.txt: 123abcd123 123efgh123 123mnop123 I want a command that does something like this: ...
7
votes
3answers
5k views

how to count total number of words in a file?

I am looking for a command to count number of all words in a file. For instance if a file is like this, today is a good day then it should print 5, since there are 5 words there.
3
votes
2answers
811 views

Get 'less' to display filename

Something that seemed quite useful to me when looking for a certain var in a bunch of config files: less * | grep some_var So this returns the value of some_var without manually looking through all ...
1
vote
2answers
92 views

Grep and sort several unsorted things in several lines (in one document)

I have a file with several unsorted things in one line which I want to put into a new file This is an example of part of the file I have: X1314448: SaMi|SM_g2554.t1 SaMi|SM_g5072.t1 Des|Des_g3808.t1 ...
5
votes
1answer
136 views

What is the meaning of this test expression []? [duplicate]

I've seen someone use command: ps -ef | grep [h]ttpd and Output is: apache 25125 31006 0 21:54 ? 00:00:00 /usr/sbin/httpd apache 26869 31006 0 22:04 ? 00:00:00 ...
-1
votes
1answer
59 views

How to get all Users and echo if they work or not?

How do I get a list of users(given from the command line) with grep and output if they are logged/work or not ?
2
votes
1answer
6k views

Read log file between two dates

I have to read log file between and my question is how to get only the text in the file wich is between two dates using unix commands? Every line contain date and I want to get the text in the file ...
0
votes
1answer
225 views

How to Get FreeBSD Grep to recognize '-' option?

I am using FreeBSD grep, version 2.5.1, and want to be able to have grep read the pattern from stdin. Other versions of grep I have used allow for the specification of - to enable this feature. ...
3
votes
3answers
131 views

| Grep, Find which file the strings came from

I have a set of PDFs, and I am running strings * | grep message This returns the strings containing that method, is there anyway I can get grep to tell me which file it is from?
2
votes
2answers
460 views

Search class names in jars

I'm using this command to search *.jar files for java classes, and I only get the name of the jars that contain those classes: grep -ral --include=*.jar SignonEJB . How can I also get the class ...
2
votes
2answers
757 views

How to find files with a certain subpath?

I need to find all xml-files that are placed in folders named config. Also config must be somewhere under a folder named trunk. For example, I am interested in all files like below: ...
0
votes
1answer
5k views

How do I redirect output from the find command (that uses grep) to a log file?

Consider the code to search for all files containing the pattern "search string": bash-3.2$ # The below find works fine.. bash-3.2$ find . -type f -exec grep -il "search string" {} \; bash-3.2$ # But ...
4
votes
1answer
1k views

How can I keep color codes with grep input?

It I grep something twice: ... | grep foo | grep bar It seems foo is not highlighted. Infact, it seems grep removes colorcodes from its input; Any way to prevent this?
0
votes
1answer
121 views

How to ask for specific parameters with grep?

I need to look in /usr/share/dict/words for a 5-letter word that starts with d or D, followed by a lowercase vowel, and ends with s. I have tried everything but to no avail.
1
vote
2answers
192 views

Split using sed

I have to split [X] ||| you owns the [X,1] ||| you own the [X,1] ||| 1 0.02020 0.07847 0.36788 3 -0.00000 -0.00000 at the pipes and output those line whose 2nd and 3rd argument are different.
3
votes
1answer
244 views

Understanding grep --label=

I am looking for an explanation how grep --label=LABEL works: Maybe somebody can give me an example [or two] on what --label= is for. I understand what grep and zgrep are supposed to do – the ...
1
vote
2answers
1k views

linux + match IP ADDRESS with 3 octets or with 4 octets

how to match IP address with 4 octets or with 3 octets in one command? target - match xxx.xxx.xxx or xxx.xxx.xxx.xxx ( syntax should fit for Linux and Solaris ) how to merge the following commands ...
0
votes
2answers
2k views

Grep to search directories for patterns inside a text file

I have a file where it contains a list of search patterns (searchPattern.txt). Its contents is similar to the contents below where there are 3000+ of them. 123456 234567 345678 . . . What I wanted ...
72
votes
8answers
11k views

How can I prevent 'grep' from showing up in ps results?

When I search for some process that doesn't exist, e.g. $ ps aux | grep fnord wayne 15745 0.0 0.0 13580 928 pts/6 S+ 03:58 0:00 grep fnord Obviously I ...
2
votes
3answers
641 views

Very strange behavior with grep and IFS

I'm having trouble using grep, the returned results are "n-empty", I mean without the 'n' character... This is the script sample : OLDIFS=$IFS IFS="\\n" i=$(grep -ril $1 *) echo $i IFS=$OLDIFS ...
1
vote
2answers
278 views

Search and replace full line in recursive files

I'm using: grep -n -H -o -R -e textword . List all file recursively under directory '.' with string 'textword' and show the file, line and only portion matching. I need to remove lines that ...