Questions tagged [grep]

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

Filter by
Sorted by
Tagged with
1
vote
1answer
22 views

How to select first occurrence from between two patterns including them

What I have: interface GigabitEthernet0/2/1/6.2061 description --> Some description here service-policy input BESTEFFORT-IN service-policy output Egress_30M vrf VPN-s2s ipv4 mtu 1500 ipv4 ...
1
vote
3answers
67 views

How to search for strings that occur more than once in a line?

I know that I can use grep to search for a string. But what if I want the output to show only lines where the string I search for occurred more than once? I prefer grep. To clarify, I need to print ...
0
votes
2answers
28 views

lookup a word in a file and verify the values for the last 3 occurances

I am performing a grep for word "percent" in log strings /var/lib/activemq/data/gc.log |grep percent | grep ten which gives me an output <mem type="tenure" free="2188996168" total="2617245696"...
2
votes
4answers
83 views

More elegant approach to append text to a file only if the String doesn't exist?

I want to append NoDisplay=true to a .desktop file, but only if the entry doesn't exist. I do this the following way: grep -q 'NoDisplay=true' '/usr/share/applications/yelp.desktop' || bash -c 'echo "...
1
vote
1answer
41 views

How to sort the content of text file .txt by files size

the file has been generated by ls -Raltr -block-size=M /* > /home/root/all_files.txt I have tried several commands with grep to no avail.
1
vote
2answers
25 views

Grep data in specific pattern

MY File: "DocumentCreationDate="2019-07-15T23:56:31" SampleID="1" entClassID="65535" ClientID="0" CardID="11209797"" Want to grep Pattern : CardID="11209797" The number may be different between "...
2
votes
1answer
44 views

Piping grep output into grep inside a bash

I've got a text file, and I want to search for lines that have search_string but do not have exclude_string. In terminal, the following gives me the output I want: grep "search_string" | grep -v "\(...
4
votes
3answers
48 views

Grep doesn't work with both -v and -A/B

So I'm trying to use grep with both -v argument and -A argument. So my file has text like: This error -this -this -that [text I want] This error -asd -asfag -adsfhs [text I want] [text I want] This ...
0
votes
1answer
22 views

Grep “Pattern 2” only in files containing “Pattern 1”

I am trying to search for a "pattern 1" only in files that contain another string "pattern 2" E.g. A.txt Pattern 1 ... Pattern 2 B.txt Pattern Z ... Pattern 2 I intend to filter out files that ...
-1
votes
4answers
50 views

Copying first line of text to another text file

Before: file1 123 456 file2 foo testing After: file1 123 456 file2 foo testing 123 Any help would be greatly appreciated.
3
votes
0answers
27 views

Difference in output of GNU and BSD zgrep

I am using zgrep to search a pattern and print N lines after the matched line from multiple .gz files. In BSD zgrep I see a separator -- after each file: zgrep (BSD grep) 2.5.1-FreeBSD $ zgrep "...
0
votes
1answer
50 views

bash + grep only the time stamp from output

we want to capture the time from the output and not the time from the command sshpass expected output Sun Jul 14 12:47:49 UTC 2019 Sun Jul 14 12:47:49 UTC 2019 Sun Jul 14 12:47:49 UTC 2019 but from ...
0
votes
2answers
31 views

How to count number frequency from specific column?

Below I have given my file (filename = 1.txt) structure. I want to count the number frequency from the first column and it should be started from line 3. Because the first line contains 411 and second ...
-1
votes
0answers
33 views

i am facing below error while moving file from path1 to path 2. but file were moved successfully

SCRIPT : FILES=/path1/NAME_????.csv ; for f in $FILES ; do if [ "$(grep -c "^" "$f")" -gt 1 ] ; then echo yes ; else chmod 777 "$f" ; mv /path1/NAME_????.csv /path2 ; fi ; done Error : ...
0
votes
0answers
17 views

Search and display row of third word [duplicate]

How do I display rows that have value larger than 60 on the third word column? Text file 7/12/2019 23:59:45 39.00 7/12/2019 23:59:50 60.38 7/12/2019 23:59:55 0.53 7/12/2019 00:00:00 2.10 7/12/2019 ...
6
votes
5answers
836 views

Extract an attribute value from XML

Using Bash, File: <?xml version="1.0" encoding="UTF-8"?> <blah> <blah1 path="er" name="andy" remote="origin" branch="master" tag="true" /> <blah1 path="er/er1" name="...
0
votes
2answers
32 views

sed deleting everything before ip address of host

I'm looking to delete every line before the IP address of the local host. I'm grepping the host's IP address successfully via: grep `hostname` /etc/hosts | awk '{print $1}' Now I'm looking how to ...
0
votes
6answers
85 views

Simplest way to extract a portion of a string?

I've got a file (bigfile.txt), one of the columns looks like this NW_017095471.1 Gnomon mRNA 108321 109565 . + . ID=rna34;Parent=gene27;Dbxref=GeneID:108565285,Genbank:XM_017925071.1;...
3
votes
2answers
39 views

Parallel Recursive Grep While Omitting String

Problem I cannot perform a recursive parallel zgrep while also omitting a string from the results. I am zgrepping through about 640GB of compressed firewall logs and almost %30 of the lines have the ...
0
votes
1answer
70 views

How to detect and examine recent changes in a text file?

How to get last changes in text file on Linux? Example: text1 text2 text3 and then a programme adds another line to the text file: demo1 If the last change was the addition of demo1, print in ...
0
votes
1answer
27 views

How can I search terminal output?

Say I type git help to learn about the merge command. I don't want to read all the output just the lines that contain merge and their surrounding lines. I thought this would be a common question but ...
0
votes
1answer
46 views

man <sth> | grep -A2 “SEE ALSO”

When I'm grepping a man page for the section titles written in capitals, it seems as if they are not there. It works with the same words in lowercase. For example, when I write man <xxx> | grep -...
0
votes
0answers
31 views

looking for help extracting the last dozen entries that match grep command

I have a log file that I pull data to a private web page to monitor my router. There are alot of entries. for each access control list. right now I have a simple php script. I only want the last 12 ...
0
votes
2answers
22 views

Remove trailing char if condition matches

Let's say I have a file with many URLs: http://foo.com/abc_(Programming_Language) http://example.com/test) I'd like to remove the trailing ) for each line in which there is no opening ( using sed ...
0
votes
0answers
38 views

GREP with multiple exact matches

I have an CSV file which holds information in below format - V1 V2 V3 1: ITC 02-01-12 96.4500 2: ITC 03-01-12 97.5833 3: ITC 04-01-12 97.0500 The Column 'V1' has many ...
1
vote
4answers
99 views

How to convert Syslog Date-Time [UTC] to Epoch?

I am currently piecing together a tool to work with Syslogs generated in my network, one of the requirements is to convert the DateTime from the format in which it is in syslog (%b %d %Y %T) to epoch. ...
0
votes
1answer
43 views

How do I match multiple number of digits using [:digit:] in grep?

I am for the first time using grep and after reading the manual I decide to use [:digit:] instead of \d or [0-9] for matching numbers. I found out that in grep, it is actually [[:digit:]] that matches ...
-1
votes
0answers
48 views

Grep with '$' connundrum [duplicate]

System Ubuntu 18.04. Recently I discovered that on using grep with the '$' characters to search patterns at the end of a line in a text file, it doesn't always appear to work. I was searching my ...
0
votes
2answers
26 views

Grep the URL's embedded in XML file

I'm trying to grep/sed from the following xml data: <record address="11.800.96.10" name="_ldap._tcp.gc._msdcs.example.com" port="3268" target="aws-sit2.example.com" type="SRV"/> I wanted to ...
0
votes
3answers
66 views

How to grep multi lines

I want to find all files, who contains two lines: abc and def. For example, xxx 111abc yyy 222def333 This file should be caught by grep because it contains abc and def. What should I do? I've tried ...
0
votes
1answer
18 views

How to get an output results as text to more than one location at once? [duplicate]

Is there a way that I can get a resulted output to multiple locations at the same time. ls -RsgorCh >> /Users/User1/Text1.txt will print the output as a text file in the given location. I want ...
1
vote
1answer
24 views

Split a long grep pattern into multiple lines

To make my script a bit more readable I'm trying to split my long grep pattern. Is that possible? Example, in by bash script, instead of this long line smartctl -a /dev/sda | grep -Ei "...
0
votes
2answers
34 views

Search a string on the specific columns using GREP/AWK/CUT

Kindly modify my script, because I want to search the word "dBm" in Column 1,3, and 4 of File.csv and return all unmatched lines/records 'cause "dBm" need to be deleted on the file. cat File.csv | ...
0
votes
0answers
12 views

Bash, autocompletion run commands with parameter

I have bash script kbase for 'cat' and 'grep -A10' from specified files for specific word with arguments. Main part of script. cat ~/bash/kb/*.kb | grep "## $1" | grep $2 | grep $3 One .kb file is ...
0
votes
2answers
28 views

How to grep a string with any two numbers that match or that have the first number one less than the second number?

I have a tab delimited file with four columns. I would like to grep for the lines that have a specific pattern in column 1, where it says apple M of N.  I only want to extract the lines that have the ...
0
votes
3answers
38 views

Linux grep - finding lines that contain same number

I am currently working with a file for atomic distances in a protein. Distances are either within the same residue or between different residues. I am wondering if there is a way to sort/find all of ...
2
votes
1answer
48 views

How do I avoid greping matching lines with -

I'm trying to grab only matching lines from a file using grep. The problem is that, when for example I use the following expression: grep -w AFFX-KIT-000088 textfile it will return: AFFX-KIT-000088-...
0
votes
1answer
25 views

Simple Binary Replacement

I tried using bbe as below bbe -e 's/01110011/01111000/' test.txt All I'm trying to accomplish in this example is to read the file as if it's a binary file, and replacing the letter s with x. I'm ...
0
votes
2answers
35 views

Bash - Find ID Pairs on One File Listed in Another File

I have a large file "F1" with listed id-pairs: id1 = digits, id2 = characters id1 id2 id1 id2 ... The goal is to extract the lines containing the id-pairs from a second file "F2", formatted as ...
1
vote
2answers
46 views

How to grep a data within a square brackets?

I have following type of data in my .log file: Cell[BoxData["0.8693473380926441`"] Cell[BoxData["100.40266203596555`"] Cell[BoxData["23.338724857049048`"] Cell[BoxData["0.25720321805387686`"] Cell[...
0
votes
1answer
30 views

match groups with an inverted match

I need to extract with -o1 a group that is matched in lines that do not contain a string. If I use -v pcregrep does not match any groups - which is reasonable: I'm asking to group-match something in ...
0
votes
1answer
21 views

remove specific sysctl.conf setting

on some of my machines i have this block of setting in sysctl.conf(the numbers are different on each machine) : #Size Of RAM (In GB): 23 kernel.shmmni= 2547 kernel.shmmax= 365482458 kernel....
-1
votes
2answers
46 views

How to replace a matching string if n+2 line does not contain a particular string

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/...
0
votes
0answers
12 views

Retrieve number right after grep command [duplicate]

I have a file with a bunch of text in it. I can grep the file as so cat email1.txt | grep np= which will highlight np= in the file. What I need is to record the value right after the '='. Is there any ...
0
votes
2answers
58 views

How would I implement alternation with regex (Basic Regular Expressions only) in grep?

I am aware that this question does not make much sense, since I could just use Extended Regular Expressions (ERE). But let's say for a minute, that implementing alternation is the only option right ...
0
votes
1answer
44 views

How to grep all unique ip address in /var/log/messages and send them to another log file

I am in the prosses of decommissioning an old Redhat public BIND server and would like to find all unique IP address that are sending DNS request to this Nameserver. So far this is what I have been ...
3
votes
2answers
78 views

Sed to print out the line number

Here is my sample file user@linux:~$ cat file.txt Line 1 Line 2 Line 3 Line 4 Line 5 user@linux:~$ I can print line 2-4 with grep -A2 'e 2' file.txt user@linux:~$ grep -A2 'e 2' file.txt Line 2 ...
0
votes
4answers
46 views

Use grep to search for words beginning with non-word characters

The contents of the file testing.txt are: ls -a cmake --verbose verbose I want to use grep to look through this file and find only the word beginning with "--" i.e. the word "--verbose" However ...
1
vote
1answer
45 views

Grep string which contains square brackets

This is the data 10:43:19.538281|TraceDetail |UPBSStandardGenericBillPaymentComponent.PostBillPaymentTransaction|Total Time 19/06/2019 10:43:19.538281|TraceDetail |...
0
votes
1answer
31 views

grep all strings that start with a certain sub string, and finish with the first quotation mark

I'm new to regex and I'm trying to extract all the unique occurrences of each competition. So it should return FA Cup but only once no matter how many FA Cup games are in the file <Date="2014-02-...