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

learn more… | top users | synonyms

0
votes
2answers
25 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
32 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 ...
0
votes
0answers
16 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 ...
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
21 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
88 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
42 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
131 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
43 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
51 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 ...
2
votes
3answers
43 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 ...
3
votes
6answers
364 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 ...
2
votes
1answer
46 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
8answers
77 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
47 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
346 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 ...
4
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
157 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
406 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 ...
17
votes
2answers
56k 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
100 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
593 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 ...
10
votes
7answers
20k 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
47 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
73 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
312 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
26 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 ...
69
votes
5answers
179k 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
36 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(); ...
2
votes
4answers
79 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
1answer
2k views

How to execute grep in tcl for getting XYZ-123 type pattern

How can I execute this grep command in TCL? grep '[A-Z][A-Z][A-Z]\-[0-9][0-9][0-9]' file1 > file2
0
votes
2answers
54 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 ...
13
votes
5answers
8k 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
40 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
114 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 ...
37
votes
4answers
106k 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
348 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
51 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?
1
vote
1answer
55 views

tool in terminal like helm-occur/helm-swoop for terminal?

I don't know whether this is the right place to post this question, but if you are not an Emacs user, I'll explain the basic usage of helm-occur/helm-swoop. I like helm-occur/helm-swoop for Emacs ...
1
vote
2answers
115 views

Grep multiple patterns and print a different number of lines below each of the patterns?

I am trying to find a way to grep/awk multiple patterns and then print a certain number of lines below the first pattern and a certain number of lines below the second pattern. For example .... ...
4
votes
3answers
3k views

GREP / SED or AWK: Print entire paragraph in a file on pattern match [duplicate]

I have a file with hundreds of paragraphs of around 15 lines each. I need to search for a pattern, say Occurance: 1. If this pattern is found in the para, I need to print the entire paragraph. Note ...
0
votes
0answers
40 views

print duplicate fields in different columns from two files using awk

I have two large files. I am attempting to identify and print any duplicate fields to a new file. I have a 19 columns and over 100k rows. I need to compare columns 5-19 for duplicate values. ...
0
votes
3answers
54 views

parsing out duplicate barcodes in a separate line

I've got the following data I need to parse out the duplicates from column1 into a separate file, For example, 21288003132541:cr 21288003267289:fr 21288003758683:ph 21288003758683:tag ...