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

learn more… | top users | synonyms

1
vote
1answer
18 views

getting the root device?

I am trying to grep lsblk: lsblk | grep '/' The only issue is it returns me this: └─sdb1 8:17 1 7.3G 0 part / └─sdc1 8:33 1 7.6G 0 part /media Which is great except I only ...
3
votes
4answers
94 views

Find lines in (multiple) files that contain the given line in an input file

If I have a number of files like the following: file1: 123 456 789 012 file2: line1 922 line2 392 line3 456 line5 291 line6 201 ... file3: line1 111 line2 123 line3 19 line5 542 line6 ...
1
vote
3answers
174 views

using grep and wc commands to solve a problem

I am trying to figure out how to count the number of lines in file.txt that have at least ONE number or digit. So far I figure will count the amount of lines: wc -l filename.txt I then figure this ...
0
votes
1answer
349 views

How to use grep when file does not contain the string

In my bash script I'm trying to print a line if a certain string does not exist in a file. if grep -q "$user2" /etc/passwd; then echo "User does exist!!" This is how I wrote it if I wanted the ...
-1
votes
4answers
187 views

How to grep AND pattern with pipe

I wrote a piece of script similar to something below: srvc_location=`ps -ef| grep '${sandbox}.*pset' | head 1 | awk {print $9}` echo "SERVICE LOC : "$srvc_location But strangely this $srvc_location ...
1
vote
2answers
31 views

How can I open the file with the most matches for a given regex?

Let's say I have a directory ~/mydir that has a whole bunch of text files in it. I want to search for searchterm in this directory and then view the file that has the most matches. How can I do this ...
1
vote
1answer
93 views

POSIX-compliant recursive grep with no errors for inaccessible directories

What script would allow me to grep a keyword and print the filename containing the keyword inside the file content, for example 'Carhart' inside all .sas files in all subdirectories? I tried something ...
0
votes
2answers
56 views

