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

learn more… | top users | synonyms

1
vote
1answer
58 views

Only grep for lines that only contain alphabetical characters

Question: How can I grep for lines that ONLY have the following characters (all in lowercase)? aábcdeéfghiíjklmnoóöőpqrstuúüűvwxyz example: INPUT: árvíztűrő tükörfúrógép árvíztűrő ...
3
votes
3answers
82 views

Run fgrep multicore mac os x

I'm running this command on my Mac OS X on 8 cores: for i in $(cat file1);do grep "$i" file2; done > output.txt My file2 is 9 million lines so it takes a lot of time. Is it possible to use all ...
3
votes
6answers
206 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 ...
0
votes
1answer
50 views

Regex for matchng anything between parenthesis

I have the following regex so far: printf "this is (test.com)\n" | grep -Po '(?<=\().*(?=\))' The above regex will print the desired output, which is test.com As far as I am adding new ...
0
votes
2answers
39 views

How to match multiple lines starting with a TAB, and the line before the 1st one in a group?

I've got some third party log files I'm trying to pull errors out of on the command line. The logs look like this: time=1 time=2 time=3 at com.test.com.... at com.test.com.... at ...
0
votes
1answer
29 views

grep --exclude-dir behaviour: a bug or a feature?

I have noticed an annoying change in grep -r --exclude-dir behaviour. Consider the following test: mkdir -p ./d1/d2 echo 'randomtext' > ./d1/d2/testfile grep -nr --exclude-dir ./d1/d2 ...
0
votes
3answers
88 views

How can I output sample rows from a .csv?

I'm doing a lot of data merge printing and would like to be able to output, say, 10 samples from a large database. My goal would be to extract the 10 rows spread evenly from the supplied .csv ...
0
votes
2answers
54 views

How to add a column if line contains string?

If I have a tab-delimited file containing the following lines: Sample1-Baboon 1 2 3 4 Sample2-Human 1 2 3 4 Sample3-Human 1 2 3 4 Sample4-Baboon 1 2 3 4 How can I add a new column ...
0
votes
1answer
98 views

Reading a string till a key word and replacing from there with another string

I am new to UNIX and am practising shell scripting. I have an application where I need string searching and replacement. There are some lines in a file 1 and file 2. I have a search key word. If the ...
1
vote
1answer
183 views

grep (/sed/awk) month range

I've looked around for solutions to this, but it's sort of difficult to search for the solution to such a specific problem (lot of words to put in Google). So, I have a file with data that looks like ...
-1
votes
1answer
75 views

Script to `grep` a file with variable [duplicate]

while read wholeline do echo ${wholeline} --outputs John Jones #grab all the lines that begin with these values #grep '^John Jones' workfile2 --works, prints contents of workfile2 where the ...
1
vote
2answers
67 views

Multiple grep commands with sort

I have a text file containing a list of student grades: Andrew cooking A Andrew cooking B Andrew computing F Steven java C Steven python E Ben C++ D Ben python C Jamie java E Jamie C++ E Jamie C++ B ...
4
votes
2answers
112 views

Characters visible in vi, but not in cat.

I have a 300 lines file with ^@ character between each characters in the file. (I cannot post the entire contents for security reasons, so I am pasting only the first line) [mercury@app01 ftp_logs]$ ...
1
vote
1answer
64 views

Find which file contain a particular keyword using `grep`

Suppose I know I have to change myhostname = abc.domain.lcl to myhostname = abc.domain.glbl directive but I don't know in which file exactly the directive myhostname is in (i.e., say I have never ...
7
votes
8answers
687 views

Using 'grep' to find lines that contain all of three specified characters in any order [duplicate]

We have a document which contains lines and we have to find if [a|b|c] appears at least once in every line no matter which order. For example: Input: abc bca cab hhfdhdhfabjfdjdjff acjfdjdfjdf ...
0
votes
2answers
29 views

How to select specific sets or sturture from a file, if struture contains specific string?

I want to extract the sets with having specific string, for example: file abc.txt contains abc 123 abcd 1234 abcde 12345 abc 123 abcd 123 abcde 12345 abc 123 abcd 1234 abcde 12345 Now from the ...
3
votes
2answers
77 views

Grep command that finds/excludes all line where a separator character appears a certain number of times?

