Questions tagged [grep]

For questions pertaining to grep, a command-line tool for searching text patterns in files. Use this tag for questions about grep itself or questions about issues arising from using the grep command-line tool.

Filter by
Sorted by
Tagged with
2 votes
2 answers
163 views

read line by line and execute cat with grep

Im building a script to load from a file content and then use to cat and grep in another file. The problem is that is not showing any values while executing the grep. If i do the command manually it ...
  • 121
0 votes
4 answers
74 views

Print ONLY the next line after a match with awk

I have a command that prints the line directly after a match with several pipes using different tools like grep, awk, and tail. This works but it feels somewhat messy and uses multiple pipes to ...
  • 173
1 vote
0 answers
11 views

What's wrong with my log file generation?

I'd like to capture and filter the logs from my ESP32 peripheral from the serial port and generate a new log file with the selected line. I do as follows: $ (((stty speed 115200 && cat) </...
1 vote
1 answer
30 views

How to search file name just created by some one on Linux server

How to search multiple files which has been created. I'll try to explain my problem as best as I can, but please pardon my English .. What I'm trying to do, seemed simple enough to me, however every ...
1 vote
1 answer
51 views

Bash Function Grep Mod

TL:DR How do I add a grep to a bash function while allowing a variable number of optional inputs? I find that in repeated grepping of large outputs, I end up clogging the terminal with data. ...
-1 votes
2 answers
30 views

AWK or grep with cut. Need help cutting a field

I have this file and need to cut field 3 which should be cut - f 3. Right? I am using this code and its absolutely not doing anything for me. grep '^2014-' FileName | cut - f 3| less
  • 1
0 votes
1 answer
38 views

Determine whether strings in file exist or not in file hierarchy

I have a list of 500 strings. I want to know if they either do or do not exist within 18566 files in 1770 folders. I need to store the resulting output. How do I do that?
3 votes
1 answer
105 views

Inverse grep does not find all matching lines

EDIT: I am making this too complicated. It's not about inverse grep. I get the same results using just grep -x -f stop.txt < in.txt. If who comes before whose in the stop word file, the result is ...
1 vote
1 answer
165 views

grep $? -ne 0 breaks script

I have a script that executes all commands until it reaches the if statement: Script: #!/bin/bash ######VARIABLES####### #grep return value gret=$? ######LOGIC####### cd /home/logs echo "Enter ...
0 votes
2 answers
41 views

Extract all subnets from its first common octet

I have a list of subnets in a file. I just need to extract those subnets which have common octet as mentioned. I tried using grep "grep -oP '1.[^"]+' but still I got some different results....
  • 149
0 votes
1 answer
47 views

List all the words starting with $ symbol

I want to list all variables in a php file, these are all the words starting with the $ symbol. grep -o '\bm\w*' /var/www/html/my_site/index.php This commands works to list all the words starting ...
-1 votes
3 answers
90 views

How to match end of last string in file which does not end with punctuation

I'm trying to search a directory for files which do not end in a period. I'm trying to list the file name with the final ascii character which is not followed by period/ punctuation. I've looked all ...
1 vote
2 answers
73 views

Arranging the lines in required order

A file contains lines as follows: acb/xyz/row<t> acb/xyz/row<t> abc/xyz/row<1> abc/xyz/row<1> abc/xyz/row<0> abc/xyz/row<0> abc/xyz/row<3> abc/xyz/row<3>...
0 votes
0 answers
29 views

Change import statements dynamically with shell script

Background I have a folder (called classes) in which I have multiple files. I need to write a shell script that goes through all the java files and change the import statement. Example classes │ ├──...
  • 1
1 vote
2 answers
498 views

Why does this regular expression match odd files?

Just playing with regular expressions to learn. Why does it match the other files: errsort, pytest.py, etc.? On the second line adding a question mark on the end matched two more files. Tried grep ...
0 votes
1 answer
20 views

How to get one line above the enabled crontab entry

I have the below crontab entries $ crontab -l #Cron to auto restart app1 #Ansible: test1 #*/15 * * * * ansible-playbook /web/playbooks/automation/va_action.yml #Cron to auto restart app7 #Ansible: ...
  • 429
5 votes
3 answers
328 views

How to use sed, grep, or awk to keep certain lines in a file depending on line numbers in another file

I have two files. File1 contains some sentences, and File2 contains the line numbers I want to keep in File1. For example, File1: He is a boy. She is a cook. Okay. She went to school. She is pretty. ...
  • 233
-3 votes
1 answer
32 views

grep/awk/sed lines from match until text