find /mydir/tmp/*20151014* -print | xargs grep -l 'filesTransmitted=1' throws error

The command throws error: $ find /mydir/tmp/*20151014* -print | xargs grep -l 'filesTransmitted=1' bash: /usr/bin/find: The parameter or environment lists are too long. Is there any optimal command ...
2
votes
3answers
80 views

Bash script to echo the first positional of each line into file with name of the second

I have a text file that is organized as follows: Group 1 asdsdsdsdf.html jeff xcvxcvxcvx.html bob vrgeiuvhif.html sue Group 2 iwdowijdoi.html mary pokpompojm.html doug ndkjfsjfbs.html lisa I ...
4
votes
3answers
187 views

Remove long string from files

Files on my server have been corrupted. I want to remove a 13000 character string from all PHP files containing it. The string looks like: ?php if(!isset($GLOBALS["\x61\156\x75\156\x61"])) { $ua=...
1
vote
0answers
233 views

Grep can't find end of line in DOS format text file?

In Ubuntu I'm working on a C file that was originally created under Windows and then committed to source control. Vim shows the file format is "dos", which I gather just means it has CR/LF line ...
5
votes
3answers
8k views

Looking for a Java class in a set of JARs with find, unzip, grep

I was trying to find the JAR containing a Java Class. JARs are in zip format. My first attempt was: $ find -name "*3.0.6.RELEASE.jar" | xargs -l1 unzip -l \ | grep stereotype.Controller ...
0
votes
2answers
35 views

filter data by column's value

I want to search only lines where a column start with 03: (hours), but while using below command, showing all the rows where value contain 03. Its an example the data is pretty much huge in a file. ...
0
votes
2answers
42 views

Find the position of repeated character “&”

How can determine the position of “&” in the column number 5 to substring the value 2 or substring value 3 by awk functions column1|column2|column3|column4|variable1:value1 & variable2:...
8
votes
5answers
19k views

How to display line number while doing grep on a file

How to display line number while doing grep on a file. For example: grep CONFIG_PM_ADVANCED_DEBUG /boot/config-`uname -r
0
votes
1answer
37 views

Egrep expression as a grep expression

I have this egrep command which's expression searchs for filenames with length 3 (excluding .extension): ls | grep -E '^.{3}\.+' However, I'm unable to write this with grep only, is it possible?
1
vote
4answers
226 views

Trying to grep url from html source in .txt file using sed

i have been able to use the code below to grep lists of urls from html source before, but for some reason it's not working for this specific example. grep: grep -1 box-download shareit1.txt|sed 's/&...
0
votes
2answers
408 views

How to match stream against white-list and black-list?

How can I duplicate a pipe stream (output from another program) so that for every line, I can check whether it matches independent regular expressions (ideally, reuse a grep command with options like -...
0
votes
3answers
110 views

Avoid pgrep in sh

#!/bin/sh ALTER="$1" NAME="$2" for pr in $(pgrep $NAME); do elapse=$(ps -o etime= -p $pr) [ "${elapse%-*}" -gt "$ALTER" ] && echo $pr done I am running this on AIX which doesn't ...
-1
votes
1answer
50 views

Avoid pgrep in csh

I got that code foreach pr (`pgrep "$NAME"`) And I try to avoid pgrep: foreach pr (`grep "$NAME" | awk '{$2}'`) but that does not really work. (later with that pr should be the PID printed out ...
0
votes
1answer
56 views

find by name or by grep

find . -perm -u+x,g+x,o+x -type f -exec grep -i -l -H -E '#!/bin/bash|#!/bin/sh' {} \; 2>/dev/null 1>/$user_scripts I am tasked with finding any script that could exist for N users in 4 ...
0
votes
2answers
59 views

how do i write a regular expression using grep to find lines that start with 'O' or 'o' AND contain 2 consecutive chracters

how do i write a regular expression using grep to find lines that start with 'O' or 'o' AND contain 2 consecutive 'c' characters. grep '^[Oo]c{2}' filename
2
votes
2answers
103 views

Remove specific (known) extension from filename if it's there

I'm not even sure whether grep is meant to be used for regex in one-line data. Googling led me to this lookahead expression that, along with grep, matches any .ext file and returns bare name: XX@XX:/$...
3
votes
6answers
686 views

How to grep a line with unknown number of its indented lines

I often need to grep a line with its indented sections. For example, if the indented line number may be known, we would achieve this goal with the following: $ lspci -vq | grep -i wireless -B 1 -A 5 ...
2
votes
2answers
88 views

how to grep specific string in random column

I'm looking to find out any farmer who produces bananas and his address from a database. My data looks something like this: - farmer1 address1 apple1,banana-green,orange-5 - farmer2 address2 ...
3
votes
3answers
234 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 ...
1
vote
2answers
73 views

Using a shell variable with grep [closed]

I have a list of tokens in a text file and want to use grep to get the lines from a second text file that contain those tokens, but seem to be having trouble accessing the shell variable with grep: ...
13
votes
5answers
27k views

How can I find all files that do NOT contain a text string?

What concise command can I use to find all files that do NOT contain a text string? I tried this (using -v to invert grep's parameters) with no luck: find . -exec grep -v -l shared.php {} \; ...
-1
votes
1answer
263 views

Grep inside while loop removes the file!! [Bash script]

I'm working writing a bash script and I am becoming crazy with a mysterious loop... so I'm asking to you expecting someone could give some help. I need to seek for a concrete code inside a text file (...
4
votes
3answers
159 views

egrep regular expression for times over five minutes

I have the following time formats in a text file `1` equals one second. `5|01` equals five minutes and one seconds. `13|01` equals thirteen minutes and one seconds. `21|12|01` equals 21 ...
0
votes
2answers
35 views

Search string using grep

In my file_*.txt there are two type of words one containing LC1 only and another containing LC1_R. I would like to grep lines that contain only LC1 not LC1_R. How can I do it? I tried: grep "LC1" ...
2
votes
4answers
148 views

Grepping pieces of a string to form a new string

I am parsing some log files and have grepped out the errors. Each line looks something like this: CreateOrder_hostname1.domain.com_201509291530_tee.log:2015-09-29 15:42:06,715:ERROR :Thread-...
4
votes
3answers
254 views

grep regular expression solution (greedy not working)

I have the following text in the data.txt file :MENU1 0. public 1. admin 2. webmail :SYNTAX ! opt1, ... : :ERROR1 Error #1, blah... blah.. blah... Please do ... :ERROR2 Error #2 ... and I want ...
0
votes
2answers
103 views

Grep string then delete word and do next statement

I want to create bash shell script which can - find string in file - then delete word - then restart service I have tried this code Restart.sh #!/bin/sh grep -q 'RestartServiceApache' /srv/...
0
votes
3answers
942 views

How to grep for two words with a tab in between them

I've got a file with with a list of words each delimited by tabs. I'm trying to use grep to search for two of the words, but I can't figure out how to include the tab in the search string. I've tried: ...
0
votes
1answer
86 views

Match everything after backslash and before space

I have strings similar to the following: *unknown*\*unknown* (8) hello\morning (3) I'm trying to match just morning or *unknown\*. So far I have tried: [^\\]+$ But that matches from backslash ...
1
vote
2answers
679 views

Count how many lines start with which characters

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
1answer
122 views

Regular expression: when every alternate character is a digit

I am trying to write a Linux command using egrep that validates lines whose even character is a number. I am doing it this way: egrep "^(.[0-9])*$" text.txt However, this fails for i) empty ...
0
votes
1answer
345 views

How do I find block of text, from a text file, containing a specific string [duplicate]

I've been looking to extract a block of text, from a text file, containing a string with no success. Any help would be really appreciated. For example: I have a file named xyz with below contents: ...
0
votes
2answers
37 views

Looking for files ONLY in subdirectory

So let's say I want to search for files that are C files. How do I go about searching for them ONLY in my sub-directories? I tried: ls -R | egrep .c$, but that also searches for .c files in my ...
5
votes
3answers
248 views

Search for a precise number with grep

I am trying to look for lines in a file that have one or more instances of 1234, but no other numbers (non-digit characters are allowed). Any other number should cause the line not to match. Valid ...
0
votes
3answers
71 views

Words that end with s BUT exclude words that end with 's

I need to find a list of 5 letter words beginning with d(upper or lower case) , followed by a lowercase vowel including y and end with s. My command: grep -i '^d[aeiouy]....s$' file name. I want to ...
1
vote
3answers
101 views

How to grep for a word taking into account hyphenation?

For the purpose of building an index I am searching for some words in a bunch of latex files. This process is complicated by the fact that latex has a discretionary hyphen command \-, which indicates ...
3
votes
1answer
183 views

How to use grep with files in arbitrary encoding, without locale installed and without sudo?

I have utf-8 locale on my computer, and i want to search in KOI8-R encoded files. But i don't have KOI8-R locale installed. And i don't have sudo rights to install this locale. Is it possible to ...
0
votes
2answers
103 views

How to find lines in file containing set of special characters?

I am writing a script that runs through an existing c program and finds whether it contains comments: They should start and end in comment tags: /* and */ How can I achieve this? Example: This ...
1
vote
3answers
1k views

