Questions tagged [grep]

grep is a Unix command-line utility that searches input for lines matching (or not) a regular expression and prints the results.  Use this tag for questions about how to use grep features, how to write regular expressions that work with grep, and general usage.  Because grep is now available in Unix-like environments (e.g., Cygwin on Windows), you can also use this tag for compatibility questions, and differences between versions and software environments.

Filter by
Sorted by
Tagged with
5 votes
3 answers
7k views

How do you search for strings in a file which in turn is in an archive?

I can search through a number of files for a particular string by pipelining 'grep' and 'find' commands. Now, I have some 10 zip files each of which has archived a lot of text files. Is there any ...
2 votes
2 answers
633 views

Grep: Count specific string in all recursive files with matchi filename

I already could manage to count strings in all files in directory: grep -roh „XXX" . | wc –w But I don't know how to isolate the statement. I only want to count the words in files which have the ...
0 votes
2 answers
1k views

How to restrict grep to search only below a token of a column?

How can I use grep to search only the values of a column below a token? For example in the following CSV file: A B C 1 4 5 2 3 0 1 3 6 ...how can I get only ...
0 votes
2 answers
3k views

grep to display all IPs in a text file (linux)

I'm writing a script to look for a ping from a certian foreign IP, but I am having some trouble understanding how to format my "grep" command.. Inside my text file is the output from "netstat anp" ...
0 votes
1 answer
1k views

Perform a GROUP BY-like command in UNIX

I am having a text file like the following: 1 2 3 4 5 6 7 8 9 ... n <-- column numbering 1 0 0 1 0 0 0 1 0 ... 0 0 1 0 0 0 0 0 0 0 ... 1 1 0 0 0 0 0 0 1 1 ... 0 0 1 1 1 0 1 0 0 0 ... 0 0 1 0 0 ...
6 votes
3 answers
14k views

Using find or grep to locate filenames with accented characters from a different encoding system (Windows to Linux)

I tried to tag late onto a question similar to mine on stackoverflow (Find Non-UTF8 Filenames on Linux File System) to elicit further replies, with no luck so far, so here goes again... I have the ...
0 votes
0 answers
466 views

Ping multiple domains from file and out only desired IP

Need bash script to ping multiple domains from file and output just domains with adress ip : 1.2.3.4 exemple (domains.txt): www.google.com support.mozilla.com stackexchange.com support.mozilla.com ...
1 vote
2 answers
3k views

grep: return NULL if false

