Questions tagged [grep]

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

Filter by
Sorted by
Tagged with
0
votes
1answer
46 views

Grep: Find 3 consecutive char and then follow another char or end of the line

I want to find 3 consecutive characters and then another character or the end of the line. I tried this: egrep '(.)\1{2}' file but I need less results.
0
votes
1answer
110 views

Alpine linux, latest and greatest version of grep

I'm looking to install a version of grep in an Alpine Docker container thats a similar version to my host OS, v3.3. In hunting around online, I see the package is available for Alpine. However, on ...
-1
votes
1answer
105 views

GREP only xyz.log not xyz.log1 [duplicate]

I have a listing of files that have numeric characters at the end after a dot. ex : abc.log abc.log.1 xyz.log xyz.log.1 xyz.log.2 I need to list only the logs that have .log at the ending and not ...
0
votes
0answers
80 views

build regexp that does not depend on the order of input data

In my script I'm dumping some out some data and grep with regular expression rules, which assume that the data come in certain order. However I've found that the order of data may vary. So instead of:...
14
votes
2answers
1k views

How can I prevent Bash expansion from passing files starting with “-” as argument?

I'm trying to recursively search a string with grep but I get this: $ grep -r "stuff" * grep: unrecognized option '---corporate-discount.csv' Usage: grep [OPTION]... PATTERN [FILE]... Try 'grep --...
0
votes
1answer
133 views

Advantage of elasticsearch over grep when trying to search keyword in logs files [closed]

I know elasticsearch is good for full text search but when it comes to analyze logs, say nginx logs what is the advantage of using it over grep (with tail, awk, etc.) ? Quite of often I dig through ...
0
votes
0answers
48 views

How can I find in a log a specific timestamp [duplicate]

This is something I really need on a daily basis and it's really hard sometimes I need to get logs from a specific date time from a log file. This is an example of the file INFO | jvm 1 | main ...
2
votes
0answers
24 views

why does recursive searching with grep print fewer lines when using \bSTRING\b compared to the -w option?

There are two ways to read all files under the current working directory: grep -rw pip | wc -l grep -r "\bpip\b" | wc -l Assuming 1 and 2 both search the working directory for the whole word "pip", ...
-1
votes
4answers
294 views

How to represent greater than or equal 3600 in regex

I wrote this regex to match numbers greater than or equal 3600. This is my attempt. However, I am not sure if it is complete: grep -P '36[0-9]+[0-9]+[0-9]*' test.txt I mean positive decimal integer ...
1
vote
2answers
149 views

find n most frequent words in txt file using the terminal but with the cat, “grep .” and tr -d “/r” commands

I would like to find n most common words in a text file but with the following conditions: cat to get the file. grep . to get the words. tr -d '/r' to get rid of new lines. I've managed to get ...
5
votes
3answers
287 views

Iterate lines of string variable in bash

I have a script where I want to list USB devices using the command lsblk. The command: $ lsblk -o NAME,TRAN,VENDOR,MODEL | grep usb which results in sdb usb Kingston DataTraveler 2.0 sdc ...
1
vote
3answers
344 views

How to find control characters in a file?

I want to find what control characters are in my file. I do not search for specific character, but it is possible \t or \n. I have a program that is telling me: Invalid control character. But when I ...
2
votes
1answer
217 views

egrep in bash script not working

