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

learn more… | top users | synonyms

4
votes
1answer
211 views

`set -e` and `grep` idiom for preventing premature exit from shell script when pattern not found

Help required - in the context of shell scripting on a GNU/LINUX bash: I always use set -e. Often, I would like to grep and do not always want the script to terminate execution if grep has an exit ...
1
vote
4answers
133 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 ...
0
votes
3answers
69 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
36 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
51 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 ...
1
vote
1answer
107 views

How can I write a bash script that sends a graphical notification to all users' X displays?

This is a request for improvement and update (NOT a duplicate!) on these 2 questions & answers: Can I launch a graphical program on another user's desktop as root? Show a notification across ...
0
votes
2answers
45 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
66 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: ...
1
vote
2answers
63 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: ...
-1
votes
1answer
148 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
105 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 ...
2
votes
2answers
68 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 ...
0
votes
2answers
30 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" ...
4
votes
3answers
184 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 ...
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 ...
0
votes
2answers
60 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' ...
0
votes
1answer
52 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
3answers
154 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: ...
-2
votes
1answer
102 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
164 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
34 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 ...
0
votes
3answers
40 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
61 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 ...
5
votes
3answers
223 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 ...
3
votes
1answer
65 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
68 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 ...
0
votes
1answer
18 views

Is there a way to get all the NEWS.gz which is/was this year in /usr/share/doc/ of $PACKAGENAME.

If people go into /usr/share/doc/$PACKAGENAME (you can use any valid package name which you have installed on your system) you will see that the directory has various files in it, at the very least ...
1
vote
3answers
89 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
48 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 ...
-1
votes
4answers
99 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 ...
3
votes
4answers
215 views

Bash script condition always passing even when grep should return nothing

I am having issues when trying to return the one image file that fits the parameters. $1 is the search parameter, in this incident it is "real" which is a tag on one of the images (not two) in the ...
0
votes
3answers
146 views

How to extract a particular value from log file and transfer it to another file

I have a log file which looks like: ########### mv90_poll.sh started Wed Sep 23 02:36:00 PDT 2015 ############ /usr/mv90/mv90/system/*.di not found list *.HHF files in /usr/mv90/fats -rw------- 1 ...
2
votes
2answers
56 views

Identify sub-directories that do not contain a specific string in a specific file

I have a directory named dir1 which has about 800 sub-directories named disp-001, disp-002, ... disp-800. I need to find sub-directories that either do not contain a file stdout or if they do, that ...
3
votes
1answer
69 views

Find words with the same vowel separated with a consonant

I'm looking for a grep argument to find in a text words that have the pattern vowel-consonant-samevowel. Examples: oro enero fofo ppppepepppp Thanks.
1
vote
1answer
66 views

Including or in grep pattern

I want to grep a word wrap in single quote or double quote eg. "myWord" 'myWord' I am using grep -rn "\"\|\'myWord\'\|\"" this should allow "myWord" , 'myWord" , "myWord' and 'myWord' , but ...
6
votes
4answers
297 views

Count number of a substring repetition in a string

I have a file which contains a gene sequence such as: ...
1
vote
3answers
75 views

Search a file for lines NOT containing a phrase with context

I have a file with many entries consisting of a keyword followed by several lines of numbers. I want to filter out one category of entries, so if entry derp is followed by three lines I want ...
3
votes
2answers
232 views

split a large file to new file with unique file names

I appreciate your help if some one could help me with a script that could split a file into unique file names. I can do it with sed command eg, sed -n '/scaffold135_/w 135-scaf.txt' input file.txt ...
1
vote
1answer
203 views

Conditional Check for IPTables

I am trying to check whether specific rules in IPTables exists or not. #!/bin/bash if iptables -L -n | grep -- "ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:8880"; then ...
0
votes
1answer
74 views

grep: memory exhausted on big file when using pipe [duplicate]

When I try to find a specific byte with grep not using pipe I get some output: $ grep -aboP "\\x55" bigfile 510:U 1049086:U 1049598:U But when a pattern is supplied via pipe then there is a memory ...
4
votes
2answers
76 views

How does grep decide that a file is binary?

I have a large utf-8 text file which I frequently search with grep. Recently grep began reporting that it was a binary file. I can continue to search it with grep -a, but I was wondering what change ...
1
vote
3answers
101 views

Grep for multiple strings, show number of lines after one of the strings (but not the other)

As the title suggests, I would like to search a file for multiple strings and for one particular string, show the 5 lines following it as well. cat file | grep -e 'string1' -e 'string2' works of ...
1
vote
2answers
96 views

How to remove a line from a tag in shell script?

We have an xml file (abc_lop.xml) in which I need to remove a line which is present in a tag: Below is an xml file, I have shorten it down as it is very big. <HELLO version="4.2" ...
-1
votes
1answer
33 views

Get the respective tablenames if it matches particular pattern

I need to list the table names if it finds particular pattern from the below file. CREATE TABLE IMS.ACTIVITY ( "ID" INTEGER NOT NULL , OWNING_TYPE FOR COLUMN OWNIN00001 VARCHAR(20) ...
1
vote
3answers
130 views

how to extract a value from a string in xml using grep or awk in Linux

I have a file containing: DEVICE="7" PGMNR="24" VCONF="800" In this file, I want to find PGMNR="24" and I need the value 24 without the ". The PGMNR="24" may be located anywhere in the line. The ...
0
votes
0answers
29 views

grep names from big list and sort it according to pattern [duplicate]

I have 2 big files as shown below. I want to read the pattern file and grep the line from full-info file and sort the channel name order according to pattern. pattern file HBO COMEDY; DTV CABLE ...
2
votes
1answer
104 views

In bash, grep for a line beginning with \, and assign the result using backticks

In bash, I'm trying to grep a file for a line beginning with a \, and return the result using backticks. For example: echo \\Hello > myFile out=`cat myFile | grep '^\\Hello'` echo $out returns ...
1
vote
1answer
62 views

Figure out the PID of exact node.js application

Here is an output of my ps aux: ps aux | grep "node" root 2501 0.0 3.4 720400 68512 ? Sl 16:09 0:03 node /etc/skbx/host/skbx.js 8081 tula13 8082 8084 8085 10.8.0.6 root 20586 ...
-1
votes
2answers
42 views

passing all the contents of a file as keywords after grep command [duplicate]

Suppose I have two files named a.txt and b.txt. Is there any way to use all the contents of file a.txt to use as patterns for grep command if I want to search in file b.txt? General format of grep ...
1
vote
1answer
60 views

grep -E regex syntax changed?

A lot of documentation I've read says that the following command should match lines where x appears at least 3 times in a row but no more than 4 times in a row: grep -E {x}\{3,4} E.g. ...