I'm trying use grep to extract attributes from a large collection of XML files. I've tried usinggrep -E -m 1 -o -Z "<tag>(.*)</tag>" /home/somepath/*.xml || printf "NULL" but for some ...
14 votes
3 answers
23k views

Better way to do "echo $x | sed ..." and "echo $x | grep ..."

I often find this in scripts (and, I have to admit, write it myself): a=$(echo "$x" | sed "s/foo/bar/") or if echo "$x" | grep -q foo then ... fi Consider "foo" to include some regex stuff. I ...
0 votes
2 answers
192 views

remove pattern with random numbers (digits) from php files

I am trying to remove or replace the digits ((random numbers)) in big numbers of php files using find & xargs and grep with sed etc .. the numbers looks like that inside the php file /web/...
0 votes
1 answer
515 views

How would you count every occurrence of a term in all files in the current directory?

How would you count every occurrence of a term in all files in the current directory? - and subdirectories(?) I've read that to do this you would use grep; what is the exact command? Also, is it ...
1 vote
1 answer
280 views

How to find the last 2 characters of Chinese names with command lines tools?

I have a list of Chinese names, some of them have 2 characters, others may have more or less, like following: I want to use some command lines tools, to find the last 2 characters of each. If doesn't ...
0 votes
1 answer
55 views

Save text from file between two given characters and save the output to file?

I have a file with the following kind of text: @ heading with some text the.actual.text.of.choice which.can.be.several.lines in.length.with.no.pattern + more.text.that.i.want.to remove.several.lines....
0 votes
1 answer
4k views

how to move files that contain string using find, xargs, grep, in linux

i have many php files maybe over Three million.. how can i use grep with xargs and find with mv to find contain the string in files and move them to directory if they match. I try like that find . -...
1 vote
1 answer
153 views

Converting from vim search to sed

I have a long Latex document with many sections. I'd like to check that all sections of the document are filled in. So I search for any time there is a '}' ending a section title, somewhitespace, and ...
0 votes
2 answers
2k views

How do I search for a pattern using GREP that must occur AFTER the nth occurrence of another pattern on the same line?

I have a file with records that are pipe ('|') delimited. I need to search for a specific string on the 6th column only and then return the entire line through the grep utility. There are totally 10 ...
1 vote
3 answers
973 views

grep on Windows 7 Invalid Argument

I am using grep from Gnuwin32 on Windows. Doing: grep -r INSERT *.sql Yields: grep: *.sql: Invalid argument Any idea why?
0 votes
2 answers
2k views

How to grep a whole word containg a dot (.)

My file contains the following text xyz.com 123 321 xyz.com.br 123 123 abcd.xyz.com 123 321 I want to grep the line which contains xyz.com alone. ie, the output should be the first line....
1 vote
1 answer
907 views

Why does grep -r --include=*.js * > dirname/.filename not write all .js files to a single file?

I am trying to put all js files in a directory and sub directorys into a single file in lexical order. What did I do wrong?
0 votes
1 answer
637 views

Gnuwin32 grep: piping output on cmd.exe or Powershell

I' m trying to use the Gnuwin32 grep command to produce a list of files I would like to feed to another command (a Perl script really). I have a bunch of HTML files and I wish to list every one of ...
1 vote
2 answers
9k views

any way to run grep backwards, i.e. from the end of the file and up?

I have a 70G+ log file, and i'd like the most recent entries (apache log append new items at the end) that match a pattern. i can either: run grep | tail or run tail | grep Option 1 will take ...
8 votes
2 answers
56k views

How do I grep multiple keywords from a file?

I want to use Unix's grep function to pull out multiple lines (with different keywords) from a file in one command line. For example, I have something like: doc-A1-151 file-A2-15646 table-A3-1654 ...
3 votes
2 answers
356 views

Performing PowerShell analog of *nix grep <multiple files by mask>

Is there a way to optimize PowerShell code below (it greps particular lines by string contained from a bunch of text files into a single one): $ErrorActionPreference = "Continue" Start-Transcript -...
1 vote
1 answer
133 views

Searching across multiple documents for common words

I have the lyrics to a song. (.txt) I also have lyrics to 50 other songs. I'm looking for a way to analyse/search those 50 song lyrics with the lyrics to the first song, and find which one of the ...
3 votes
0 answers
1k views

How to print absolute paths with git grep

I use the following grepcode command to recursively search a large number of git repositories. It prints the path of found files relative to the repository root, how can I print the absolute path of ...
0 votes
1 answer
3k views

Extracting lines from a large text file which follows a pattern

How do i extract lines from a text file which satisfy this regex ^2. I am using linux. I know grep is able to extract the number of lines but i would need them to be output to a text file. Is it ...
3 votes
2 answers
97 views

How can I find a duplicate copy of an image file?

I have an image file, and I know there's another copy of it somewhere in a directory or one of its ancestors, but it has a different, unknown file name, and there are thousands of images in this ...
1 vote
3 answers
1k views

Use grep to find files and pipe/open to open them

How can I pipe files from grep to open all matching files? I've tried grep -li "type" * | open, but that does not work.
0 votes
1 answer
111 views

Removing lines in text document that end the same

We have a script that downloads a list of domains for our squid box to block, but we keep getting warnings like the following: 2015/03/02 17:08:47| WARNING: You should probably remove '.artnau.com' ...
0 votes
1 answer
143 views

Awk eqivalent to grep -C <num>

I want to print 5 lines of context around each line that matches "foo" but not "foobar". I can do the match easily enough via <output> | awk '/foo/ && !/foobar/' but I can't figure out ...
0 votes
2 answers
59 views

Linux : let process run per CPU

My server has 4 CPUs, and I'm parsing a file using grep. It's very slow using just one process. Is it possible to let run four processes using these 4 CPUs? How to do it ? Thanks.
1 vote
1 answer
1k views

Grep matching lines but return matching key IDs in GnuPG output

Having this input: rsa2048/C7927B82 2015-08-30 rsa2048/FB2D99F9 2015-08-30 I want grep to only return the key ID, for example: C7927B92. Using the pattern from Grep characters before and after match?...
-1 votes
2 answers
167 views

grep - mark this line, which contains even quantity selection

I would like to mark this line, in which there are even quantity selection. In this case I want to mark even number of digits too. for example: for input: 22 34 423 22 23213 34 345 23 Result should ...
1 vote
1 answer
923 views

grep - mark the same digits in the line bash

I want to mark the same digits, which can repeat 3 times. My command: grep -E '[[:digit:]]{2,3}' file mark every digits, but I want to one the same digit. Input: 11111 222 33 ...
1 vote
1 answer
540 views

Can using a SSD speed up "grep"?

I need to use grep to parse a 50GB file, and it's very very slow on my server. I wonder if it'll be faster if I replace my server disk with SSD? Is it worth the upgrade? Thanks. Edited: This is ...
3 votes
1 answer
9k views

how to grep something with a wildcard path?

I want to grep a Gemfile in few rails apps. But for each rails app there are many branches and out of which the latest branch name lets say is 'main'. The structure is something like this: workspace ...
1 vote
3 answers
284 views

Error extracting data from a text file, with grep command in bash

I generate an output file named "silence.txt" with ffmpeg. When I try to extract lines containing the word "silencedetect", I've got a strange extract. Here is my original "silence.txt" file : ...
3 votes
1 answer
60 views

How can I mark two columns in the middle of text

How can I mark two columns in the middle of text. grep --color '^[[:alnum:]]*[[:blank:]]' file This mark text in the beginning, but I want to mark second and third columns. How can pass first one?
1 vote
1 answer
6k views

CygWin: grep command shows some "binary file coincidence" message

I have been googling about this, but, maybe due to my spanish version of CygWin/Windows, I have not been able to find a solution. I have latest (December 2014) CygWin 32 bits installed, and it seems ...
-1 votes
1 answer
781 views

bash change strings with multiple escape characters

Hello i try to automate for example change one variable in php file . root /tmp # cat defaults.inc.php|egrep "reply_all_mode" $config['reply_all_mode'] = 0; I try to first catch this by grep ...
1 vote
0 answers
84 views

Grep exit status not returning expected value

In file "WAP" word is there. When I am running grep command grep -i "WAP" filename. I am getting exit status 0. That is fine. But when I am searching for those words which they are not in file. I am ...
2 votes
0 answers
109 views

Search text in zipped files and move them to other folder

Friends, On my Mac, I have 100s of zip files in a directory named Test. Let's assume I have files a.zip,b.zip,c.zip....... Now I want to grep all files for a text "searchText" and move the matching ...
0 votes
2 answers
356 views

How to insert lines into specific files in which a string is not present?

I'm looking for some help with a small script. I would like to search all files corresponding to /usr/local/directadmin/data/users/*/httpd.conf for the string centralized.log If the string is not ...
1 vote
1 answer
375 views