I have this set of commands : s3ls="aws s3 ls $ssyss3/ --recursive --human-readable --summarize" egrepCommand="egrep '$currentDate|Total'" totalSize=$(echo $s3ls|$egrepCommand| awk -F 'Total Size:' '{...
0
votes
2answers
36 views

grep - only show X chars in the result

Is there a way to instruct grep to cut results after X chars? E.g. if I execute grep -iR searchString then sometimes there are compressed javascript files in the result where the whole code is in one ...
2
votes
1answer
142 views

POSIX and Portability | shell scripts | grep -s, grep -q

I am all in for portability in regards to shell scripts. But I am unsure if I am overdoing it right now. In this example, we have a function called confirmation, which accepts the very first ...
1
vote
2answers
15 views

Use multiple expressions in grep consumer

I have these bash functions filter_history(){ grep -v '^cd[^ ]|^ls[^ ]' } get_clean_bash_history(){ cat "$HOME/.bash_history" | filter_history } I am trying to get the bash history but filter ...
-1
votes
1answer
100 views

Is it possible to use the -A paramter on a grep command to print every other line?

As I'm reading from a file, I want to grep every instance of 'x' from that file (line 1), along with the next line (line 2), and the line after that (line 3)... but then I want lines 6, 7, 10 and 11. ...
0
votes
2answers
28 views

Using grep for the output of a command [duplicate]

grep might not be the solution to what I need. I'm trying to grab the MB/s info from this command dd if=/dev/zero of=tempfile bs=1M count=1024 which returns the following information 1024+0 records ...
0
votes
2answers
45 views

How to delete string other than pattern?

I have a m3u file as below. I want to delete all strings except the pattern and next first line. or save the pattern and the next first line as another file. Patterns group-title="TR ULUSAL" , group-...
1
vote
2answers
70 views

Have custom grep exclude dirs for each project

I'm curious if anyone has a way to automatically generate a different set of --exclude-dir flags for grep for each different codebase that you work in. Because in one project, I'll never want to ...
1
vote
3answers
94 views

Why does `^[ ]{0,}` not work with linux grep?

This is my sample text. grep w, grep ^w and grep '^[ ]w' are working just fine. [user@linux ~]$ grep w text.txt whitespace 0 whitespace 1 whitespace 2 [user@linux ~]$ [user@linux ~]$ grep ^w text....
0
votes
2answers
39 views

String to replace in shell upto certain context

I want to replace string in file using sed/grep but the string is mingled with another strings Example: sed -i 's/php/vinu/g' test.yml # to replace string php with vinu test.yml php phpehealth ...
0
votes
0answers
32 views

Grep Word Count is Empty

grep -a 'END:' RTFC-1.bin | wc -l This command is not giving me the word count 'END:' happens to be in a Binary file. I am testing this command on MINGW32 on Windows.
1
vote
1answer
949 views

why grep only allows me to search whole words

I'm trying to grep some code for any code that matches 'enforce' and the following works and shows results: grep --include=\*.rb -rnw . -e enforce yet the following gives no results: grep --include=...
0
votes
1answer
58 views

Insert characters at the middle and the end of a line with specific line length

Currently I am working on a folder of files, and each file have some lines as follows: abcde fghij abcde fghij jklmn pqrst ..... ..... These lines have a specific line length 43. At the ...
1
vote
2answers
44 views

Unexpected grep behaviour when using command substituiton

I'm trying to find and print out all the files that contain a base64 encoded string, using grep and command substitution. If I first encode the base64 string, and then grep, I'm able to find the file:...
0
votes
1answer
44 views

How to grep in the beginning of line using a string which is stored in a variable

I have a shell script; say test.sh with the following contents: for j in *_seqs.txt; do while read line; do count_of_occurences=$(grep "^$line" $j) echo $count_of_occurences done &...
0
votes
2answers
49 views

Use grep command to read a file from specific line number to end of file

Suppose I have a file which is getting appended in every seconds/minutes. At first time I have read whole file and stored the last line number(eg: last_line_numer was : 2000) later I want to read file ...
0
votes
1answer
23 views

Extract some portion from line with regular expression

I have file which consist lines like : 50.57.61.8 - - [04/Apr/2019:09:52:01 -0400] "GET /tracker.php?q=1&p=2 HTTP/1.1" 302 20 "-" "Rackspace Monitoring/1.1 (https://monitoring.api.rackspacecloud....
4
votes
1answer
136 views

Bash Script - Get result of `(.*)` (dot star) in grep regex

Let's say I have the following JSON string in a Bash script variable, DATA: { "id": 10, "name": "Person 1", "note": "This is a test" } I need to get the value of the name field. I have ...
1
vote
2answers
91 views

For loop printing echo command only once

In this small for loop I created, I need the loop to print this message only once for all arguments. for arg in $@ do echo "There are $(grep "$arg" cis132Students|wc -l) classmates in this ...
0
votes
4answers
81 views

Match text between two words with grep on multiple lines [duplicate]

I know there's the a syntax you can use, like: grep -oP '.word1.*?word2' but this doesn't work on multiple lines. So here's an example input: user1:x:1001:1001::/home/user1home:/bin/bash user2:x:...
5
votes
1answer
156 views

Different output when alias

I have a command set as an alias like this: alias badalarm="cat ~/sagLogs/* | grep -I 'failed to generate expected' | awk '{print $4}' | sort | uniq | tee /dev/tty | wc -l" It gives me this output: ...
5
votes
3answers
370 views

Printing a string when grep does not get a match

Suppose I have two files which look as follows: $ cat search_file.txt This line contains kwd1. This line contains kwd2. This line contains no match. This line contains no match. This line contains ...
0
votes
2answers
93 views

Grep -A1 format output

I have 10 files with several IP ping test results --- 10.90.101.224 ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2003ms Some Text here... --- 10.90.101.225 ping ...
1
vote
0answers
35 views

Which tools can find unicode (UTF-8) text in files containing any form of normalization?

I like to find international text, like "müller", or even chinese text, in files, no matter which unicode representation and case is used. I.e, it should not matter whether the "ü" is stored in NFC ...
0
votes
1answer
45 views

sed / grep randomly fails to trim a string

I am using a bash script that will load content from a text file and extract data accordingly. Problem is that sometimes extracted data is preceded by a dot. Code: function str_extract() { pattern=...
0
votes
3answers
64 views

extract lines containing patterns file and save them in different files grep, sed, awk, print

I have this file - . ID = tom_fa_10005086 - 0 Parent = tom_fa_10005086 - 0 Parent = tom_fa_10005086 - 2 Parent = tom_fa_10005086 - 1 Parent = tom_fa_10005086 - 0 ...
1
vote
1answer
289 views

Convert json file to a “key-path” with the resulting value at the end of each “key-path”

I have a large and relatively complex json configuration file that I want to search for keys and/or values using simple tools like "grep". When I 'grep' the file I would like to have in the output ...
0
votes
2answers
177 views

Pipe and filter bash outputs in realtime

I am running some commands in bash (basically some ffmpeg commands), that I'm using grep and awk to filter out the results. The command takes some time and continously outputs some results as it ...
2
votes
1answer
51 views

Grep/sed/awk solution to excludes lines between two search keywords

I see there are ways to print lines between two search patterns, as explained here How to grep lines between start and end pattern? sed -n '/aaa/,/cdn/p' file awk '/test1/,/test2/' I need negation ...
0
votes
2answers
44 views

Don't print the same string again if it was found between two files

I'm looking for the string SUCCESS into the file_1 & file_2 which in this case looks for the string and prints it based on whether its there on only file_1 or on both file_1 and file_2 while I'm ...
0
votes
2answers
106 views

How can i terminate 'find' after the first match?

I am using find in a while loop. it takes a list of ips in ip.txt, searches for the ip in a directory of files and then writes the name of the file that each IP was found in to a file called results....
0
votes
2answers
37 views

finding files of a particular pattern

I want to find names of the file having abc in its name. For example: Dozain123.ctabcln_WbLogReq.log' Dozain123.ctabcln_WbLogReq123.log Dozain123.ctabcln_WbLogReq456.log Dozain123....
0
votes
1answer
48 views

How to capture two regex

In this command, I want to extract strings that are like *.com or *.net, where * is not a dot. I wrote this: grep -P -E '^[^.]+\.com$ |^[^.]+\.net$' example.csv I get this error: grep: conflicting ...
3
votes
3answers
142 views

Extract lines that have two or more dots

I need to extract (or count) the lines (in a file) that have two or more dots. The lines should not start with dot (it’s OK if they end with a dot), and there must not be two dots in a row (i.e., the ...
1
vote
2answers
51 views

grep from list of keywords

Over the past couple of days I've been trying to dig through directories of files to report on files containing key words. Through the help of other users showing me the correct syntax for grep and ...
0
votes
1answer
62 views

Grep not understanding whitespace delimiter?

In the following example: apt-file search apache2.conf | grep -E "apache2.conf\b" Output: apache2: /etc/apache2/apache2.conf apache2-doc: /usr/share/doc/apache2-doc/examples/apache2/apache2.conf.gz ...
1
vote
2answers
65 views

Grep complete name including dot in pattern

I want to grep the complete word with dot. Like I have file test.txt Data in file: Abc ABC.xyz abc ABC.xyz Def DEF.mno def DEF.mno When I am using grep -o I am getting below output grep -o "\w*...
0
votes
1answer
42 views

Formating grep output [closed]

I have website on my hard drive that I need to locate keywords within the HTML files, then output to an html file, listing each find on a new line. Currently i'm useing the line below, but the output ...