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

learn more… | top users | synonyms

2
votes
3answers
25 views

Move line from output to the front of output or custom reordering

I have a command find "$PWD" -name __openerp__.py|awk -F/ -vOFS=/ 'NF-=2'|sort -u and I want to take a certain line containing /web/addons and push it to the front of output then do the same with ...
2
votes
2answers
255 views

How do I check that a filename conforms to a pattern?

I'm tasked with writing a shell script which checks that a filename conforms to a specific pattern and I'm not sure how to go about it. The filename should follow a pattern which looks like: ...
2
votes
1answer
65 views

using grep --perl-regexp

this is a sample output of ls $ ls -lAF -rw------- 1 martin martin 168 Jan 25 23:26 aaa -rw------- 1 martin martin 168 Jan 25 23:26 bbb -rw------- 1 martin martin 168 Jan 25 23:26 ccc I would ...
1
vote
7answers
1k views

Sed / Awk - Find string exactly n characters long

Looking through the man pages for sed, awk and grep, I'm unable to find a way to search for a string that is exactly n characters long. Given the following text file, I want to extract just ...
4
votes
1answer
477 views

Using Modifiers of Perl-compatible Regex (PCRE) in grep

According to grep --help and man grep, we can use the -P option in order to interpret the pattern as a Perl regular expression (PCRE, to be precise), instead of the default POSIX basic regular ...
5
votes
5answers
154 views

A shell script to write selected fields to a single line

I have a log file trace.log which prints the time stamp, thread name and Transaction method and transaction ID as below. 2014-01-23 15:50:41,724 [catalina-exec-35] INFO TRANSACTION getConnection ...
3
votes
1answer
105 views

Can someone clarify this Bash script

I'm learning something about Bash scripting, and I came across this example: The task is to write a Bash script that reads a file, given with the first argument, pulls out those records that have the ...
1
vote
2answers
123 views

Why is my grep + regex not working?

I recently decided that enough was enough -- I was going to learn to use grep fluently. It's been all of three hours and I'm already stumped by this toy problem. I'm currently syncing a RAID5 array, ...
2
votes
1answer
934 views

find + grep to match exact keywords in a file

My script is not matching exact words only. Example: 12312312Alachua21321 or Alachuas would match for Alachua. KEYWORDS=("Alachua" "Gainesville" "Hawthorne") IFS=$'\n' find . -size +1c -type f ! ...
3
votes
2answers
335 views

grep string from contents of file command then delete file

I have a peculiar problem I need help with. I have a large batch of .docx files where a certain number of them are junk. I know how to identify these files from the file command, as they contain the ...
10
votes
3answers
815 views

grep to find instances of “Foo” where “Bar” does not appear within 10 lines

Suppose I want to search an entire tree for all CPP files where "Foo" occurs. I might do: find . -name "*.cpp" | xargs grep "Foo" Now suppose I want to list only those instances where some other ...
1
vote
2answers
278 views

How do I grep a file in a smb mount point without using mount or fstab?