Is there a Unix utility to restrict a file or directory listing to given minimum and maximum number of levels deep? I have a git command which displays a unique listing of the directories where files ...
-1
votes
1answer
59 views

How can I recursively grep through several directories at once?

I use grep -r all the time to find occurrences of a string within files in a given directory: $ grep -r "string" app/assets/javascripts > app/assets/javascripts/my_file.js: this line contains my ...
3
votes
2answers
189 views

Capture in shell variables the arguments after a command (between brackets)

Suppose I have a file that contains, among many other things, \command{arg1,arg2,arg3} (arguments been paths, expressed with /, ., characters and numbers) But that a user can as well call it ...
0
votes
1answer
35 views

is there a way to see a listing of packages ONLY from Debian-experimental?

I am on Debian testing/Jessie and I have debian-experimental as well as in my /etc/apt/sources.list. So I am looking to list packages in two ways :- a. List all the packages which are in ...
1
vote
2answers
54 views

Why does regex with \\$\{ work with egrep, but not with sed?

Given a text like this ./RFF_09 -f${FILE} -c${COND} inside a file, this egrep command will correctly match: egrep './RFF(.*) (.*)-c\\$\{COND\}' file but this sed command will not sed -n ...
1
vote
1answer
38 views

Problem with grep on multiple files and not getting desired output

I am using the below command on a file to extract few lines based on chr# ( different chromosome numbers). This is just a single file am working on. i have 8 such files and for each file I have to do ...
1
vote
5answers
897 views

How to use grep and cut in script to obtain website URLs from an HTML file

I am trying to use grep and cut to extract URLs from an HTML file. The links look like: <a href="http://examplewebsite.com/"> Other websites have .net, .gov, but I assume I could make the cut ...
1
vote
4answers
257 views

Regex pattern search for alternating character case

How would I pipe the ls through grep to have an alternating character case, as output? For example: I have files (1) aAbBaBbA, (2) bAbBaA, (3) bbAb,(4) AAaBbAa,(5) BBBaaa, and (6) aBaB. I want to ...
1
vote
2answers
225 views

Why does “grep '.*[s]' file” work and “grep .*[s] file” doesn't?

Comparing grep '.*[s]' file with grep .*[s] file Why do you need quotation marks to let this work properly? In the second case, grep seems trying to inspect every file with a period.
2
votes
2answers
90 views

How to return 0 if a pattern is matched from a file? [duplicate]

I have a binary that repacks android kernel (not mkbootimg). I'm making a script to automate the process. I don't want any output on the screen. So I have redirected the output to a file named ...
-1
votes
1answer
74 views

grep and print all pattern in one lane

I have a file with a list of pattern like this K00001 K00003 K00005 I want to grep and print my pattern in a tab delimited table (the original table is without blank line) like this: K00001 ...
3
votes
4answers
100 views

fgrep beginning of line?

I'd like to use fgrep to handle searching literal words with periods and other meta-characters in grep, but I need to ensure the word is at the beginning of the line. For example, fgrep 'miss.' will ...
0
votes
1answer
158 views

How do I grep on a live log (stdout)?

I'm not sure if that what stdout is, but that's what I think. What I want to do is filter out a certain log message when running an executable. I tried this: Executable 2>&1 /dev/null | ...
2
votes
3answers
212 views

How can I insert the contents of a file into a string in bash

I would like to append the contents of a multi-line text file after a particular line in a string. For example, if the file file.txt contains line 1 line 2 I'd like do so something like printf ...
1
vote
1answer
312 views

How to do a netstat that will only filter based on pid

How can I have the following command below just show/filter based on the PID's I'm looking for? sudo netstat -lp --inet The results come back as this Proto Recv-Q Send-Q Local Address ...
3
votes
1answer
60 views

Show exact number of CRLF occurrences in file

grep -c $'\r\n' file returns the exact same number as grep -c $'\n' file, it shows the number of LF in the file instead of the number of CRLF. (The file definitely does not contain that number of ...
2
votes
2answers
86 views

Group matching with grep includes extra characters

I wanted to extract some text with regex in bash, so I decided to try the following simple example out. echo "abc def ghi" | grep -Po " \K(.*?) " I was expecting to get a "def", but to my surprise ...
12
votes
1answer
1k views

grep inside less?

I'm currently sifting through a lot of unfamiliar logs looking for some issues. The first file I look at is Events.log, and I get at least three pages in less which appear to display the same event ...
2
votes
1answer
89 views

counting occurrences of a string

I have a large database of this format: 0500506 gi|561108321|ref|NC_018143.2| 71446 0 0500506 gi|561108321|ref|NC_018143.2| 71447 0 0500508 gi|561108321|ref|NC_018143.2| 71449 0 0500509 ...
1
vote
1answer
79 views

Why recursive grep does not work with STDOUT? [closed]

I've been long used to have some custom aliases in my Ubuntu/Mint machines, like alias grep='grep -irn --color --line-buffered'. However, I've noticed that since a while ago this stopped working for ...
0
votes
1answer
83 views

Different usage of grep

I know grep can be use to do partial search, for example if i use echo "Enter example" read example echo "Enter Subject" read subject grep -i ^$example info.txt | grep -i ^[^,]*:$subject The ...
2
votes
5answers
908 views

grep files from list

I am trying to run grep against a list of a few hundred files: $ head -n 3 <(cat files.txt) admin.php ajax/accept.php ajax/add_note.php However, even though I am grepping for a string that I ...
1
vote
3answers
182 views

How do I make an output for grep fail?

I am using if-else statement to search for keywords and displaying the results in the terminal, here's an example of my code. read finding if ["$finding" != "" ]; then grep $finding ...
0
votes
0answers
33 views

egrep for specified strings and newline

I receive a prespecified list of strings. In a number of textfiles (example below), I want to search for strings, that do not start with one of the strings in the list. From time to time, a blank link ...
1
vote
3answers
550 views

Pattern Search between specific lines and print line numbers

I have a file of 20 lines. I want to search for a string between the lines 10 and 15 and print the string along with the line numbers of the original file. I have tried sed -n 10,15p file | grep -n ...
6
votes
5answers
1k views

grep skip n lines of file and only search after

I have a huge log file and want to grep the first occurrence of a pattern, and then find another pattern right after this occurence. For example: 123 XXY 214 ABC 182 558 ABC 856 ABC In my example, ...
0
votes
2answers
212 views

Using grep for listing certain lines from a file

Using a random file, such as /usr/share/vim/vim74/tutor/tutor, is there a way to list all lines that start with a 5 letter word ? How about the lines that end with a 5 letter word ? I've been ...
2
votes
1answer
31 views

How do I make grep list the count of matches before the filename?

grep -cZ has the weird behaviour that, while filenames are nul-terminated, the count of matches appears after the corresponding filename. Via od -c: 0000000 / u s r / i n c l u ...
-2
votes
1answer
299 views

How to use Cut and Grep command to find data separated by “:” [duplicate]

In my simple database, it contains text of like, example, soldiers. Name:Rank:Gender:Age:Years of Service Tom Corporal:Recruit:Male:19:2 Nicole Sergeant:Corporal:Female:30:10 Daniel ...
0
votes
1answer
326 views

How to use grep and cut together?

Lets say I'm doing a database for fruits. Using a text file for it, inside contains, in this format, (Fruit:Quantity:Cost) Apple:10:1 Orange:20:2 Pears:10:3 Banana:20:4 10 Apples at $1 each. 20 ...
1
vote
1answer
84 views

How can I teach grep to treat properties-files like text-files

Since I upgraded grep from 2.16 to 2.21 it treats .properties-files like binary files. This sucks because I like to use grep -I ... to exclude binary files from the search results and ...
1
vote
2answers
60 views

find and replace a value in a line of text

I have a text file which contain only 1 line of text 1314:Battery:1.90:50:30 I'm trying to figure out if is there anyway to do a grep and sed to change the value 50 and 30 to the following ...
3
votes
2answers
149 views

Select lines starting with specific numbers

I have to show the PID, PPID and STATUS of the procceses on the system, but only the ones which PID starts with numbers in the range 5-8. So i tried something like this... ps xao pid,ppid,s | grep ' ...
0
votes
1answer
36 views

Why the grep result depends on folder path? [duplicate]

I meet a strange phenomenom when using grep on Solaris 10: I try to use grep to find creat related DTrace probes. When in a folder (/data1/nan), it works OK: bash-3.2# pwd /data1/nan bash-3.2# ...