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

learn more… | top users | synonyms

2
votes
1answer
26 views

How can I use sed to snip a line off of a string?

I'm writing a script to display the domain registrar info and I'm trying to snip off a few lines from what I already have I currently have it grabbing all the indented text from a whois. Now I'm ...
0
votes
1answer
17 views

Add time from all log files using bash

I want to extract time from all of my .log files and add all of these times into a single entity. My log files are placed in nested directories. Directory structure for my log files: |-- temp-system ...
4
votes
2answers
267 views

How can I grep two things where one has option -w?

I have a json file from where I want to get the value of two items which are in an array using grep. Let's say I'm trying to get data from values foo and bar, the problem is that baralso matches ...
1
vote
2answers
87 views

Grep lines that contain “1111-11-11” or “2382-21-20”. (Any Numbers)

I have tried to do this and I came up with this grep -E '\<[0-9]{4}"-"[0-9]{2}"-"[0-9]{2}\>' It doesn't work and the reason for that is the "-" and multiple grep things, so I tried dividing ...
1
vote
0answers
21 views

filter journalctl output, preserve color [duplicate]

I am using journalctl to watch the system by journalctl -f -p 4, but I'd like to grep-out some information. This works: journalctl -f -p 4|grep -v "maximal mount", but grep discards any color info, a ...
2
votes
2answers
35 views

grep for single quote and double quote in a string

so i am trying to write a grep which finds the .epl file names or file paths in a given file. I have tried the follwoing regexs Part 1 : REGEX Used grep -rEco "/(.+).epl|(.+).epl" ./index.epl Result ...
0
votes
3answers
38 views

Extracting a date from a log file and creating a file with unique dates

I would like to extract from a file the date with format DD.MM.YYYY, date is always in the first place, here an example of the entries 15.04.2016 13:13:30,228 INFO [wComService] ...
0
votes
3answers
51 views

How to extract logs between the current time and the last 15 minutes

I want to extract the logs between the current time stamp and 15 minutes before and sent an email to the people configured. I developed the below script but it's not working properly; can someone help ...
0
votes
2answers
60 views

How to process a file containing quotes?

I know a fair bit about escape characters in BASH. I know that \ will escape the next character, " " will escape all characters except for $ and ", and finally ' ' will escape all characters except ...
3
votes
2answers
21k views

Find all lines in a file with a certain character at a certain position

I have a lab question asking me to use grep with a regex pattern to match every word where the third letter from the beginning of the line is an "a" and save it with a redirect. How can I do that?
68
votes
6answers
171k views

How can I count the number of lines of a file with common tools?

I am redirecting grep results to a file, and then using cat to show its contents on the screen. I want to know how many lines of results I have in my results file and then add it to some counter. ...
0
votes
1answer
25 views

How to return the matched expression of a grep command

