Tagged Questions

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

learn more… | top users | synonyms

5
votes
1answer
114 views

Search for a text in Linux but don't display the complete line in results

I'm using grep to do this, like: grep someText file.log But this returns the complete line in results. If someText is found in the middle of a line I don't want to ouput the whole line, just the ...
6
votes
2answers
9k views

Reverse grep to get the last couple of occurrences in a file

Say I want to grab the last 5 mentions of cron in /var/log/syslog.log looking over the grep manual it seems like I can't get grep to search in reverse. I can however grab the most recent occurrences. ...
5
votes
2answers
231 views

Is there a archive of old man pages? specifically for the features available for grep in early 'ed'

This is just a bit of history rummaging... I'd like to know how different today's grep is to the original implementation, introduced by Ken Thompson in ed. which evolved to ex, which then became vi... ...
2
votes
1answer
2k views

Grep - ^(\s*$) vs ^\s*$

I want to search for a field that only has spaces. When counting, this two commands gave me different results: grep "^\s*$" -E -c grep "^(\s*$)" -E -c Another thing. What is the correct way to say ...
1
vote
0answers
89 views

How to search for file contents [duplicate]

Possible Duplicate: Finding a substring in files across subdirectories with a single built-in command? I know that I can use grep for a single file, but what about a whole directory tree?
13
votes
4answers
5k views

Grep: Show all the file up to the match

grep --before-context 5 shows 5 lines before the match. I want to show everything before the match. Doing grep --before-context 99999999 would work but it is not very... professional. How to show ...
3
votes
2answers
104 views

Generate suggestions for errors from a lookup dictionary

EDIT: the files were changed to tsv to deal better with spaces in text fields I have 2 csv files in the following form: File 1: availableText.csv (can be very big) "id1" , "text1-1" "id1" , ...
6
votes
5answers
1k views

How can I recursively search directories for a pattern and just print out file names and line numbers

Currently I am using grep as follows: grep -lr <PATTERN> . This gives me the following output, containing a list of all files whose contents (not name) contain : ./path/to/file1 ...
7
votes
6answers
2k views

Is there any alternative to grep's -A -B -C switches (to print few lines before and after )?

grep -A 2 -B 3 prints 2 lines after the grep string and prints 3 lines before. grep -C 3 prints 3 Lines before and 3 lines after Unfortunately, the grep I'm using does not support these ...
26
votes
7answers
5k views

Is there a way to modify a file in-place?

