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

learn more… | top users | synonyms

17
votes
6answers
914 views

How can I highlight matches using GNU grep?

I have seen on some Linux, that grep is configured to highlight the match, and print the matching file. How can I configure grep to highlight matches?
2
votes
1answer
35 views

Get each column which contains multiple spaces?

My file: subnet 172.25.10.0 netmask 255.255.255.0 { option domain-name-servers 172.25.10.9, 8.8.8.8; default-lease-time 300; max-lease-time 7500; range dynamic-bootp 172.25.10.10 172.25.10.30 ; ...
0
votes
1answer
43 views

Quote escaping struggle with grep and sed

I'm writing a bash script to extract data from html tags and I found this command in another thread : grep -o '<tr>.*</tr>' HTMLFILE | sed 's/\(<tr>\|<\/tr>\)//g' > NEWFILE, ...
5
votes
4answers
124 views

Search a pattern and print preceding lines starting with another pattern

I need to grep for a pattern and once I find it, I need to look for another pattern preceding that and print all those lines in between those. Better example: 1 a 2 a 3 a 4 a 5 a 6 b 7 c 8 d 9 xyz ...
-4
votes
1answer
101 views

What command in bash to search for this string?

file.txt contains: aa=testing sss=whoiam bbb=findme What command do I use to search for testing and print it? Expected result: # you search testing
4
votes
1answer
82 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
6answers
233 views

grep with a lot of files which contains a lot of lines?

Imagine we have a lot of huge(30000 line)files: a.2014-05-06, a.2014-05-07, a.2014-05-08 and so on. I know that we can grep a line with word using this command: grep "word" a.* First I guess this ...
3
votes
2answers
141 views

Getting the combined count of all occurences of a string under multiple directories?

I have a (hopefully) interesting problem that I could use some advice on. I have a system that's essentially used for storing logs. It has a directory structure like: YYYY/MM/DD/hostname/ There are ...
0
votes
0answers
31 views

Get only files specified in egrep

I could to like ask how to get only the files that contains or match with this command [ "$(egrep -l 'GE.[100|120|140|159].' /home/test_dir/* xargs egrep -l 'EA.[NT|[HD|HS|QR].')" ]; My problem ...
4
votes
1answer
1k 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 ...
6
votes
1answer
607 views

find words with all vowels in order from a file

I have a large dictionary file with 300,000+ words in it and I'm trying to find all words with the vowels aeiou in that order and have only exactly 5 vowels. My current attempt does not seem to be ...
-2
votes
2answers
275 views

Extract certain column from CSV files [closed]

I have a directory with a group of CSV files. All files have the same column headers. I need to extract values in a certain column from all files. The common part in files names is ...
5
votes
4answers
227 views

How to replace m part of Lines with n part of Lines?

I have a file "test" like this: RewiteEngine On RewriteCond %{REMOTE_ADDR} !^192\.168\.1\.7$ [NC] RewriteCond %{REMOTE_ADDR} !^192\.168\.1\.8$ [NC] RewriteCond %{REMOTE_ADDR} ...
0
votes
1answer
91 views

Print lines if pattern is not matched

Print lines of File 1 only if they don't contain patterns specified in File 2 Exemple: $ cat File_1 L1 pattern1 L2 pattern pattern2 L3 pattern3 pattern L4 pattern4 $ cat File_2 ...
5
votes
2answers
283 views

How do you get fgrep to find the literal “--help”?

fgrep --help | fgrep "--help" returns just the whole fgrep --help, how do I return just the lines that have the literal "--help" in them? The quotes don't do anything, nor does \-\-help.
7
votes
3answers
3k views

What are the exact differences between awk and cut with grep? [closed]

We know that we can get the second column of the line we want from a file using these two techniques: awk '/WORD/ { print $2 }' filename or grep WORD filename| cut -f 2 -d ' ' My questions are: ...
2
votes
2answers
84 views

Will grep go faster with line breaks?

I have some big files that I have changed into "standardized form", removing line breaks. I want to speed up greps on those files. I know that you can optimize grep by doing grep 'partofpattern' | ...
2
votes
1answer
31 views

Grep patterns in selected lines

How to keep rows containing pattern 1 only if the next line contains pattern 2 and starts with pattern 3 ? Exemple: keep line starting with an A only if the next line starts with a B and contains ...
3
votes
1answer
437 views

How to export data in the terminal to a file (e.g. csv file)

I am a complete beginner in using Linux and I would like to know how to export a bunch of data in the command line to a csv or spreadsheet file (or even text would work). Right now I am simply using ...
-1
votes
2answers
87 views

How to use grep to find all instances of one .txt file in another?

How would I use grep to compare if any line from one .txt document occurred in another? Further how would I use grep to find out the opposite? Would it be grep -v?
2
votes
4answers
480 views

Matching string with a fixed number of characters using grep

I am trying to find all 6 letter words using grep. I currently have this: grep "^.\{6\}$" myfile.txt However, I am finding that I am also getting results such as: étuis, étude. I suspect it has ...
4
votes
1answer
56 views

grep variable-length optional text

I would like to search for edge-anchored text that has a block of preceding optional text, any amount of which can be included in the match. For instance, supposing I am trying to find ^xyz but would ...
3
votes
0answers
21 views

agrep with directionally biased penalties

I am using tre-agrep, an implementation of agrep, to perform approximate pattern matching. This utility searches for matches based on Levenshtein distance, and the user can configure the penalty ...
0
votes
1answer
40 views

For a given directory, how do I concatenate the tail end of recently modified files to a new file?

In case there's a better solution to the problem, my goal is to be able to quickly analyze ALL recent apache activity in /usr/local/apache/domlogs. The usefulness of this is when the server load is ...
8
votes
3answers
17k views

How do I count the number of occurrences of a word in a text file with the command line?

I have a large JSON file that is on one line, and I want to use the command line to be able to count the number of occurrences of a word in the file. How can I do that?
0
votes
2answers
37 views

How to list similar lines?

The delimiters could be: " "; "_" INPUT: foo#foo_ehh113#The password of user 111 will expire within the next seven d foo#foo_ehh204#The password of user 111 will expire within the next seven d ...
0
votes
3answers
157 views

grep substring between quotes

Let's say I have a string like this: title="2010-09-11 11:22:45Z" How can I grep the date itself and disregard the quotes/title/Z? The file can contain more strings like: randomstring ...
7
votes
2answers
580 views

Why is `tac file | grep foo' (piping) faster than `grep foo < <(tac file)' (process substitution)?

This question was motivated by "Reverse grepping", about grepping a huge file from bottom up. @chaos said: tac file | grep whatever Or a bit more effective: grep whatever < <(tac file) ...
7
votes
4answers
5k views

Find the exact string with grep

by way of example, I've a big text file with many email address, using bash I need search/verify that an email exists (or no). Should be use (only) the "anchors"? grep '^user1@example.com' text_file ...
0
votes
1answer
93 views

how do I recursivley grep (or otherwise search for) hex strings

Long and short I'm trying to track down some files left by evil scum on a server. fgrep -r "STRING" /path/here has been working well for finding general stuff. However, how do I search for hex ...
1
vote
2answers
145 views

List all the html tags in a file

I want to know if there is a way to list all the html tags in a file. Lets say I have a file file.html <html> <head> <title>Test</title> </head> <body> This is a ...
0
votes
3answers
84 views

Grep display both matched and umatched lines [duplicate]

How can I use grep command to display both matched and unmatched lines? Matched line should be in red and other lines should be in normal color. Is there a grep option available to do that?
3
votes
2answers
7k views

Search string in many files on HP-UX

I need to find which files (they can have space in the filename) of a directory contains a string using only sh and system's commands (Perl is not an option). For a few files, this command works ...
3
votes
5answers
133 views

How to “grep” a string of a returned result

How to grep the number 50.0 from the string temp=50.0'C In JavaScript I can use the expression /temp=(.*?)'C/ to get it. But I cant get it running in grep.
2
votes
3answers
2k views

Command-line tool to search docx files

Is there a command-line tool to text-search a docx file? I tried grep, but it doesn't work with docx even though it works fine with txt and xml files. I could convert the docx to txt first, but I'd ...
0
votes
3answers
333 views

Grep finding phone numbers in files

How would I look for files in the /etc directory (but not subdirectories) that contain a standard United States long distance phone number, written using the pattern of 1-###-###-####, where each # is ...
0
votes
2answers
736 views

Solution to integer expression expected [closed]

So I when I run this code on my mac there are no errors, and it provides me the perfect output. But when I run it on Ubuntu or CentOS i get the following error integer expression expected ...
1
vote
3answers
372 views

How do I Copy and Paste lines between a start and end keyword?

I have two text files, and I want to copy a bunch of lines from one to the other. File one has a list of packages, and I want to copy it to list two. This list of packages isn't at the start of file ...
2
votes
2answers
165 views

How do you construct a grep & command to match a number of patterns depending on how many are provided at runtime?

I have exported some MS Word documents to plain text and use this function to parse the contents of the .txt files in the current directory: mo1 () { for i in *.txt; do echo "File: $i" grep ...
2
votes
3answers
238 views

bash - extract filenames from html file containing multiple links

I have downloaded an html file autogenerated by a script on a webpage. The file contains multiple links, including links to images I am trying to extract the full names of the images, for example ...
2
votes
1answer
597 views

How do I use grep to find a text string in files in sub folders of a parent folder without -r switch

I'm using Solaris 10 and have two grep versions one in /usr/bin and one in /usr/xpg4/bin. I have been searching for an answer on how to search for text within files within sub folders of a parent ...
5
votes
1answer
708 views

Why do I have to escape a “dot” twice?

I know that we can escape a special character like *(){}$ with \ so as to be considered literals. For example \* or \$ But in case of . I have to do it twice, like \\. otherwise it is considered ...
1
vote
2answers
64 views

Have grep output spaced for each file

When I do grep on a bunch of files, and several file have the word I am looking for, the output is hard to read as it's one after the other. Is there anyway to insert additional \n's after the ...
3
votes
1answer
115 views

Grep with spaces suddenly doesn't work

When performing a grep like: $ tail -f apilog_2014.07.09.log | grep "HELLO" | grep "99999" I get the desired output: 12:22:35 server apache2: HELLO FRIEND 99999 12:22:35 server apache2: HELLO ...
1
vote
1answer
100 views

Why does the same sed regex (after grep) fail when run in a bash script vs bash command line?

Here's the script. It is successful when I run it from the BASH prompt, but not in the script. Any ideas? When I say "fails," I mean the sed regex doesn't match anything, so there is no replaced ...
0
votes
2answers
87 views

How to count number of instance of a word starting at another word

I need to grep the MySQL-error log starting at today's date formatted like: `date +"%y%m%d"` (140710) And look for all instances of the word partitioned. I've tried various things to no avail. ...
4
votes
6answers
770 views

ls content of a directory ignoring symlinks

I have a directory in which I would like to list all the content (files and sub directories) without showing the symbolic links. I am using GNU utilities on Linux. The ls version is 8.13. Example: ...
1
vote
6answers
438 views

Print word that doesn't contain character

My file is in the following format: this!,is!,another!,test,yes! this!,is!,another!,column,yes! no,not!,another!,column My output should be: test column no It shouldn't contain the '!' ...
3
votes
1answer
35 views

annotate “include” file for grep with comments?

I do a grep sort that includes certain strings. That is, give me the lines in a file that DO contain certain strings. As in grep -fv include file > out That command looks in file and sends to ...
4
votes
2answers
949 views

tr complains of “Illegal byte sequence”

I'm brand new to UNIX and I am using Kirk McElhearn's "The Mac OS X Command Line" to teach myself some commands. I am attempting to use tr and grep so that I can search for text strings in a regular ...