I would like to get the following output from lscpu: (number of cores) x (cpu model name) @ (max mhz) I already figured out the attribute I need: Model name: Core(s) per socket CPU max MHz But on ...
  • 25
-4 votes
2 answers
40 views

What is the use of -q parameter in grep command? [closed]

echo "$password" | grep -q [A-Z] What does -q do in this command?
0 votes
2 answers
42 views

Zip files that match certain data pattern within the file

I have a directory full of files *.CLM_01. I want to create 1 zip file with all files which have the value "005010X222" inside the file. Basically, there are a few files that have "...
  • 3
0 votes
2 answers
40 views

Grep with regex in CLI returns nothing

I have the following file. //TESTCASES=3 //MARK=9 [runscript] nc dec s10 s11 [/runscript] [checks] [/checks] [testcase] // List: 1, 2, 3, 5, 0xA, -1 .global LIST .data LIST: .word 1, 2, 3, 5, 0xA,...
  • 103
4 votes
2 answers
258 views

The command `grep -Ilri foo . | xargs grep -i bar` works only if the resulting path has no space

To find all the files that have the word foo in it, and also the word bar in it, we can use grep -Ilri foo . | xargs grep -i bar (which is case insensitive and exclude binary files)... however, if ...
  • 2,781
1 vote
1 answer
31 views

Match if a column from one file partially matches a column in another file then print columns from both files

I have 2 files in which the fields are comma separated- aks@dev1:~$ cat dir.txt /home/aks/cleanup,512 /home/aks/git,208 /home/aks/github,424 /home/aks/predirsize,216 /home/aks/sample,288004 aks@...
0 votes
1 answer
58 views

Get rid of UNIX redirection newly created file without using rm