I don't have permission to mount a smb share with the mount command or by using /etc/fstab, but I'm able to use the smb protocol in Nautilus (smb://10.1.1.1/share), for example ... s Is it possible to ...
2
votes
2answers
82 views

help with truncate \ find

This command won't work: $ truncate -s 0 $(find . -size +1c type -f -exec grep -LwE 'keyword1|keyword2|keyword3' {} +) "find: paths must precede expression: type Usage: find [-H] [-L] [-P] [-Olevel] ...
2
votes
4answers
384 views

Using wget and grep to download HTML pages and filter by keywords

KEYWORDS=("dolphins" "fish" "dogs" "cats" "iguanas") IFS=$'\n' find . -type f -size +1c ! -exec grep -qF "${KEYWORDS[*]}" {} \; -exec truncate -s 0 -f {} \; If a file is over 1 byte, search for ...
3
votes
2answers
808 views

test if return true if grep on output has string in one line

I just want to return true or false if a service is running. Here is where I'm at, not first try or last but current at writing: service iptables status | grep -qi "not running" The sdtout for ...
5
votes
3answers
149 views

Mac OS X find/grep generating “unknown --devices option”

I am entering a command I have done often with Linux, Unix perfectly fine but with Apple Mac OS X 10.8 (and probably before) I get a grep: unknown --devices option when I attempt to run the following ...
5
votes
5answers
338 views

Number of files not containing given string

I read the question about how to find out the number of files that contain a certain string. This is possible with grep -l "string" * | wc -l. Is it possible to invert this, finding the number of ...
2
votes
4answers
122 views

Print the string between two parentheses

I have file with these lines G8 = P(G1,G3) G9 = P(G3,G4) G12 = P(G2,G9) G15 = P(G9,G5) G16 = P(G8,G12) G17 = P(G12,G15) I need the output as G1,G3 G3,G4 ..... How can I do it with sed/grep ...
3
votes
1answer
132 views

Grep perl syntax and copyright character

I am trying to pull some links out of text that I downloaded of off the internet using grep, but keep getting "Aborted (core dumped)". When using grep I tend to use -P as I prefer to use perl syntax ...
1
vote
2answers
123 views

How to bury an invisible mark into lines of text?

How can I bury an invisible mark into random lines of text? Such a mark has to be there, though it will be invisible to someone reading that text printed out on the console. I want to identify those ...
-5
votes
1answer
93 views

Copy content of an file “passwd” to an file userlist.txt [closed]

The problem is that the file passwd hasn't got any tag like .txt or so. So I can't copy the content by passed > userlist.txt.
2
votes
2answers
77 views

Pipeline with grep and find prints numbers infront of result

Am using the following "find" command to extract some files, find /lag/cnnf/ -maxdepth 3 -newer ./start ! -newer ./end | grep -nri abc | egrep '([^0-9]45[^0-9])' | grep -nri "db.tar.gz" >> ...
1
vote
1answer
101 views

Grep for single and double digit

i have path name as below, abc4/2012 abc4/2013 abc45/2014 abc45/2014 When i grep for 45 it is showing one instance of 4 and for 4 its showing all 45. i need to grep both single digit and double ...
0
votes
3answers
124 views

Ignore lines containing a capital letter

Why does the following command still return some lines containing a capital letter? aspell dump master | grep ozz | grep -v '[^A-Z]'
16
votes
9answers
986 views

Grep lines but let the first line through [duplicate]

Consider the following output from df. Filesystem Size Used Avail Use% Mounted on /dev/sda1 23G 6.1G 16G 29% / udev 10M 0 10M 0% /dev tmpfs 397M 420K ...
3
votes
3answers
284 views

Grep command in shell script

I've been writing a shell script which should make grep command outputs for further use. However when I pass a variable containing ~/.../multiple_dir/* to grep as input I get empty output file. ...
4
votes
1answer
276 views

Grep multiple pattern negative match

So I have a bunch of Apache logs with using the standard log format. I want to get all the log lines that did not come from a web crawler. So lets say I have a file robot_patterns with entries like ...
4
votes
4answers
2k views

How can I suppress output from grep, so that it only returns the exit status?

I have the grep command. I'm searching for a keyword from a file, but I don't want to display the match. I just want to know the exit status of the grep.
4
votes
1answer
1k views

Why does grep sometimes show the file name, sometimes not?

Can somebody please explain to me why I don't get the full path in example 1? example 1 - returning found line grep MODIFY /opt/releases/packages/cr_c_cr6/sas/dbms/*/*; outcome: MODIFY example 2 ...
2
votes
1answer
51 views

Matching two different lines that are at least N lines distant from each other

I would like to match files that contain try (in one line) and consecutive catch (in another line) at least N lines later (to find very long try-catches and try to get rid of them). I mean, the first ...
24
votes
3answers
543 views

Multicolored Grep

I'm trying to get each grep command to highlight it's results in a different color. I can do it manually with a line like this: ls -l GREP_COLORS='mt=01;32' grep c | GREP_COLORS='mt=01;31' grep o | ...
1
vote
1answer
78 views

Help understanding an egrep expression

I stumbled across the following egrep expression: egrep '^([^aieou]*[aieou]){5,7}[^aieou]*$' /usr/share/dict/words on this page: http://matt.might.net/articles/sculpting-text The expression is ...
0
votes
1answer
95 views

What does Grep */* mean in KSH

In ksh, what does grep */*[myPattern] mean?
2
votes
2answers
2k views

grep: regex only for match anything between parenthesis

How do I match word only between parenthesis Input : this is (test.com) Desire Output : test.com
1
vote
1answer
245 views

Grep pattern in ksh [duplicate]

a small question regarding grep. I'm doing : find . -type d And I have three directories : ./backup-2013-11-27 ./backup-2013-11-26 ./backup-2013-11-28 If I only want to retrieve the one from ...
3
votes
2answers
539 views

“grep: Unmatched [” error when using regex

I'm trying to find a pattern similar to this: tail -n 100000 gateway.log | grep -B10 -A10 'Nov 22 11:13:56 Received Packet from [10.50.98.68' Where "11:13:56" could be any time. This is what I ...
3
votes
2answers
41 views

Extracting just a single group from a regular expression

I am trying to get a list of numbers from some hacked-together log files using zgrep. So, I am using: zgrep -ie "total time: .* ms" * This returns the whole line that I care about. I'd just like ...
6
votes
2answers
203 views

Why two almost idetical grep commands return different output: w/o and with filename

I have 2 almost identical greps: [Alex@localhost tmp]$ grep /bin/bash /etc/passwd root:x:0:0:root:/root:/bin/bash AlexL:x:500:500::/home/AlexL:/bin/bash user1:x:501:501:user1 ...
8
votes
6answers
2k views

How to pipe the result of a grep search into a new vi file

I'm using grep -e Peugeot -e PeuGeot carlist.txt to search through carlist.txt and pull out some items and I presumed that grep -e Peugeot -e PeuGeot carlist.txt | vi would pipe it through for me but ...
0
votes
2answers
147 views

Count occurrences of line in multiple files

I have log files that I need to grep and count the number of occurrences of a specific line. Issue is that I must start at a specific line of the first file and only count occurrences after that ...
2
votes
1answer
802 views

Search for a String inside all files inside a war

Where I work someone had Java project and used Eclipse to export that project into a .war package. Now I need to search for a string inside all the files that make that .war package. I know that a ...
5
votes
3answers
138 views

Robustly segmenting a csv file containing noisy data

Say I have a csv file with these columns: timestamp,cpu,memory An example of the shape of the actual data would be this chart: Having the data visualized in this way, it's easy for the human eye ...
5
votes
5answers
444 views

Pattern matching across multiple lines

What is a quick and easy way to validate if the pattern "301 domaname.com 200" is present within BASH when spread across multiple lines of output? I was using something like the following: awk ...
1
vote
2answers
54 views

Replace “-from” with “this” in the begining of line

I want to replace "-from" with "this" in the begining of line. This should happen when that line has 'R' at the end and line above it has "D" at the end. For example for the block shown below: ...
1
vote
1answer
162 views

Grep Wvdial output while it is running

I want to grep the output from wvdial while wvdial is running. Actually I want to fire a task when the modem is connected. I want to do this in the following way: grep string "IP address", if it is ...
1
vote
3answers
1k views

Count tabs per line in text file with utils

I have a tab-tabulated file. I would like to check if every line has the same number of tabs. For first step, I'd like to print the number of tabs for each individual line. I've tried grep -o '\t' ...
1
vote
2answers
149 views

Count entries for each directory in tree

Say I have the following tree: root: d foo d bar /foo: d A - tmp.txt /bar: d B - tmp2.txt /foo/A: - tmp3.txt /bar/B: d = directory and - = file I need to answer the following question: which ...
1
vote
1answer
179 views

grep block of lines

I want to grep every block of lines that has starting line with "CK \" at the end of line and ending line that has "D \" at the end of line, I can use sed/awk/grep etc. So far i have tried following ...
5
votes
1answer
282 views

Find all lines between a line matching a pattern and a line matching another pattern

i want to start grepping from the lines that has "CK" at the end of line and stop grepping when the line has "D" at the end. I tried grep "$CK" "$D" file..txt , but it didnt work. Input: kkkkkkkkkkk ...
4
votes
4answers
321 views

How can I grep for a string containing regex metacharacters like $ and '?

I want to search all files in the current directory and below for the substring $form['#node']. So, I have tried, initially: grep -R "\$form\[\\\\'" . and grep -R "\$form\[\'" . But it returns no ...