Compare first column in different files

I want to compare one file with other files in the same directory. file1.txt contains: move 34#123# get 11#278 put 33#29#567#23 file1.txt should compare with files 1.txt and 2.txt. 1.txt ...
1
vote
3answers
142 views

bash: grepping contents of column which is dynamic

I have data as following : What i need to get is the OS_NATIVE_NAME of a particular DEVICE. I know one way is to print its column value using: awk '{print $*col_num*}' But the column number ...
0
votes
2answers
67 views

Grep for string but ignore commented out items with string

I'm looking for the words gmail or outlook in a directory. Although there are some lines commented out with the word gmail and outlook in it. How can I grep for this? I tried a bunch of things but I ...
21
votes
11answers
33k views

How to print all lines after a match up to the end of the file?

Input file1 is: dog 123 4335 cat 13123 23424 deer 2131 213132 bear 2313 21313 I give the match the pattern from in other file ( like dog 123 4335 from file2). I match the pattern of the line ...
45
votes
7answers
33k views

How to grep standard error stream (stderr)?

I am using ffmpeg to get the meta info of an audio clip. But I am unable to grep it. $ ffmpeg -i 01-Daemon.mp3 |grep -i Duration FFmpeg version SVN-r15261, Copyright (c) 2000-2008 Fabrice ...