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

learn more… | top users | synonyms

3
votes
3answers
76 views

Print all matching lines and the previous unindented line

For example, lets say I want to find all lines with 'matching' and the previous unindented lines in this text. Container 1 some text some text matching text some text Container 2 some ...
3
votes
2answers
43 views

How to use grep to return lines with an hexadecimal number in a given range?

If I have, say: blah;PC=1234abcd PC=4444bbcd;blah PC=0000abcd;;foo PC=1234abff How do I grep for lines with PC values in a given range, say 1234ab00 to 1234b0ff. The - range option seems to only ...
14
votes
1answer
712 views

How to “grep” for line length *not* in a given range?

NOTE: This question is the complement of this Q&A: How to "grep" for line length in a given range? How can we grep for lines that have less than 8 or more than 63 characters, none ...
0
votes
4answers
71 views

Why isnt this simple grep/sed script working [closed]

I have a variable with, among others, a line that looks like this iBMC=277 I'm trying to match that like this: BMC="$(echo "$allnow" | grep iBMC=[0-9] | sed 's/iBMC=//g')" This makes BMC empty. ...
1
vote
1answer
18 views

How do I get grep to search for ebuilds with specific strings and produce an output containing only the ebuild's location?

I have been trying to create a Bash script function that can be used to search for and open ebuilds with desired strings in Atom (the text editor). This is my current Bash script function (stored in ...
4
votes
2answers
1k views

How to “grep” for line length in a given range?

NOTE: This question is the complement of this Q&A: How to grep for lines shorter or longer than specific thresholds? I need to get only the lines from a textfile (a wordlist, separated with ...
2
votes
3answers
47 views

How to extract particular field from rows from a file, based on matching subsequent rows

I am trying to replicate a workplace problem. I have a xml file like below [~]$ less -N sample.xml 1 <SOURCE BUSINESSNAME ="" NAME ="TABLE1" FOO="ABCD"..... > 2 <SOURCEFIELD ...
4
votes
5answers
890 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 ...
5
votes
4answers
5k views

How to exclude some files not matching certain extensions with grep?

I want to output all lines containing the word OK recursively from a directory. But there are a few extensions which I need to exclude from the result: *~ *.map *.js except *.debug.js I tried: ...
7
votes
5answers
1k views

How to find lines containing more than 100 characters and contains “if”?

So, probably I should use grep for this. As long as I need recursive search I should use grep -r. But then I don't know what should I do next ;) How can I do that?
1
vote
1answer
40 views

How do I grep two patterns from one file and direct the lines to different output files depending on which patter matches?

I'm searching for two patterns from very large files and I don't want to grep more than once. I'm currently running this command to look for the patter, then if found also take the one line before ...
5
votes
2answers
381 views

Find files containing string in file name and different string within file?

I would like to (recursively) find all files with "ABC" in their file name, which also contain "XYZ" in the file. I tried: find . -name "*ABC*" | grep -R 'XYZ' but its not giving the correct ...
3
votes
1answer
63 views

grep two conditions, one negated, without a pipeline?

Assume we have the following test.txt: # commented line, no match # commented line, would match /app/ # again commented, would match app non commented line, matchin /app non commented line, no match ...
2
votes
2answers
39 views

Use of \$ at end of variable name passed to grep

I was going through one of the shell scripts and found a line : ls $dir | grep -c "$NAME.${var}\$" I understand that $ would expand the variables NAME and var, but is it required to put \$ at the end? ...
0
votes
2answers
46 views

how to parse hostname from a line

I have a line : Check space in www.raja.server.local.com and it is 40% full in /usr I want to just parse www.raja.server.local.com from above line. Note: www.raja.server.local.com can be any ...
0
votes
1answer
25 views

what grep -oh “[*?<com]” do?

I am trying to achieve something and in the experiment I came through following case , could somebody explain me how to understand whats happening. echo " Agent process not running on ...
7
votes
4answers
16k views

How to grep for same string but multiple files at the same time?

I have a set of log files that I need to review and I would like to search specific strings on the same files at once Is this possible? Currently I am using grep -E ...
25
votes
6answers
4k views

Pipe to multiple files in the shell

I have an application which will produce a large amount of data which I do not wish to store onto the disk. The application mostly outputs data which I do not wish to use, but a set of useful ...
0
votes
0answers
37 views

How do I use grep to search for strings that exist both on single lines and multiple lines?

I am searching large amounts of SQL scripts for data dependencies but due to variations in the scripts, I have encountered an interesting problem. If I am searching for the word FROM it only returns ...
9
votes
3answers
2k views

Print a line only if the next line does NOT contain a particular match

I am trying to search a log file for logged activities that did not complete. For example, I log a "Starting activity for ID 1234..." and if successful, the next line will be "Activity 1234 ...
8
votes
6answers
2k views

How do I pipe ls to grep, and delete the files filtered by grep?

I wanted to delete some package in my home file, but the filename was too long (google-chrome-stable_current_i386.deb). So, I decided to use the command ls|grep chrome|rm to pipe the files to grep to ...
2
votes
1answer
168 views

GREP exclude first 11 values

I have a massive log file that I need to filter. In this log, I want to display all logs that contain the string dns and I want see them just once. i.e from this: Dec 9 07:24:02 94.15.218.140 ...
1
vote
1answer
49 views

How to Count the Number of Lines of an Output?

Let say I have the program: Calculate.py Is there a unix command-line that counts the number of lines outputted from my program, Calculate.py?
0
votes
2answers
66 views

Search for a specific word in each line and print rest of the line

I have text file with server URL lines like: request get https://abc.net/search?q=hello/world/hello/word/search=5&size=10 request get https://abc.net/search?q=hello/world/hello/world/hello/word=5 ...
2
votes
3answers
2k views

How can I retain the console input in mplayer when reading from stdin?

I'm playing around with the command line interface of mplayer. I'd like to script it in the following way find /some/path/ -type f | grep -vif blacklist | mplayer -shuffle -playlist - where ...
1
vote
2answers
78 views

use find and grep to find lines that contain some string and don't contain other string from files

First, I try the following code find . -name "*.cfg" | xargs grep "good"|grep -v "^\#" In the current folder, I get the list with the file name's extension is "cfg". Then I want to search the ...
1
vote
1answer
47 views

Grep exact numeric match

I am using grep to extract some exact numbers in column #2 of a long file. I am using the following code: grep -e "^71161 |^71072 |^72617 " mainfile > outputfile This does nothing in ...
4
votes
3answers
513 views

Extracting unique piece of text from file with awk, sed or grep

From the output of pactl list sink-inputs, I need to grab the sink input number for VLC. Before that, I'm trying to extract the piece that contains the output for only VLC. All the methods that I ...
1
vote
2answers
18 views

using egrep & regex

Suppose I have the following text file called inputFile: hello HELLO Hello World wOrld asdgfsafd abc 123 1 3 5 a b and I want to print out the lines containing only one word(any sequence with ...
1
vote
1answer
31 views

Matching in sequential order

I have to match multiple strings in a file using grep command, following is the command i am using: grep -e 'str1' -e 'str2' file.log I need all the lines containing str1 and str2 but in the same ...
9
votes
5answers
364 views

Find last occurence of string in multiple files

I need to search multiple log files (all files generated in last 24 hours, all kept in same directory) to find the last occurrence of a string. This is the command I wrote: find . -mtime 1 | grep ...
0
votes
2answers
48 views

Conditional grepping

I have a scenario to handle. Need help with that... I have a config file and its content looks something like below: Jobname|Type|Silo|Description #comment1 #comment2 ...
4
votes
4answers
812 views

Find the last occurence of a string in a given filetype in all subdirectories

I have multiple sub-directories each with different depths. I need to search for the last occurrence of a string in a specific file type (say *.out). How can I accomplish this? I have tried: grep -r ...
4
votes
1answer
130 views

If empty condition behaviour

What is the difference between: if echo " T " | grep -q " F "; then echo FOUND; else echo NOT; fi And: if ""; then echo FOUND; else echo NOT; fi The output of first command is: NOT The output ...
2
votes
1answer
28 views

Find value for a given key using grep in the string

How can I grep SSID value from the string below using grep? SSID: Home, BSSID: aa:bb:cc:dd:ee:ff I tried grep -oh "SSID:.*," and other combinations but to no avail. The reluctant ? quantifer ...
3
votes
2answers
32 views

Search in specific column for pattern and output entire line

I'm working in HDFS and am trying to get the entire line where the 4th column starts with the number 5: 100|20151010|K|5001 695|20151010|K|1010 309|20151010|R|5005 410|20151010|K|5001 ...
1
vote
5answers
108 views

How to use grep to extract file names at end of lines?

I have a log file which contains information such as output using ls *.CCD from directory /var/tmp/Person: -rwxr-xr-x 1 bv90 dkas 153698 Nov 18 13:08 WE994002.CCD I have many such ...
2
votes
2answers
34 views

Can i run grep from cPanel without SSH?

I need to search a large amount of files on my hosting that runs cPanel. They don't have SSH, is there a way I can run grep in any other way to search my files for a certain piece of text?
4
votes
5answers
198 views

Print several lines after nth occurence in bash

I'm looking for a way to search for the nth occurrence of a pattern and print k lines after the pattern. I guess awk would work better than grep in this case but I cannot figure out the way to do it ...
1
vote
0answers
32 views

Extract coordinates of matching text in PDF

Basically I want to grep a string in a PDF and get the coordinates of the matching result(s). I already use pdftotext to do the opposite, namely to extract some text given its coordinates. In ...
1
vote
2answers
902 views

pattern search and display the last occuring pattern?

I have a log file containing startup and shutdown times for everday. I want to see the last pattern for startup and shutdown (which is located at the end of the file being updated everyday). Also I ...
2
votes
3answers
43 views

how to extract a number from the line in unix

I need to find the first occurrence of the attribute ICount and its value must be returned. Only the number from the attribute. How to get it? < FCount = "1" ICount = "0" Ccount= "1"> Please ...
2
votes
1answer
40 views

How to use BRE or ERE regex to match a pattern which does not have a specific string

How to use a posix BRE or a posix ERE regex to match a line that doesn't contain 834 at any place in string? I want to find grep '!<pattern>' file which will be equal to grep -v ...
2
votes
1answer
37 views

Grepping and sedding IP from ip addr show

I'm trying to get just the local IP of my Arch VM. I've managed to get just the line containing what I want with grep, but I also want to trim it down with sed. inet <<192.168.0.16>>/24 ...
1
vote
1answer
30 views

Validate a list of xml files

I'd like to validate a list of xml files against a schema, and only be notified of failures. This command seems to work, but the last grep command has no effect: find -iname "*.xml" | xargs xmllint ...
10
votes
6answers
8k views

How do I recursively grep through compressed archives?

I'm trying to find out what modules use Test::Version in cpan. So I've used minicpan to mirror it. My problem is that I need to iterate through the archives that are downloaded, and grep the files ...
2
votes
5answers
144 views

List (or move) only files with a certain number of lines?

Dealing with a whole bunch of two-line config files, I'd like a way to exclude any files that have a different number of lines. So, something like: mv * destdir only if file contains exactly two ...
1
vote
2answers
61 views

Grep from a line to a line [duplicate]

I have a file with hounded of thousands lines. I need to grep from a line to another line and include everything between these lines i.e lines from Wed Nov 25 23:08:01 GMT 2015 to Wed Nov 25 ...
0
votes
2answers
43 views

Find the number of all typed commands which include a string , using grep

I'm searching through my bash history log for all the typed (started) commands which included the string x. For some reason, the homework checker always says that the number I'm giving is bigger than ...
5
votes
1answer
14k 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 ...