I am trying to debug a command that I regular use to search across my file system. I have an external file that contains a list of regular expressions that I want to match against. Example [the real ...
0
votes
1answer
28 views

Return owner of process given PID

I'm trying to grab the owner of a process from a list, I have the command pidof nmap to get the then ps -u <PID> | grep USER that I'm currently playing around with, but when I run it, it ends ...
7
votes
6answers
685 views

Number of files not containing given string

I read the question about how to find out the number of files that contain a certain string. This is possible with grep -l "string" * | wc -l. Is it possible to invert this, finding the number of ...
10
votes
14answers
2k views

'ls -1' : how to list filenames without extension

ls -1 lists my elements like so: foo.png bar.png foobar.png ... I want it listed without the .png like so: foo bar foobar ... (the dir only contains .png files) Can somebody tell me how to use ...
2
votes
5answers
213 views

How can I list the names of directories that contain file names matching a regex?

Given a directory X that contains several subdirectories and files, I would like to list the names of the immediate child directories of X that (directly or indirectly) contain file names matching the ...
1
vote
1answer
63 views

Why grep on remote system says file or directory not available

I'm trying to run a grep command on remote system to give me the result for May 22. But reply comes /bin/grep: /var/log/Clients_Logs/127.0.0.1/*: No such file or directory But when I specifically ...
-4
votes
1answer
42 views

recursive script not working

I am trying to add recursive support to my filegrep script. For some reason this does not work as expected: RECURSIVE_MODE=off # iterate over args for ARG in "$@" do if [ -d "$ARG" ] && ...
2
votes
3answers
157 views

reformat a text file to have all text on one line using CentOS 5.6

I have a 210-line block of HTML that I have stored in a HTML for debugging purposes. The HTML, as you would expect, is formatted accordingly so is stored in the file with spaces and tabs. Is it ...
13
votes
3answers
4k views

bash find xargs grep only single occurence

Maybe it's a bit strange - and maybe there are other tools to do this but, well.. I am using the following classic bash command to find all files which contain some string: find . -type f | xargs ...
0
votes
3answers
35 views

Command to print the number of *.pdf files in a chosen directory [duplicate]

I am currently not on a Linux machine. Would the following command produce my wanted result? ls | grep ".pdf" | wc
-3
votes
0answers
31 views

How to compare an input name (first and last) with names in a file (revised) [closed]

How do you compare an input name (first and last) with names in a file and print out either name is good or name is bad? I can't seem to find this anywhere. Do I need to set up the file a certain way? ...
1
vote
2answers
44 views

How can I add a filter to my grep script to NOT include a string?

I'm working on a script that will separate the Registrar information from a domains whois. So far it's working enough but there are a few things that I want to remove in order for it to be a bit ...
3
votes
3answers
1k views

How to count occurrences of all the words in all the files of a directory using grep? But with count incremented only once per word per file

I have already asked a similar question but people misunderstood what i was asking. I was asking how to generate a list of every word with a word count incremented only once per word per file. For ...
0
votes
1answer
32 views

have a log file that I would like to break up into several files based on content

I have a log file that I would like to break up into several files Table of Contents *** Configuration Database *** Logs *** Configuration Logs *** Query Logs . . and so on The issue ...
1
vote
2answers
74 views

Print text between multi-line delimiters

I am trying to extract data between 2 matching patterns, only if the content is not empty and I have issues doing it: here is an example: ============================== Queue Manager is : ...
0
votes
1answer
28 views

how to print the count of matched /unmatched patterns and print multiple patterns separated by line

how to print the count of matched /unmatched patterns and print multiple patterns separated by line. Input example (test.log): This 23 line has eight 8888 This 11 line has three 3333 need 12 to ...
-4
votes
1answer
31 views

Can anyone explain in detail the following two commands regular expressions

The following two commands used to count no of non-zero integers of second column in the text file. Can anyone please explain the regular expression in detail. grep -c '^[^,]*,[^0][^,]*,' < ...
-1
votes
3answers
26 views

Count non-zero numbers for each row of a second column in a text file

I have a text file in that each line words are separated by commas, like this: 7022122465,0,\N,,0,2015-09-29 10:48:33 7022597642,0,\N,,0,2015-09-29 10:48:33 7022848906,0,\N,,0,2015-09-29 10:48:33 ...
0
votes
1answer
3k 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
4
votes
1answer
48 views

Why grepping with color doesn't return anything?

I've got the following foo.txt file: This is the first line. This is the middle line. This is the last line. And I'm trying to grep the middle line just by the word middle and return the ...
7
votes
6answers
6k 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
2answers
89 views

parse HTML in Bash (when grep just isn't enough)

This is driving me insane, as I'm sure there must be some program doing the job but I just can't find it. To be noted, I'd be on ARM/Raspberry so unfortunately Xidel is not an option here, unless ...
1
vote
1answer
32 views

find matching URLs in a line, then shorten them to the domain name

I am new to the Linux command line and need some help. I have a log file I am writing a script for so it only shows certain fields of output. The last bit I need is to shorten the URL's so that it ...
2
votes
2answers
42 views

How to get numbers of specified (range of) length from string with grep?

Here I have a bash "one-liner": cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 16 | head -n 16 | grep '[0-9]', which generate 16 lines of 16 character alphanumeric strings. Example of output: ...
3
votes
2answers
314 views

Getting the combined count of all occurrences of a string under multiple directories?

I have a (hopefully) interesting problem that I could use some advice on. I have a system that's essentially used for storing logs. It has a directory structure like: YYYY/MM/DD/hostname/ There are ...
0
votes
0answers
23 views

grep, failing to find an existing file with correct path given and checked [duplicate]

I need help with grep. Below is what I've written and the If function is working, but the grep part is saying grep: ~/tmp/file.txt: No such file or directory however if I copy and paste the pathway on ...
4
votes
1answer
2k views

Bash, remove lines containing string with grep

This is currently deleting the whole contents of the file in question: #!/bin/bash rpc_port="9201"; dir="/etc/apache2/ports.conf" cat $dir | grep -v "Listen $rpc_port" > $dir The file contents: ...
5
votes
2answers
501 views

Blank lines when executing “grep | xargs” in a “find -exec”

I'm trying to list all directories of a web server that are blocked by Apache via a .htaccess (containing deny from all). I've managed to get the list of blocking .htaccess but when I try to extract ...
3
votes
2answers
66 views

find latest file created in folder and grep contents

I have a need to search a certain folder for the newest created file and grep the contents for a pattern. I have had ok success so far using this command: find /var/log/folder -type f -printf '%T+ ...
1
vote
1answer
45 views

passing ls variable to grep when doing a file search only works inside directory

I am running a command that I use to find the most recent file in a folder then grep the contents for a matching pattern. Here is the command flow that shows the problem I am having. root@backup /]# ...
0
votes
3answers
12k views

Regular Expression for finding double characters in Bash

I am looking for a regular expression that finds all occurences of double characters in a text, a listing, etc. on the command line (Bash). Main Question: Is there a simple way to look for sequences ...
14
votes
8answers
682 views

grepping a fixed string at the beginning of a line

grep "^$1" sort of works, but how do I escape "$1" so grep doesn't interpret any characters in it specially? Or is there a better way? Edit: I don't want to search for '^$1' but for a dynamically ...
3
votes
2answers
36 views

Using grep with the --exclude-dir flag to exclude multiple directories

I am searching through a Ruby on Rails application for a word using grep on OSX, and I would like to exclude directories that match a certain pattern. I am using the following command: grep -inRw ...
0
votes
1answer
32 views

how to interpret “grep -Rl jdk1.7.0_80 . | xargs sed -i s,jdk17.0_80,weblogic”?

I have just installed Oracle weblogic on rhel server. Installation went successfully however the install guide given to me to run below command without explanation. I would like to know what I am ...
8
votes
5answers
39k views

Find the exact string with grep

by way of example, I've a big text file with many email address, using bash I need search/verify that an email exists (or no). Should be use (only) the "anchors"? grep '^user1@example.com' text_file ...
4
votes
3answers
177 views

grep : '+' special character

I did some simple tests with grep '+' and '*' special character $ echo 'where wh+'> /tmp/toto $ grep 'wh[e]\*' /tmp/toto $ grep 'wh[e]*' /tmp/toto where wh+ $ grep 'wh[e]+' /tmp/toto $ grep ...
0
votes
1answer
38 views

bash program to execute something when keyword seen on stdin

Suppose I have an executable which connects to an external machine and outputs a bunch of stuff to stdout over many minutes. At the end of that output there'll be a keyword such as "STOP", or the EOF ...
5
votes
1answer
194 views

Grep word boundaries

Accorging to GNU documentation: ‘\<’ Match the empty string at the beginning of word. ‘\>’ Match the empty string at the end of word. My /etc/fstab looks like this: /dev/sdb1 ...
0
votes
1answer
22 views

How to display only .txt files with the name ScheduledJobs in them

I have tried this: find ~/ops_scripts -type f -maxdepth 1 -name "ScheduledJobs*" but I'm not sure how to get only .txt files to display.