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

learn more… | top users | synonyms

4
votes
2answers
1k views

How do regular expressions differ from wildcards used to filter files

While we use * to denote zero or more previous characters in grep, we use *.c to find all C files when we use it with the ls command like ls *.c. Could someone tell how the use of * differs in these ...
0
votes
1answer
2k views

grep the only first word from output with grep -P

I'm trying to grep username users |grep "^\b\w*\b" -P how to do it with grep?
0
votes
1answer
101 views

What does this “matching lines means”?

The grep manual says "By default, grep prints the matching lines.". What does this matching lines says. When I tried its prints the whole paragraph containing the pattern.
0
votes
2answers
179 views

Sample file for grep

Can anyone please point to some good sample files that helps to learn grep. This is for academic use. It should be a large one so that students could understand its importance
1
vote
1answer
167 views

Display words in color

I am looking for a tool which takes a file in input and a word to search. It should display the file with color the words if it corresponds to the search. Like grep --colors but displays all the ...
3
votes
5answers
182 views

grep device name and look for next value :

I have this output from a find command: abc,10.11.13.14,def,1.2.3.4,geh,6.7.54.23 where abc,def and geh are device names and could be of any length and others are IP address belong to devices. ...
2
votes
3answers
8k views

Grep command to find files containing text string and move them

