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

learn more… | top users | synonyms

3
votes
1answer
620 views

grep on line before the match plus the match

zzzzzzzzz aaaaaaaaa bbbbbbbbb & ccccccccc & ddddddddd hhhhhhhhh eeeeeeeee fffffffff & ggggggggg & in the above line, what i want is to grep/sed/awk (any method is fine) line that ...
2
votes
1answer
119 views

Grep ^ and $ at the same time

I'm, trying to use grep with a ^ and $ at the same time but for some reason that does not work. If I type grep '^s.*e' german.dic I get a lots of words starting with s and having an e somewhere. A ...
1
vote
2answers
105 views

Meaning of the following linux commands

cat "$1" > /tmp/file.$$ 2>/tmp/file0.$$ grep "*" /tmp/file.$$ >/tmp/file0.$$ I understood that in the cat command, if an error occurs, the error will be shown in file0.$$ and if it ...
22
votes
5answers
2k views

Pipe to multiple files in the shell

I have an application which will produce a large amount of data which I do not wish to store onto the disk. The application mostly outputs data which I do not wish to use, but a set of useful ...
1
vote
1answer
853 views

How to grep top most frequent error messages in a unix logfile

If I have a file example apache log file How to extract the top most frequent error messages in a unix log file with no timestamps the key is most frequent error message should come on top of the ...
0
votes
1answer
1k views

Getting a date format with grep

I've looked at similar questions and tried most suggestions, but none of them got me anywhere, even though i have a fairly simple task I think :S. I'm fairly new to this whole thing, so I've run out ...
0
votes
0answers
28 views

How to use grep command to find the matching pattern and some more characters? [duplicate]

I have a compressed JS code and I want to find a pattern, but since the file is compresses, it has only a line, and the match pattern is all the file. The output is too much large. grep -r ...
8
votes
6answers
556 views

How to get multiple lines out of a file by a regex?

