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

learn more… | top users | synonyms

4
votes
2answers
192 views

how to extract parts of file into separate files in linux using awk or grep or sed commands

I have text file as shown below: Input file1.txt ....................... cha21 1 3 5 nar cha21 2 3 6 piy cha23 2 3 5 ram cha23 3 ...
4
votes
4answers
185 views

Insert new lines with missing values (NA)

I would like to insert new lines in text files if there are missing values. I have for example the following text file (A.txt), for which line 5 is missing. In addition, as the file should have 12 ...
4
votes
3answers
635 views

cascaded grep matches color code as pattern

I am piping output of one grep comand into another grep. The first grep is using --color=always, so that the first match is colored. In practice, that means that the match is enclosed between two ...
4
votes
5answers
4k 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 ...
4
votes
2answers
273 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
2k 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 ...
4
votes
3answers
581 views

Use command grep and locate

How I can make the grep command locate certain words in the files specified by the routes found by the locate command? locate my.cnf | grep user (I want that grep command search the word "user" ...
4
votes
1answer
116 views

What is the opposite of “grep”?

Say I'm printing out a long series of strings, and instead of only showing lines containing a certain pattern (which one uses "grep" to do), how do I filter out certain patterns?
4
votes
3answers
2k views

Why is this command not working: “ps aux | grep xscreensaver”

I didn't see any hits about this on google, so I'm asking you: I just tried to run this command in an ssh session, local is Debian Wheezy KDE, remote is Fedora 21 Gnome: ps aux | grep xscreensaver ...
4
votes
7answers
1k views

Grep for a line containing only 5 or 6 numbers

How would you grep for a line containing only 5 or 6 numbers? Something like this. case 1 (has leading space) 10 2 12 1 13 case 2 (no leading space) 1 ...
4
votes
3answers
3k views

Using a perl compatible regex with GNU grep -P

I'm using this regex (?<=\[')[^,]* on a file containing the following line disk = ['OVS/sdasd/asdasd/asdasd/something.img, w'] I want that to return OVS/sdasd/asdasd/asdasd/something.img How do I ...
4
votes
4answers
193 views

Bash-completion and grep

I installed a program (iftop) on Debian Squeeze but some days ago I forgot it's name. I had only this information about the program: 1) It was called *top 2) I could find it using bash-completions. ...
4
votes
3answers
2k views

Why piping find and grep returns nothing?

I'm trying to have grep search inside specified files that are returned by find: find . -type d -name 'mydir*' -exec find '{}' -name '*.java' \; | grep 'MyClass' This doesn't work. Meanwhile, ...
4
votes
3answers
3k views

How to search and replace text in all php-files in a directory and it's subdirectories