I have a fairly large file (35Gb), and I would like to filter this file in situ (i.e. I don't have enough disk space for another file), specifically I want to grep and ignore some patterns — is there ...
1
vote
3answers
333 views

Switch from grep to sed

I have a files students.txt, with lines of the form: Surname, Forename: Day.Month.Year: Degree For example: Smith, John: 15.01.1986: MSc IT Taylor, Susan: 04.05.1987: MSc IT Thomas, Steve: ...
5
votes
5answers
19k views

Multiline pattern match using sed, awk or grep

Is it possible to do a multiline pattern match using sed, awk or grep? Take for example, I would like to get all the lines between { and } So it should be able to match 1. {} 2. {.....} 3. ...
5
votes
2answers
4k views

What params do I pass to grep to return only file names?

I'm trying to use grep to find a specific piece of text in a bunch of files on my web server. No problem, except that it returns way more information than I want! Ideally it would just return a list ...
6
votes
3answers
9k views

What does \? mean in a regular expression?

The following command is used to search for a 7-digit phone number: grep "[[:digit:]]\{3\}[ -]\?[[:digit:]]\{4\}" file What does \? stand for?
8
votes
2answers
10k views

giving grep output to rm

I'm trying to pipe grep output to rm, but it outputs useless stuff. Is any switch required for rm? Or can rm can be provided a regexp directly? ls | grep '^\[Daruchini'| rm rm: missing operand Try ...
1
vote
3answers
967 views

A problem with find and grep

I have defined the following alias in ~/.bashrc: alias fg='find . -name $1 | xargs grep --color $2' in order to write fg "*.txt" " my_text " and find all file that have extension .txt and ...
2
votes
1answer
122 views

The cursor to a match, edit, the cursor to the next match, edit — like in a directory?

Algorithmic blackout: problem: find the files that match problem: stack/buffer the matches move the cursor to the next file of the stack and thereby to the location of the match repeat step 2 ...
7
votes
5answers
603 views

How intelligent are unix utilities?

Unix utilities like sort, find, grep, diff et al are very handy to perform quick tasks, sometimes without writing any code at all. I wanted to know what algorithms do they internally use and how do ...
6
votes
7answers
16k views

How do I find text within a file and have it search multiple subfolders?

I'm looking for a function name and the folder structure is deep and there are a lot of files to look though. Usually I go with something like find * | grep functionname but is that the best way?
26
votes
3answers
3k views

Does grep use a cache to speed up the searches?

I have noticed that subsequent runs of grep on the same query (and also a different query, but on the same file) are much faster than the first run (the effect is easily noticeable when searching ...
3
votes
1answer
303 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/ ...
2
votes
3answers
754 views

Filtering paths to a specific depth

I'm using rsync and the flags -nPaAXz ~/ to check which files are going to be copied. This is far too verbose to make any sense of. How could I filter the output so I view the list of files/folders ...
11
votes
3answers
2k views

Is there a 'rc' configuration file for grep/egrep? (~/.egreprc?)

I usually do some grep when developing, and there are some extensions that I always don't want to look for (like *.pyc). Is it possible to create a ~/.egreprc or something like that, and put some ...
2
votes
3answers
3k views

Extracting tokens from a line of text

Using bash scripting and grep/awk/sed, how can I split a line matching a known pattern with a single character delimiter into an array, e.g. convert token1;token2;token3;token4 into a[0] = token1 … ...
3
votes
2answers
674 views

Using grep/sed/awk to classify log file entries

I need to process a very large log file with many lines in different formats. My goal is to extract unique line entries who have the same starting pattern, e.g. '^2011-02-21.*MyKeyword.*Error', ...
8
votes
4answers
7k views

Finding a substring in files across subdirectories with a single built-in command?

In Windows, if I wanted to find a string across all files in all subdirectories, I would do something like findstr /C:"the string" /S *.h However, in Linux (say, Ubuntu) I have found no other way ...
9
votes
2answers
3k views

How to remove multiple lines per occurrence in a file?

Say I have this 857835 line file, containing stuff like this: a1 rubbish1 rubbish2 rubbish3 rubbish4 a1 rubbish5 rubbish6 rubbish7 rubbish8 And I wish to remove all occurences of a1 and the next ...
2
votes
2answers
539 views

grep acting strangely

Grep has been acting strangely on one of my systems (Ubuntu Desktop - all my other systems are Ubuntu Server), and I can't figure out why. I created a control file named text that contains the ...
0
votes
1answer
125 views

Locate a directory within an archive

I have .tgz archive i'm trying to locate a directory within it to extra how do i search within a .tgz i tried the following below but no luck. tar -tzf archive-9.tgz | grep '161519' | xargs tar -Oxzf ...
17
votes
5answers
46k views

grep: count total number of occurrences

grep -c is useful for finding how many times a string occurs in a file, but it only counts each occurence once per line. How to count multiple occurences per line? I'm looking for something more ...
14
votes
9answers
15k views

How can I grep in PDF files?

Is there a way to search pdf files using the power of grep, without converting to text first in Ubuntu?
7
votes
4answers
6k views

Number of files containing a given string

How can I count the number of files (in a directory) containing a given string as input in bash/sh?
2
votes
1answer
345 views

How to generate a report summary of messages that triggered a specific DSN code

I use Sendmail on CENTOS 5.x, I hope this is a simple question. =) I need to generate a report summary of messages that triggered a specific DSN code. For example: Jan 11 07:43:34 server-example ...
3
votes
1answer
7k views

Filtering on dates with grep and awk

I have created the alias below in my .bash_aliases file alias auth="grep \"$(date|awk '{print $2,$3}')\" /var/log/auth.log | grep -E '(BREAK-IN|Invalid user|Failed|refused|su|Illegal)'" ...
3
votes
4answers
2k views

How do I check if a file already has line with “contents” in it?

I need to know if a file aready has line with contents X in it, if not append line. here's the code I've tried. if ! $(grep 'eval $(perl -I$HOME/foo/lib/perl5 -Mlocal::lib=$HOME/foo)' ~/.bashrc) then ...
12
votes
5answers
7k views

Adding numbers from the result of a grep

I run the following command: grep -o "[0-9] errors" verification_report_3.txt | awk '{print $1}' and I get the following result: 1 4 0 8 I'd like to add each of the numbers up to a running count ...
17
votes
2answers
2k views

What is a “loop device” when mounting?

I am doing a mounting of an iso file and looking at this tutorial. They use the command: $ mount -o loop disk1.iso /mnt/disk I'm trying to understand the use of -o loop. So I have two questions: ...
1
vote
1answer
245 views

How can I grep through files whose name begins with `-`? [duplicate]

I've tried this. grep -r thanks * slave-iv grep: invalid option -- '@' Usage: grep [OPTION]... PATTERN [FILE]... Try `grep --help' for more information. Here's ...
27
votes
7answers
20k views

How to grep standard error stream (stderr)?

I am using ffmpeg to get the meta info of an audio clip. But I am unable to grep it. $ ffmpeg -i 01-Daemon.mp3 |grep -i Duration FFmpeg version SVN-r15261, Copyright (c) 2000-2008 Fabrice ...
8
votes
3answers
1k views

How do I take a list and remove it from a file?

I have a long list of domain names that I need to remove from /etc/remotedomains. They're probably not in any particular order in the file. Each domain is on one line. How could I iterate through the ...
11
votes
7answers
10k views

grep and tail -f?

Is it possible to do a tail -f (or similar) on a file, and grep it at the same time? I wouldn't mind other commands just looking for that kind of behavior.
10
votes
2answers
12k views

What does ^d mean in ls -l | grep ^d?

When I do ls -l | grep ^d it lists only directories in the current directory. What I'd like to know is what does the caret ^ in ^d mean?
54
votes
7answers
16k views

Is there a basic tutorial for grep, awk and sed? [closed]

I've been a Linux user for a while, and I've a pretty decent understanding of most the common command line utilities. However, ones that come up and up again in relation to programming are grep, awk, ...
8
votes
3answers
16k 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?
6
votes
2answers
2k views

How to make vertical bar | work in grep?

Here's an example of a grep command line I've tried: grep '(565172|565173|565175)' /var/log/cups/error_log It doesn't produce any output, even though all 3 values are in the file multiple times. I ...
7
votes
2answers
993 views

grep - why do brackets in grep pattern remove the grep process from ps results?

Why do brackets in a grep pattern remove the grep process from ps results? $ ps -ef | grep XXXX [...] XXXX [...] grep XXXX $ ps -ef | grep [X]XXX [...] XXXX
1
vote
3answers
505 views

Help with a find command

I have a bunch of directories. Inside these directories is a cover letter and a zip archive of work. I wanted to run a quick and easy query to open up all the cover letters so I can decide if it's ...
8
votes
4answers
8k views

grep — removing text after delimiter token

I have a file in which I need to eliminate everything after the first ; on every line. So a file like this: sdfsdsdf; fsdfsddf;sdfsd; Will result in this: sdfsdsdf fsdfsddf I have looked into ...
3
votes
1answer
260 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 ...
42
votes
4answers
7k views

Convince grep to output all lines, not just those with matches

Say I have the following file: $ cat test test line 1 test line 2 line without the search word another line without it test line 3 with two test words test line 4 By default, grep returns each ...