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

learn more… | top users | synonyms

-1
votes
1answer
38 views

Why does grep goes into blocking stage? [closed]

I am making a test My task will be one child pipelining the output to the input of another another child. I am using ps -ef as the first command(by using execlp()) and grep root as the second ...
4
votes
2answers
66 views

Find if any line of a file is a subset of data in another file

I have a file file1.txt with the following data: MIME_HTML_ONLY SUSPICIOUS_RECIPS SORTED_RECIPS HFILTER_HELO_5 and another file file2.txt with lines as: HFILTER_FROM_BOUNCE FORGED_OUTLOOK_HTML ...
12
votes
3answers
4k 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 ...
2
votes
2answers
71 views

search for exact string in unix

I have a file, xyz.txt, which has one string per line. How do I search xyz.txt for an exact match? I get an input string, eg. defabc, from another app. If defabc is found, it should return true, else ...
1
vote
1answer
30 views

Get the filename that the record come from

I have a huge number of .gz files, which being uncompressed using zcat together. I am looking to get from where each record came from, as below example: File1: 1,2,3,4,5,6,7,NEW,9,10,11,12 ...
0
votes
1answer
45 views

AWK summary file from a filtered list

I have 2 files, the first being a file of records that tell me which rows from the second file I am interested in (I'll call this the FILTER_FILE). This file can contain duplicate lines and I only ...
4
votes
3answers
280 views

Grep all at once

The script I have receives arguments and needs to grep through another file. Script loops through every argument. Is there a way to collect the arguments in a single search string and grep with OR ? ...
3
votes
1answer
58 views

Grep on single line [duplicate]

I have a script that streams a pattern in a single line (no linebreaks). I want to grep stock_ticker in that line and output it as soon as I have found one. Now the script is never ending and is ...
0
votes
2answers
30 views

How could I run -exec on find command after | grep

I am on a remote machine attempting to move all loader.gif files into a /Pictures folder find . -iname *.gif | grep loader shows four file I would like to move, naturally I tried: find . -iname *.gif ...
1
vote
2answers
42 views

Extract columns of a string using grep

I need to extract from /proc/net/dev the second and tenth columns separated by spaces. In other words, the number of received and transmitted bytes. Is it possible to do this using grep and ...
4
votes
5answers
1k views

How to remove all comments from a file?

I have a file with a few lines commented out: foo bar stuff #Do not show this... morestuff evenmorestuff#Or this I just want to print all the uncommented code: foo bar stuff morestuff ...
8
votes
5answers
7k views

How to grep for text in a file and display the paragraph that has the text?

Below is the text in the file: Pseudo name=Apple Code=42B state=fault Pseudo name=Prance Code=43B state=good I need to grep for "42B" and get the output from the above text like: Pseudo ...
1
vote
3answers
25 views

Extract specific value from block of data

I have the following block of data and need to extract the percent value in XYZ FREEBYTES, i.e. captured value should be equal to 64. As far as I know, I can grep and cut the data. However, the digits ...
1
vote
1answer
105 views

grep not matching in nc output

I'm using nc to scan for open ports but I'm scanning a wide range and it's displaying too many results. Trying to grep it for the word "succeeded" doesn't work for some reason: $ nc -zv localhost ...
1
vote
1answer
43 views

grep also returning non matching lines

I'm trying to grep the output of strings and only find lines that start with a certain number of = chars. I tried strings data.txt | grep ^=* Sample output: 1~{, WX2 };K,< ========== ...
2
votes
3answers
136 views

How to print the before lines of search keyword with out using grep in a gz file?

How to print the lines before a search keyword without using grep in a .gz file? ID:342N000390AAAAAAAA 07/14/15 10:26 (MV90 ) * Register Data Imported * Warning - No ...
1
vote
2answers
49 views

Grep and ignoring leading whitespace

I've been working on a bash script to search for names of defines, and then grab the hex values for them and put them in the list. Once I have the list of names I'll attempt to search for "#define ...
0
votes
1answer
67 views

Grep /var/log/maillog for email to a certain user, based only on his linux username

Level 1, beginner here. Question about grep and maillog. I have a learning environment, based on Linux CentOS, with Postfix and SquirrelMail running, but my assignment is more in general. I need to ...
3
votes
3answers
59 views

Regex search for word roots with common prefixes

I recently had a curiosity about words in the dictionary that share both "pro-" and "con-" as a prefix. So, for example, procession/concession, produce/conduce, profess/confess, progress/congress, and ...
8
votes
5answers
619 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 ...
2
votes
1answer
125 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 ...
4
votes
6answers
374 views

Grep the text which matches with a pattern

I was looking for a command which will print the selected portion of a line which matches the conditions. Eg, my text line is like this: p1=X||p2=Y||p3=X||p4=X||p5=X||p6=Y||p7=X I want to print ...
1
vote
8answers
85 views

How can I stop grep returning itself [duplicate]

# ps -ef | grep gre root 15067 63614 0 11:03 pts/0 00:00:00 grep --color=auto gre Another example # ps -ef | grep sshd root 1186 1 0 Jul26 ? 00:00:00 /usr/sbin/sshd -D root ...
-1
votes
2answers
48 views

Find file, Edit text within files

Im trying to find a file then edit the text within these files. This is what I have so far, but sadly Im not that well versed... Help Please? grep -rl "wrongtext" ~/Library/Calendars ...
9
votes
3answers
397 views

Equivalent command to grep binary files

I have a bunch of binaries and I know that inside these binaries there are strings I want to find. I want to do a: grep -lir "the string I am looking for" and get a list of all binaries inside a ...
5
votes
2answers
1k views

git grep colors differ from grep custom colors

I've setup grep colors in my ~/.bashrc : export GREP_COLORS='ms=01;34:mc=01;34:sl=:cx=:fn=35:ln=32:bn=32:se=36' They work for grep --color=auto grep --color=always Unfortunately, those custom ...
1
vote
4answers
189 views

Grep for a string in file without using pipe

I want to grep for a word in a file in the last n lines without using the pipe. grep <string> filename enables to search the filename for a string. But, I want to search for a string in the ...
3
votes
5answers
5k 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 ...
2
votes
5answers
430 views

How to grep all xml files that do not begin with “<”

How can I find all xml files in the current directory and all sub directories, that do not start with < in the first line. I have tried this, but the grep doesn't work: find . -type f -name ...
18
votes
2answers
60k 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 ...
2
votes
1answer
123 views

fgrep or grep -F, which one is better for portable script? [closed]

I am having this controversial commentary on this answer to a quesion whether we should use fgrep or grep with the -F switch (i.e grep -F) for portability. The points that have been came into light so ...
12
votes
5answers
662 views

How can I count the number of lines in a file after a grep match?

I am trying to count the number of lines after a problematic row in a csv file. I am aware I can use the grep -a # syntax to output # number of lines after a match has been found. I'm only interested ...
11
votes
7answers
23k views

How to read first and last line from cat output?

I have text file. Task - get first and last line from file after $ cat file | grep -E "1|2|3|4" | commandtoprint $ cat file 1 2 3 4 5 Need this without cat output (only 1 and 5). ~$ cat file | ...
1
vote
2answers
53 views

Remove x lines before and y lines after nth line (which matches a pattern) in a file

I'm trying to remove a four line block from a text file if the second line matches the Regex N+. The text file is composed of many repeating 4 line formats, ie: @HEADER1 ACTGCNNNT + 583@#!NMY ...
3
votes
2answers
124 views

comparing the first column of two files and printing the entire row of the second file if the first columns match

I have two files in these formats: file1 : air smell hand dude road cat file 2: air,4,21,01,13,3,2 smell,21,4,2,5,6 dude,1,31,42,1 road,1,4,2,1,4 cat,1,5,6,3,1 hand,1,4,2,1,6 mouse,1,3,5,6,2 ...
2
votes
1answer
1k views

How to print strings matching a pattern with grep?

I want to get a list of strings matching the following pattern: CLASS_NAME:"*" where * represents any number of characters. I tried: grep -o CLASS_NAME:\".*\" script.js and grep -o ...
1
vote
4answers
346 views

How can I find the biggest “error_log” files listed by location and size?

I host about 200 websites and I have noticed that some got huge error_log files. I would like to see which ones are consuming the most space so I can take a look on them and solve the error so they ...
0
votes
1answer
40 views

Grep unix for a data to find out result

i have a file, contains huge data. for example there is written "COST-> 1.00" line. and i would like to grep for example between 1.00 and 100. it includes both integer and string characters. how ...
0
votes
2answers
28 views

Replace fields in file inline

I have file (a.txt) with following contents: Redhat master 12345678 V1A 20.0/20.0.1 Windows slave 12345678 V1B 19.0/19.0.1 I want to grep line with word master and replace 4th and fifth field with ...
78
votes
5answers
192k views

How do I grep for multiple patterns?

I want to find all lines in several files that match one of two patterns. I tried to find the patterns I'm looking for by typing grep (foo|bar) *.txt but the shell interprets the | as a pipe and ...
3
votes
4answers
2k views

How can I open a file that results from grep?

I often grep a bunch of files to find a line, and then grep returns one result. Rather than copying and pasting the filename into a new command, I'd like to be able to open that one result with an ...
0
votes
2answers
39 views

grep for lines/files that contain exactly X chars

Here's the output of a grep command I ran: [user@localhost] : ~/Documents/challenge $ grep -i -e ".\{32\}" fileA fileB fileA:W0mMhUcRRnG8dcghE4qvk3JA9lGt8nDl fileB:observacion = new Observacion(); ...
3
votes
4answers
92 views

Print alternating lines with conditional

Still struggling with messy logfiles. What I want is to find lines that first matching string X and then string Y. I then want to print them together. The problem is that, sometimes there is X, but no ...
0
votes
2answers
62 views

How to count the number of words of each sentences in a file using shell command?

I have text files like this: Mr.P.K.Baneerjee has visited the site today at 4.30pm The permit tag has been set in the ds busbar Opearation has been performed in presence of control engineer Opeation ...
14
votes
5answers
9k 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 ...
1
vote
1answer
43 views

Recursively count occurrences of a word in identically named files in different directories

I've got several different directories that contain different versions of some files. I want to count a symbol in all versions of the same file. Here is an example of the hierarchy. directory ...
3
votes
1answer
141 views

Grep doesn't match carriage return characters

I'm trying to find lines with the carriage return character, but I'm not getting the results I'd expect. I've whittled it down to this proof-of-concept: $ uname -a CYGWIN_NT-6.1 Aodh ...
39
votes
4answers
112k views

How can i count the number of lines of file with common tools in a bash?

I am redirecting grepping-results to a file, and then use cat to show its content on the screen. I want to know how many lines of results I have in my results file and then += it to some counter. ...
3
votes
6answers
404 views

Grep a file on specific field

I have two files, let's say File1: Locus_1 Locus_2 Locus_3 File2: 3 3 Locus_1 Locus_40 etc_849 3 2 Locus_2 Locus_94 * 2 2 Locus_6 Locus_1 * 2 3 Locus_3,Locus_4 ...
0
votes
2answers
66 views

Display filenames with grep

I'm looking for a way to display the filename when using this command: cat *.tcp | grep "tcp" | grep "open" | sort | uniq Is there a way to do it?