I am looking for a shell script that recursively traverses all .php files in a directory and performs a search & replace of a particular text pattern. The search pattern is quite long ( > 5000 ...
4
votes
4answers
934 views

Count lines matching pattern and matching previous line

I want to count the number of times that a role is successfully deleted. The problem is that one line of the log file will show that the script is about to delete the role: Prepare to remove role X ...
4
votes
4answers
4k views

cut / grep and df -h

How can I grep or cut the "173G" under "Verf"? I need this for Unix scripting in school. jonas@jonaspc:~/$ df -h /dev/sda2 Dateisystem Größe Benutzt Verf. Verw% Eingehängt auf /dev/sda2 ...
4
votes
2answers
11k views

What is the python equivalent of grep -v?

I like grep -v. I use it all the time. But I am also doing some text processing in python, and there is one crucial thing that I lack. Usually, I use grep -v to take extraneous stuff out of text. ...
4
votes
2answers
960 views

grep caret appears to have no effect

I was under the impression that a caret symbol means "beginning of the line" with Extended POSIX regular expressions. However, when I use it with grep it behaves unexpectedly. #grep --version GNU ...
4
votes
2answers
174 views

Characters visible in vi, but not in cat.

I have a 300 lines file with ^@ character between each characters in the file. (I cannot post the entire contents for security reasons, so I am pasting only the first line) [mercury@app01 ftp_logs]$ ...
4
votes
2answers
326 views

grep for repeated values from pattern list

I have file A.txt which is a lists of unique ids (column 1) and its corresponding values(column 2): A.txt ABC1D_T1_B1 123.4 ABC2D_T1_B2 146.7 ABC3D_T1_B1 567.8 ABC4D_T0_B2 96.8 ABC1D_T0_B1 ...
4
votes
1answer
4k views

command: ls /etc | sort | grep d* is yielding no results but ls /etc | sort | grep p* lists entire directory

I am playing around with piping and grep tonight. I know that grep uses regex and that * means 0 or more occurrences of the preceding character. So the way I understand it is that if I do the ...
4
votes
1answer
558 views

Literally grep the only word in a line

I know grep -F can search for literal strings, but I want to do something like while read word; do grep -F "^$word$" FILE; done < wordlist i.e. the words need to be treated literally, at the ...
4
votes
3answers
3k views

GREP / SED or AWK: Print entire paragraph in a file on pattern match [duplicate]

I have a file with hundreds of paragraphs of around 15 lines each. I need to search for a pattern, say Occurance: 1. If this pattern is found in the para, I need to print the entire paragraph. Note ...
4
votes
1answer
3k views

Why does grep sometimes show the file name, sometimes not?

Can somebody please explain to me why I don't get the full path in example 1? example 1 - returning found line grep MODIFY /opt/releases/packages/cr_c_cr6/sas/dbms/*/*; outcome: MODIFY example 2 ...
4
votes
4answers
707 views

How can I grep for a string containing regex metacharacters like $ and '?

I want to search all files in the current directory and below for the substring $form['#node']. So, I have tried, initially: grep -R "\$form\[\\\\'" . and grep -R "\$form\[\'" . But it returns no ...
4
votes
2answers
1k views

How to adjust column width in Linux command output

When I used this command on Red Hat Linux /usr/sbin/ss -i I get the following output below: State Recv-Q Send-Q Local Address:Port ...
4
votes
3answers
1k views

grep how to suppress display of non-matched file?

I am trying to find files containing a specific word using grep. There are many files in the directory (> 500) Command I run $ grep 'delete' * Output validate_data_stage1:0 validate_data_stage2:0 ...
4
votes
5answers
802 views

grep second time faster

Suppose I do a time consuming recursive grep search. After seeing the results I want a different output; for example I want to add the option -C 3 for 3 context lines. I can do the whole search again ...
4
votes
1answer
193 views

How can I act on the results of the “locate” command?

I'm trying to find where check_dns is defined in nagios' commands.cfg file, although there are quite a few files. I know I could run something like find / -name "command.cfg" -exec grep check_dns {} ...
4
votes
2answers
1k views

git grep colors differ from grep custom colors

I've setup grep colors in my ~/.bashrc : export GREP_COLORS='ms=01;34:mc=01;34:sl=:cx=:fn=35:ln=32:bn=32:se=36' They work for grep --color=auto grep --color=always Unfortunately, those custom ...
4
votes
6answers
2k views

Grep a directory and return list with line numbers

I'm currently trying to learn more about bash scripting and all of that fun stuff, and I pieced together this little command: find $path | xargs grep -n $pattern | awk '{print $1}' While this DOES ...
4
votes
2answers
220 views

Capture in shell variables the arguments after a command (between brackets)

Suppose I have a file that contains, among many other things, \command{arg1,arg2,arg3} (arguments been paths, expressed with /, ., characters and numbers) But that a user can as well call it ...
4
votes
2answers
2k views

Limit grep context to N characters on line

I have to grep through some JSON files in which the line lengths exceed a few thousand characters. How can I limit grep to display context up to N characters to the left and right of the match? Any ...
4
votes
2answers
861 views

ack/grep search and sort by modification date

Is there any way to have ack sort the results found by date of modification? (ideally showing the date next to the result?). It doesn't look like ack has a date option, but just in case. If this ...
4
votes
1answer
112 views

unexpected output with grep

I am using pyrit. For those who dont know, pyrit allows among other things to check if a wpa handshake key is valid: pyrit -r file0.cap analyze when a file does not contain a valid key, here is ...
4
votes
2answers
4k views

To grep 20 characters after and before match

I have the problem that I get too much information after the match for grep -RnisI --color=auto "pseudomonas" * I want to get only like 20 characters or 10 words after and before the match. What ...
4
votes
1answer
200 views

Why does grep treat '[D]ebug' string differently?

I've created simple text file named "T" to test the unusual behavior of grep: 1 Debug 2 debug 3 determined 4 Determined Tried different syntax: $ grep De T 1 Debug ...
4
votes
2answers
2k views

Tail -f piped through grep not outputing to file, but outputs to console

I'm using the following command tail -f /mydir/myfile | grep "searchterm" >> outfile Without the -f it works fine, but with the -f, which I need, nothing is written to the file. The ...
4
votes
3answers
2k views

shell script to read from multiple files in parallel

I need to write a script that runs parallel and looks for a string in multiple files. I tried a lot of options but they slow down the speed of my processor.
4
votes
2answers
562 views

Searching for string in files

Is there an easy way to search inside 1000s of files in a complex directory structure to find files which contain a specific string within the file?
4
votes
1answer
283 views

Strings enclosed in '' are ignored when using grep

When I tried searching for a string within a file, the results didn't include those that were enclosed in single quotes. For example: grep -rn text folder/ The results didn't include strings the ...
4
votes
1answer
100 views

Find the files between two folder dates in linux?

I need to find the folder names along with the file names which has my string in their contents. I am in this directory "/data/queue/data" and I have lot of folders in the same directory and inside ...
4
votes
2answers
1k views

how to use grep for specific files?

for example I type ls -altr | grep "23 Dec" so it will show files for 23 Dec only and I want to use grep for them, like ls -altr | grep "23 Dec" | xargs grep -l "some_string" but this doesn't ...
4
votes
1answer
328 views

Trouble with grep -o regex

I'm trying to use a grep command with the --only-matching flag, but it's not behaving as I would expect it to. This command: echo "1/2/3/4/5" | grep -oE "^([^/]+/){0,2}" Gives this output: 1/2/ ...
4
votes
2answers
116 views

Regular expression to match sequences of alphanumerics

Any way using grep/awk/perl to match lines in the form *.* = *.* (i.e. ac4df.bx5dfd8g = ce5def.dd5f7gdgf ) Where * means 0+ alphanumeric char?
4
votes
2answers
129 views

Error getting the string between two patterns

I want to get a string between two patterns. The pattern is the first environment <p> </p> in an html file. <p>Sorcery, R (1) </p> <p ...
4
votes
1answer
1k views

How to trick an init script into returning 0

I have a init script which is poorly designed because it does not conform to the Linux Standard Base Specifications The following should have an exit code of 0 if running, and 3 if not running ...
4
votes
4answers
406 views

Grep of many keywords over many files: speeding it up

I am currently facing a "performance problem" while using grep. I am trying to locate the occurrences of many (10,000+) keywords in many (think Linux kernel repository size) files. The objective is to ...
4
votes
1answer
2k views

Escaping of meta characters in basic/extended posix regex strings in grep

Is it possible to escape all meta characters of a string inside a variable before passing it to grep? I know similar question has been asked before on SE (here) and also a good explanation here, but I ...
4
votes
2answers
156 views

How costly is case insensitive search as compared to case sensitive search?

I dont think grep -i is exponentially (wrt to the number of characters to be grepped) more costly (time wise) than a normal grep because the run times are not too different. But theoretically it ...