Using below function to filter out text file and organize to new format. Using unix redirection > command to create new files. How can I get rid of creating all new files (e.g. ${file}_final_1.txt,...
  • 5
2 votes
1 answer
183 views

How to get specific fields of JSON objects for which another field is true

I want to get a specific info from the below command [root@test opc]# /bin/oci compute instance list-vnics --instance-id ocid1.instance.oc1.ap-mumbai-1....
  • 149
1 vote
1 answer
31 views

How to print matched filename only once for multiple matches (not printing only filenames)

I'm grepping stuff from multiple logs at the same time with grep -Er But I would like to have the matched file prefix only once. What I want to achieve is kinda like ls -lr (matched file once followed ...
  • 11
3 votes
6 answers
821 views

grep the last occurrence of a pattern before another pattern

I have a huge file contains two types of patterns say pattern1 and pattern2, pattern1 may appear many times before pattern2 appears. I want to grep the last occurrence of each pattern1 before each ...
2 votes
4 answers
179 views

Show changes between consecutive lines of a file

Is there an easy way to see how lines are changing in a file? For example, assume I have this "my.log" file: cpu1 cpu2 cpu3 cpu4 5 3 3 6 5 3 3 6 5 0 3 6 ...
  • 123
0 votes
1 answer
53 views

regular expression - command not found on terminal

I am trying to grab my_name 369 from file.txt I input this grep line with regex but it return an error. I have tried using egrep and it did not work. Input: my_name 369 == match my_name 161 == match ...
1 vote
1 answer
66 views

Search Hexadecimal Sequence from start position until some defined number of bytes of huge Binary File (grep, sed)

In my Mac, I have this binary 'file' /dev/disk0s1 with 512GB (Really is a drive or volume according to AFPS of macOS) and I need to find this Hexadecimal Sequence 0x41, 0x50, 0x53, 0x42 for APSB (in ...
7 votes
4 answers
2k views

What is wrong with using "\t" to grep for tab-separated values?

I have a .tsv file (values separated by tabs) with four values. So each line should have only three tabs and some text around each tab like this: value value2 value3 value4 But it looks that some ...
  • 267
-4 votes
1 answer
38 views

Finding and deleting duplicated records [closed]

I want to find the duplicated record in which same characters are duplicated For example, the pattern that I want to find is 'AA' or 'AAAAA' I try to use the grep command to find it. But it doesn't ...
0 votes
1 answer
56 views

grep exit with status 1 when found zero value

below is my code 1_week=$(git log --since=$(date +"%Y-%m-%d" --date="@$(($(date +%s) - 604800))")|grep '^commit ' |wc -l) && 2_week=$(git log --since=$(date +"%Y-%m-%d&...
  • 51
6 votes
3 answers
411 views

Why do empty matching regexps work differently in sed and grep?

Using, for example, the tools on a Debian or Debian-derived system, a regex like N*, which could match the empty string, could result in a match in sed: $ echo 'Hello' | sed 's/N*/ xx&xx /g' ...
4 votes
5 answers
934 views

What is the sed equivalent for "grep -o"?

I would like the sed equivalent to: grep -Eo 'regex'.  I might, hypothetically, want to do further work with the output.  This step might be just the first part of something that will be more ...
  • 59
1 vote
2 answers
49 views

grep -v [string] for whole folder

I have a folder, /path/ , with files all formatted to .filetype, and I am looking to perform: grep -v [string] [filename] > [filename copy].filetype for ALL items in this folder. I assume I'll ...
  • 13
0 votes
1 answer
51 views

Bash, grep - why is a command with a caret not working in a subshell?

I'm trying to determine if a git add action has created any expected modifications, and the following works in an interactive shell $ BASE_DIR=foo/bar/baz $ git add "${BASE_DIR}" $ git ...
  • 229
0 votes
1 answer
27 views

Running multiple zgreps, looking for performance improvement

I have a script which searches through some log files, all of which are in .gz format. I feed the script a search term, and it zgreps for the term, then grabs the transaction ID from the start of the ...
0 votes
1 answer
47 views

Find the highest number after specific string from mutiple files

Each file has similar content: number 1: asdfa11sdfsadf number 2: asdfa12sdfsadf number 1: asdfa1sdfsadf number 3: asdfa33sdfsadf return 3 or number 3: are equally good. Have tried: max="$( awk ...
  • 105
1 vote
3 answers
86 views

Filter git log of pom.xml using regex with grep/perl

I'd like to filter the content of a log of a pom.xml file using a regex. I created it on regexr and it works using PCRE; this is my regex: commit \K[a-z0-9]*(?=[\s\S]*\+.*<version>1.2.0) I then ...
0 votes
2 answers
81 views

Search for asterisk (*) inside the text files and copy to a newdirectory?

I had 5000 text files in a directory, each text file name starts with a prefix OG00* (ex - OG0017774.log) The .log files which contains asterisk (*) inside the file, need to be copied into a new ...
0 votes
0 answers
27 views

Why is my script misbehaving sometimes? [duplicate]

I have created a script that can split an audio file into smaller files via ffmpeg using timestamps, and another script which can take in timestamps & names in batches. My use for this script ...
  • 1,861
0 votes
1 answer
27 views

return unique tld name from big domain name CSV file

i have a very big domain name CSV file that contect full domain name like: facebook.com google.com aws.com.in aws.co asw.co.in aws.com net.com net.in new.net.in So unique tld name result should be as ...
0 votes
0 answers
91 views

Writing a File with CSV Header Column and Unique Column Values in Bash

I have a bunch of csvfiles that I'm importing into a database. I'd like to get a preview of the unique values in each column to help me create the tables. I've written a script that takes a input csv ...
5 votes
3 answers
254 views

How do I grep for a range of unicode characters?

How do I grep for a range of unicode characters? I've seen an example for one character. How to grep characters with their unicode value? I'm interested in a method other than the shell substitution ...
  • 249
1 vote
2 answers
69 views

How to prevent grep --color from botching the output when a carriage return from Windows line ends is matched?

I have a file with some lines likes x y and when I run grep -E "x$" filename.txt it does not match anything. As vi shows ^M in the end of most lines, I guess the problem is mixed Dos and ...
  • 907
1 vote
2 answers
66 views

Explicitly Display Newlines with grep

Is there a way to get grep to display the newlines in a file in a human-readable way, for example, in the way vim displays end-of-line characters as $ with :set list? I'm trying to describe how dot (.)...
0 votes
0 answers
97 views

The total size of the argument list and exported variables exceeds the OS limit of 1MB

I was doing a grid search for some scientific computing project, and got a bunch of log files with parameters embedded in the filenames. $ ls -l | wc -l 38210 $ ls -l | tail -rw-rw-r-- 1 qys ...
  • 369
2 votes
4 answers
901 views

Grep excluding line that ends in 0, but not 10, 100 etc

I have a file that has this pattern in Linux and Unix environments. # file1 text1 98432230 text2 123412 text3 10 line4 0 line5 0 line6 40000 ... ... line10 ...
  • 23
2 votes
6 answers
270 views

Grep repeating patterns with a loop

I have two files: file1: ABA FFR HHI HAB file2: ABAABAABAABAABAABAABAABAABATRCFUJIKHRTHVFHJJHVHJJKKHGCC FFRFFRFFRFFRFFRFFRFFRFFRFFRFFRFFRFFRFFRFHJKGHKKBVDTHJNJ ...
  • 25

1
2 3 4 5
96