How to get multiple lines out of a file by a regex? I often would like to get multiple lines / modify multiple lines by a regex. A example case: I am trying to read part of an XML/SGML file (they ...
4
votes
5answers
420 views

Multi-line grep beween two tags, only show the last match

I'm having a program that intermingles data segments into log output: log message log message ----BEGIN INLINE DATA---- data data data -----END INLINE DATA----- log messge I'm looking for something ...
5
votes
3answers
390 views

With grep, how can I match a pattern and invert match another pattern?

With grep, I want to select all lines that match a pattern, and that don't match another pattern. I want to be able to use a single invocation of grep so that I can use the --after-context option (or ...
1
vote
2answers
232 views

mv complains about standard input in a pipeline that filters file name with grep

I'm trying to move several .avi files that have spaces in from a CD to my home folder. I've already know how to get rid of the spaces: find /media/mathscd/ | grep -L -r "*.avi" | for file in *; do ...
4
votes
2answers
2k views

recursive search for a pattern, then for each match print out the specific SEQUENCE: line number, file name, and no file contents

What I am after is almost exactly the same as can be found here, but I want the format "line number, separator, filename, newline" in the results, thus displaying the line number at the beginning of ...
1
vote
2answers
508 views

Show only part of the line found by grep (but not the part that matches the pattern)

In ifcfg-eth0, I managed to grep "DEVICE=eth0" by using grep "DEVICE=" ifcfg-eth0 But how can I grep pattern to show only "eth0"?
1
vote
2answers
146 views

Using grep to search a file

Re : grep command. I have to search a file for the names of all the students in the school's Linux system. I am able to do this and return about 665 names. (First string of /etc/passwd file). ...
3
votes
1answer
524 views

How to trick an init script into returning 0

I have a init script which is poorly designed because it does not conform to the Linux Standard Base Specifications The following should have an exit code of 0 if running, and 3 if not running ...
2
votes
1answer
659 views

Search and Filter Text on large log files

I use the tail, head and grep commands to search log files. Most of the time the combination of these 3 commands, in addition to using pipe, gets the job done. However, I have this one log that many ...
3
votes
2answers
227 views

grep entire filesystem blocks on devices

Command grep -rHinF --colour=always -e "aa:aa:aa:aa" / Output grep: /dev/geom.ctl: Operation not supported by device grep: /dev/devctl: Device busy It seems that grep can not proceed because it ...
1
vote
1answer
217 views

How to use awk or sed to convert csv diffs into more readable format

Can anyone give me the example of how to use awk or sed (not sure which one, as I haven't use either of these much, as I have mostly been using grep and cut to work with csv data) to convert ...
2
votes
3answers
961 views

Printing match groups with grep

I have the following grep: grep -Po 'href="(http://.*)">Transcript.* - ([0-9]*)\. (.*)</a>' I would like to print out the match groups. Is there a way to do this without Perl/Python? I ...
4
votes
3answers
460 views

Using a perl compatible regex with GNU grep -P

I'm using this regex (?<=\[')[^,]* on a file containing the following line disk = ['OVS/sdasd/asdasd/asdasd/something.img, w'] I want that to return OVS/sdasd/asdasd/asdasd/something.img How do I ...
12
votes
8answers
1k views

How to perform a multi line grep

How would you perform a grep for text that appears on two lines? For example: pbsnodes is a command I use that returns the utilization of a linux cluster root$ pbsnodes node1 state = free ...
1
vote
1answer
139 views

Retrieving two matches from repeated pattern

How can I retrieve matches from two repeated patterns: mappedname & its value in the text below?. MapName:abc someDiffName=someDiffValue x=3 y=6 NeedyMappedName=NeedyMappedValue . . Flag=true ...
6
votes
5answers
236 views

Get filesystem name and usage with `df -h`

df -h | grep test | sed -e 's/*%.^ //g;s/.*[ ^I]//' Output: /test /test/drv0 /test/drv1 df -h | grep test | cut -d '%' -f1 | sed -e 's/*%.^ //g;s/.*[ ^I]//' Output: 10 20 15 I want to ...
3
votes
1answer
2k views

Get all regex matches between two patterns and print them to file

I've got a file with a bunch of long lines. I'd like to grab every group between two patterns and print them to a new file, one match per line. I could manage to do this with Python, but I'd prefer ...
6
votes
3answers
108 views

generate links of pages with incrementing variable

consider the links http://unix.stackexchange.com/questions/tagged/linux?page=2&sort=newest&pagesize=15 ...
0
votes
2answers
224 views

grep + operator

According to the grep manual: * The preceding item will be matched zero or more times. + The preceding item will be matched one or more times. Let's test it echo 'agb' | grep 'a.*b' # returns ...
2
votes
4answers
172 views

AWK Search massive file and write to variable name

I want to pull data that I will need to reformat from a simulation file I am working with to prepare it as input for running other simulations. The target data is an optimized chemical z-matrix. The ...
2
votes
4answers
449 views

Zgrep stop after first match when arguments are passed from xargs

I'm using this command to find patterns in zip files (similar to the one) suggested here http://superuser.com/questions/144926/unix-grep-for-a-string-within-all-gzip-files-in-all-subdirectories find ...
2
votes
0answers
295 views

exit code of grep in trap where the script is launched by upstart service

recently I encountered a problem in ubuntu upstart and trap in bash shell. I use ubuntu 12.04 the upstart script(/etc/init/player.conf) is: start on (local-filesystems and runlevel [2345]) stop on ...
10
votes
2answers
928 views

Script to quickly test all keyboard keys

I need to check some notebooks for bad keyboard keys, and so I'd like to speed that up as much as possible. I found nothing for this specific task, so my idea is a script that reads the pressed keys ...
2
votes
2answers
136 views

regular expressions with grep

I have this command right here: grep -B10 -A10 '14:14:50 {"channels":["/alerts/6979/new"],"data":"New alerts for unit 6979"}' mygateway.log Rather than searching for 14:14:50 I would like the 50 to ...
3
votes
5answers
444 views

How to grep, then search and replace on the output?

I have some output from iconv, e.g. $ iconv -l | grep ISO | head -5 CSISO4UNITEDKINGDOM// CSISO10SWEDISH// CSISO11SWEDISHFORNAMES// CSISO14JISC6220RO// CSISO15ITALIAN// durrantm:~ How can I change ...
2
votes
1answer
94 views

How to grep including single quote in a file

I have this line from file.php: 'user' => '', What would be the appropriate command using grep to query the exact line?
3
votes
5answers
755 views

How can I concatenate a single column of output into a list?

I'm using: ls /proc | grep -v "^[0-9].*" to get a list of all the files and directories in /proc that don't begin with numbers. The output is a single column list, i.e.: acpi asound buddyinfo bus ...
20
votes
3answers
3k views

grep: memory exhausted

I was doing a very simple search: grep -R Milledgeville ~/Documents And after some time this error appeared: grep: memory exhausted How can I avoid this? I have 10GB of RAM on my system and few ...
1
vote
3answers
1k views

grep for an alphanumeric strings of any length with a colon on each side

How would you grep for an alphanumeric strings of 1 to 50 characters (ideally, any length would work too) with a colon on each side – a typical result would be all the lines containing the string ...
2
votes
2answers
102 views

selecting specific string in a field

I have this file: N104 F1810 N104 F3810 N104 F810 N104 F305 and a script to grep an input. Script: enter input read input cat file | grep $input when I input “810”, I get the ...
0
votes
2answers
118 views

Count lines with alphabet in linux

I have a file with a number of lines in a file filename. I want to count how many lines start with character 'a', with 'b' and so on in one go. What command i should execute.?
3
votes
2answers
403 views

grep filter all lines plus one before each hit

I want to parse some output so I exclude all lines that contain either foo or bar, as well as all lines immediately preceding those lines. For example: echo " 1 some line 2 line to exclude because ...
10
votes
5answers
2k views

Using grep or awk

To capture a particular pattern, awk and grep can be used. Why should we use one over the other. How does this work in the background? Which is faster and why? E.g If I had a log file and I wanted ...
2
votes
3answers
398 views

Command substitution in for loop not working

I want to keep all files not ending with .bat I tried for f in $(ls | egrep -v .bat); do echo $f; done and for f in $(eval ls | egrep -v .bat); do echo $f; done But both approaches yield the ...
2
votes
2answers
103 views

Filtering the result of the find command, so that it returns only directories

Is it possible to get only the results from find that are directory paths? Using find with some option, or using grep or some other utility the results are piped into as a filter? I thought something ...
2
votes
4answers
849 views

How to use the grep result in command line?

When I use grep to find some text which I need, it will display lines containing a match to the given pattern. For example, # grep -r ".*Linux" * path0/output.txt:I hope you enjoyed working on ...
0
votes
1answer
84 views

Problem with small bash script

I often run find in my code projects when I make a change somewhere and I have to find where it impacts other pieces of code, so I thought about writing a very small script to make this easier, call ...
1
vote
2answers
269 views

How can I combine grep with file and extra regex

I have a file of strings in each line. For example: AAAAA BBBBB CCCCC etc I want to check each string one by one whether it exists in a series of files in a specific directory. I ...
4
votes
4answers
2k views

How to grep for pipe |

How can I grep for line containing pipe character | or for character >: files content: |this is test where is > this is none now what I need using grep command is grep -iE "<some ...
3
votes
2answers
1k views

Optimizing a shell script with long running while loop

I have written a shell script which has to do the following: Capture session commands into one file. Every individual command into separate file. Mail every individual command file content based on ...
3
votes
1answer
388 views

Speed up zgrep on a multi-core computer

I'm running zgrep on a computer with 16 CPUs, but it only takes one CPU to run the task. Can I speed it up, perhaps utilize all 16 cores? P.S The IO is just fine, I could just copy the gzipped file ...
1
vote
3answers
479 views

Escaping * with Regular Expressions and Grep

I have a file that has unique lines that start with 2 stars (**). However when I run a grep command for grep \*\* fileName I get all of the lines in the file. This is very unusual, and what I ...
2
votes
2answers
423 views

Faster way to find matching patterns from external file grep -f {patternfile} {source} in BASH

In *nix terminal (Bash) os there any other faster way to find a pattern (or excluding them - egrep -v option) than using this: grep -f {patternfile} {source} Using this option is painfully slow for ...