xrandr | grep " connected " give me more than one line but IFS=$'\n' read can't break it

I have something like this: declare -a DISPLAYS AUXILIAR="$(xrandr | grep " connected ")" IFS=$'\n' read -ra DISPLAYS <<< "$AUXILIAR" Why it isn't detecting line breaks if echo "$AUXILIAR"...
1 vote
1 answer
272 views

How to grep in Windows for for eval code?

I have written this code in Linux, How can I achieve the same in Windows server ? find "dirname" -name "*.php" -exec grep -PHl "(eval\(.*\);)" {} \; Basically I want to find out this pattern ...
3 votes
1 answer
576 views

When piped with ls, grep in solaris is not working

I am using the below command in solaris ls -l | grep '*PROC*' But it is not working. I have many files that contain "PROC" in their name like "XREF_PROC.complete" but when I use the above command ...
0 votes
1 answer
835 views

List files that have both keywords with grep

I would like to list the files (using grep -r) that have both of the two keywords (not necessarily on the same line): word1, word2. When I do the following, I get the files that have word1 or word2: ...
2 votes
3 answers
2k views

How to use grep or awk to save every line from text file as new file?

I am a scripting beginner. What i am trying to do. I have a text file that looks like this (example): hello hi there apple I am trying to make a script that saves every line as a separate new file ...
0 votes
1 answer
250 views

how do I grep only the user created profiles among all users in linux ?

From the below output , I just want celesial and neetish user I can't figure how to do it though . celestial@celestial:~$ sudo repquota / *** Report for user quotas on device /dev/disk/by-uuid/...
1 vote
1 answer
5k views

Error with command cut: the delimiter must be a single character

I am trying to run this command from console: # HTTPDUSER=`ps aux | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data|[n]ginx' | grep -v root | head -1 | cut -d\ -f1` But got this error: Try `cut --...

1
11 12
13
14 15
23