I am able to locate files in a folder containing a specific text string using this command: grep -lir 'string' ~/directory/* How do I move the files that appear in the above result to another ...
2
votes
1answer
178 views

grep all words with specified length, without letter x

So I am stuck here: grep -E '^....c...c...$' shows all words with the expr I need. Now from this words I only need the ones that don't have a x. Who can help me?
1
vote
3answers
754 views

How can I use grep to search for lines that start with a certain character in bash

I want an alias ('ggg') that will look through my existing set of aliases and tell me all the ones that begin with g. I have a lot of g* aliases :) I tried this: alias ggg='alias | grep ^g' but ...
4
votes
6answers
6k views

How to print all lines after a match up to the end of the file?

Input file1 is: dog 123 4335 cat 13123 23424 deer 2131 213132 bear 2313 21313 I give the match the pattern from in other file ( like dog 123 4335 from file2). I match the pattern of the line ...
3
votes
1answer
912 views

Modify /etc/xdg/lxsession/LXDE/autostart before boot

I have some parameters that I would like to change in /etc/xdg/lxsession/LXDE/autostart, before it is run. I get the parameters from a grep command. I would like to feed them to the autostart file but ...
2
votes
3answers
657 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 ...
1
vote
1answer
213 views

solaris + fit egrep for solaris

On Linux machines I use this syntax, in order to match all VALID ip's. My question: what needs to change for egrep command in order to run the egrep on Solaris? According to egrep manual page "-o" ...
1
vote
1answer
128 views

questions on find and grep

With respect to the following find command-line find . -type f -exec grep -l strings {} \; I do not understand the usage of -exec and {} \.
1
vote
2answers
212 views

How to find which files contain mentions of a wildcard image name?

What I am trying to do is do a search on my server to find which files contain mentions of images with a certain keyword in the filename but all I can figure out is how to find the filenams (not image ...
10
votes
2answers
4k views

How to run grep with multiple AND patterns?

I would like to get the multi pattern match with implicit AND between patterns, i.e. equivalent to running several greps in a sequence: grep pattern1 | grep pattern2 | ... So how to convert it to ...
3
votes
5answers
2k views

Output multiple files from a single grep?

I'm not very experienced in shell scripting, but I'm trying to understand how to grep for a pattern and for each file where there is a match write a file to disk that contains the matched line from ...
2
votes
3answers
2k views

grep for capital words

I have a file that has declarations in it with capital words, something like: public final Foo BAR; these declarations are not all uniform. Is there a way for me to grep out just the capital ...
8
votes
3answers
1k views

Mark occurrences of keyword with a different color

I have a program which spits out an output to stdout like: [OK] Something is ok [OK] Something else is also ok [FAIL] This does not look good [FAIL] Neither does this [OK] At ...
1
vote
1answer
801 views

Using grep command and backreferencing

This is a pretty basic question, but I can't get my head around it. I'm learning Linux and one of the questions i've been given is; *The word sentimentalment includes the same three characters ...
0
votes
1answer
1k views

Problem with using grep command with env variable containing special characters

I have a variable "abc" with value "xyz*!@#" in my unix account (ksh). I used export command to export this variable into the environment. When I use grep command as below env |grep xyz I get the ...
6
votes
2answers
4k views

How to return both file name and line number with find … -exec grep?

When using find, how do I return the file name and the line number when searching for a string? I manage to return the file name in one command and the line numbers with another one, but I can't seem ...
1
vote
3answers
2k views

Linux: Does find | xargs grep have limitations?

I've historically performed something like: find . 2>/dev/null | xargs grep -i something_to_find 2>/dev/null If my pwd is barfoo (/foo/bar/baz/foofoo/foobar/foobaz/barfoo) it finds matches. ...
2
votes
3answers
498 views

Regex/pattern matching problem to extract data

What is the best way to remove distances in the following lines and output cdv format. Can this be done reasonably easily on the command line? I'd like to avoid having to write a full-blow script. ...
4
votes
4answers
423 views

Most efficient grep method

Currently I am grepping data from a file containing any of the following: ...
2
votes
3answers
478 views

Extract integers enclosed in round brackets

I'm new to Linux. I've a file which contains list of movie names and their release year. I want to list out all release years. Specifically my file looks like below TDKR(2012) Vicky Cristina ...
2
votes
2answers
470 views

Parse apache logs to exclude IP ranges

I need to look for some requests in a huge pile of apache logs. My only requirement is this: I need to only view the requests coming from any IP address that is NOT included in a list of 50 IP ranges ...
1
vote
2answers
210 views

Match a simple Regex

I need to match this: (tt\d{1,10}) in a file with the file ending txt. I have tried grep and ack-grep but without results. ack-grep -G \.txt$ -g "(tt\d{1,10})"
2
votes
3answers
4k views

solaris 10 + display 2 lines after match by grep?

I want to match for example: The string e1000g0 from the lltconfig -a list , and then to display the two lines after the string e1000g0 so I get only the lines: Node 0 du1a : ...
2
votes
2answers
315 views

Web front-end to find/grep/ack

In my lab, we are trying to build a web that allows the user to enter queries for find, ack, grep on a specific directory. The web would return an HTML with a table of a list of clickable files (click ...
7
votes
2answers
441 views

When is it useful to use “grep — SOMEPATTERN”?

So under Linux/AIX where ever.., when is it useful to use: grep -- SOMEPATTERN the Question is the "--" part. When is it usefull to use?
2
votes
4answers
2k views

How to detect dos format files in git bash

Git Bash is a nice bash shell you get in Windows as part of the installation of Git. It comes with other typical unix tools bundled inside, such as grep, sed, awk, perl. It doesn't have the file ...
10
votes
3answers
6k views

What is the difference between grep -e and grep -E option?

I am trying to understand the difference between grep -e and grep -E. Now from grep manpage I got: -E, --extended-regexp Interpret PATTERN as an extended regular expression (see below). ...
2
votes
1answer
3k views

The simplest method to count lines matching specific patterns, including '0' if line is not found?

I have very big logs (several gigabytes per day), that can (but do not need to) contain specific lines. I have to count the number of occurences of every one of these lines on a daily basis. I have a ...
2
votes
5answers
80 views

Generate “item tag” pairs from “item,item,…|tag” lists [closed]

The characteristics of the input data: Each line consists of two fields, separated by a pipe '|', where the first field is a comma-separated list of items, and the second field is a tag. Input ...
4
votes
3answers
718 views

Delete the matching line and several more from a file

I have a text file called file_a.txt. My first command is grep -A 12 ".production =" file_a.txt The output is a few block. Each block of string contains 13 rows I specifically want to delete all ...
2
votes
2answers
162 views

How can I enhance the output of find and grep?

I really don't look forward to having to do find/grep because the output, as returned by find . -exec grep sometext {} \; -print is just not very easy to read even when you dump it in a file. What ...
11
votes
6answers
483 views

How can I grep a directory based on the contents of two successive lines?

How can I grep a directory for lines that contain "Foo", but only get matches when the next line also contains "Bar"?
3
votes
2answers
397 views

how to grep for c:\

I'm using cygwin's grep to find all instances of hardcoded paths on c drive. doing: grep -r "c:\\" . gives grep: trailing backslash Does anybody know how I can do this?
1
vote
3answers
3k views

bash merge 2 lists removing duplicated

I have an embedded linux system using Busybox (OpenWRT) - so commands are limited. I have two files that look like: first file aaaaaa bbbbbb cccccc mmmmmm nnnnnn second file mmmmmm nnnnnn ...
3
votes
3answers
1k views

Average rows with same first column

Given a file with two columns: Id ht 510 69 510 67 510 65 5l0 62 510 59 601 29 601 26 601 21 601 20 I need a way to coalesce all rows with the same ID into one that has an average height. In this ...
11
votes
3answers
2k views

Online grep exercises?

Are there any online grep tutorials that would feature some online tool for performing exercises? For example, having some predefined text sample and being told what patterns to find and so forth? It ...
3
votes
4answers
1k views

Why does grep not work with redirection?

Using the top command with redirection works fine: top > top.log Now I want to use grep to filter a certain line: top | grep "my_program" > top.log But the log file will remain empty. But ...
6
votes
2answers
20k views

Can grep return true/false or are there alternative methods

I'm trying to write this script for an assignment - it's only the second one I've written so bear with me. As a part of this script, I need to be able to check if the first argument given matches ...
3
votes
4answers
327 views

Grep one liner to remove blocks of random text

Using a script to backup some Cisco routers and compare startup and running configs. My output is tarnished by some ssl certificates it returns - I wish to remove them. Given: ********* ERROR ...
2
votes
3answers
432 views

Which is a better way to search- find with regex or find with grep?

Of these two ways of searching a file recursively in all the subdirectories, which is faster / better ? find . -regex ".*/.*abc.*" or find . | grep ".*abc.*"
3
votes
3answers
2k views

How do I use tee to redirect to grep

I don't have much experience of using tee, so I hope this is not very basic. After viewing one of the answers to this question I came across a strange beheviour with tee. In order for me to output ...
4
votes
3answers
347 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" ...
3
votes
1answer
159 views

grep for n or more periods on a given line

I want to be able to arbitrarily match lines that contain a lot of periods, like this: $ echo "there's. someone on the wing. some. thing." | grep -stuFF [[:idunno:]]{4,} there's. someone on the wing. ...
2
votes
5answers
1k views

Need to grep file with specific letter and move to another folder linux

I need to select files which contain "Error" or "traffic" in their names, e.g. abc_ERRor.csv raError.csv bsn_Error.csv bbbctraffic.csv ccc_traffic.csv ... and move them into corresponding ...