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

learn more… | top users | synonyms

1
vote
0answers
34 views

grep not matching patterns in scripted session [closed]

We have a bunch of reports that run on a Linux server and I am trying to automate checking them and bringing back relevant parts using Powershell. I'm using a module called SSH-Sessions to connect to ...
3
votes
2answers
319 views

grep on a variable

Lets say I have a variable line="This is where we select from a table." now I want to grep how many times does select occur in the sentence. grep -ci "select" $line I tried that, but it did not ...
5
votes
4answers
370 views

Replace a string containing newline characters

With the bash shell, in a file with rows like the following ones first "line" <second>line and so on I would like to replace one or more occurrences of "line"\n<second> with other ...
9
votes
11answers
9k views

How can I “cat” a file and remove commented lines?

I'd like to know if there is a way that I could cat file like php.ini and remove all lines starting with ; For example, if the file contained this: ; - Show all errors, except for notices ; ...
2
votes
2answers
165 views

Limit grep context to N characters on line

I have to grep through some JSON files in which the line lengths exceed a few thousand characters. How can I limit grep to display context up to N characters to the left and right of the match? Any ...
1
vote
0answers
55 views

In adb logcat | grep, how to get the entire original line? [closed]

I am using grep on the Android Logcat to only watch the lines related to my application. My command line is like: adb logcat | grep 'MyAppName' But the results I get is not the original logcat ...
3
votes
1answer
173 views

grep not working in a for loop over a list

I have some log files. And I have a list of user ids. My log files are in a particular format log-yyyy-mm-dd e.g. log-2014-09-30 I am trying to search each userId in the log file. This is what I am ...
1
vote
1answer
35 views

grep *gcc* doesn't match anything

I'm using the following code $ rpm -qa | grep -i gcc libgcc-4.8.3-7.fc20.i686 gcc-c++-4.8.3-7.fc20.x86_64 gcc-4.8.3-7.fc20.x86_64 libgcc-4.8.3-7.fc20.x86_64 $ rpm -qa | grep -i *gcc* $ I would ...
1
vote
2answers
83 views

Print a line in stdout that matches an expression if the output contains another expression

This might be a common/easy task but I couldn't figure out from examples on the web or manuals of awk/sed/grep. So, here is the scenario: There is an internal command line tool that prints out a ...
2
votes
2answers
50 views

grep command to display only the two words searched for separated by a comma?

Say I have a file with the following text: Hello there, How are you? How can I grep for Hello and are such that the output of grep looks like: Hello,are
6
votes
3answers
254 views

Empty a file without grep subsequently treating it like a binary file

Currently I have netcat piping output to tee which is writing to output.txt with nc -l -k -p 9100 | tee output.txt I want to monitor this output, so I'm watching it with tail -f | egrep -i 'regex' ...
3
votes
3answers
208 views

Looking to grep or egrep year ranges from 1965-1996

I have a grep that works for some of the dates but having trouble getting my brain to make it fully functional. grep 19[6-9][5-6]$ filename it catches a few correctly but I'm looking to grab ...
1
vote
3answers
26 views

Script to search for 3 different users online and print that

I am attempting to create a script where a user can enter 3 different arguments (usernames) to see if they are online. I currently have who | grep "^$1" which will bring up whoever is searched then ...
1
vote
1answer
62 views

Understanding grep and pipes in linux

I came across this post which explains my problem. Suppose there is a file called file.txt which contains "foo World". The answer posted by Tyler explains a lot however I am confused as to how cat ...
0
votes
0answers
63 views

BC returns syntax error

#!/bin/bash cd ~/simplesim-3.0/benchmarks INSTS=$(grep "sim_num_insn" ./anagram.txt | awk '{print $2}'); IL1_ACCESSES=$(grep "il1.accesses" ./anagram.txt | awk '{print $2}'); IL1_MISSRATE=$(grep ...
2
votes
1answer
60 views

Quotes in commands - grep example

I wanted to know when to use quotes with grep or other tools. For instance the following two command give the same results. [Jhm@localhost /]$ grep Hello ./testfile Hello world [Jhm@localhost /]$ ...
1
vote
2answers
133 views

How to do a grep on remote machine and print out the line which contains those words?

I have few logs files in my machineB under this directory /opt/ptd/Logs/ as shown below - My logs files are pretty big. david@machineB:/opt/ptd/Logs$ ls -lt -rw-r--r-- 1 david david 49651720 Oct 11 ...
3
votes
2answers
137 views

ack/grep search and sort by modification date

Is there any way to have ack sort the results found by date of modification? (ideally showing the date next to the result?). It doesn't look like ack has a date option, but just in case. If this ...
5
votes
7answers
9k views

How to print all lines after a match up to the end of the file?

Input file1 is: dog 123 4335 cat 13123 23424 deer 2131 213132 bear 2313 21313 I give the match the pattern from in other file ( like dog 123 4335 from file2). I match the pattern of the line ...
3
votes
3answers
512 views

grep filter all lines plus one before each hit

I want to parse some output so I exclude all lines that contain either foo or bar, as well as all lines immediately preceding those lines. For example: echo " 1 some line 2 line to exclude because ...
1
vote
3answers
185 views

keep only certain part of a string in a certain column

I have a file like this: id target_id length eff_length 1 intron_FBgn0000721:20_FBgn0000721:18 1136 243.944268 1 intron_FBgn0000721:19_FBgn0000721:18 1122 ...
0
votes
2answers
57 views

Regex to grep filler followed by signed float

I want to extract the last column from lines that look like: 880.952 NAV_Y uSimMarine -146.17516 The number isn't always negative, and isn't a fixed length (i.e. doesn't ...
0
votes
3answers
49 views

Filter names from textfile

I have a file where there are names sitting in the first character position. I am trying to find a way to grep all the names. Example here I would want the names BUBBA and SUSAN listed. BUBBA = ...
12
votes
8answers
2k views

How to perform a multi line grep

How would you perform a grep for text that appears on two lines? For example: pbsnodes is a command I use that returns the utilization of a linux cluster root$ pbsnodes node1 state = free ...
3
votes
1answer
2k views

How to grep top most frequent error messages in a unix logfile

If I have a file example apache log file How to extract the top most frequent error messages in a unix log file with no timestamps the key is most frequent error message should come on top of the ...
1
vote
1answer
104 views

Regex works in shell but not through Perl script

I'm having trouble executing a Perl script through the Unix shell using Perl's system command. I've had more complex regex commands I had to adjust accordingly to convert from Unix to Perl, and ...
1
vote
2answers
63 views

How to match a number in one file to a name in another file?

Okay, I hope this is the right place to ask. I have 2 files. The first file has 4 fields: class CRN #, Instructor ID #, Class Section #, Number of Students. Example: 0002 T00005 006 15 0004 ...
1
vote
2answers
53 views

Strings not outputting text found using grep

I am looking for certain strings in (Windows) executable files. First I started with grep -a string file as that treats binary files like text files. The problem there is there is no neat output, ...
3
votes
3answers
531 views

How can I open a file that results from grep?

I often grep a bunch of files to find a line, and then grep returns one result. Rather than copying and pasting the filename into a new command, I'd like to be able to open that one result with an ...
0
votes
4answers
89 views

grep for files and open the resultant matches

I'm trying to open all files based on some return values from grep in the command line. I know that I can use the -l option to only return filenames, but I would like to combine this somehow with ...
4
votes
2answers
60 views

How to exchange words in a filename using the shell?

One of my customers is a school. On their file server all projects have a directory with the same naming scheme. <last name>, <first name> <second name>? - <project ...
3
votes
1answer
93 views

GNU Parallel - grepping n lines for m regular expressions

The GNU parallel grepping n lines for m regular expressions example states the following: If the CPU is the limiting factor parallelization should be done on the regexps: cat regexp.txt | ...
1
vote
2answers
88 views

grepping for multiple items

I have about 6 items I need extracted from and LDAP base dump file fullName: uid: email: ... the colon is required for the search, mainly because uid appears in a number of places in the file but ...
1
vote
1answer
105 views

Exclude hidden subdirectories from grep -r

I have a svn local copy, i want to make a search on the string some_string. $ grep some_string * -r lang/en:some_string=Some string lang/.svn/en:some_string=Some string But if a go little further, ...
0
votes
5answers
887 views

bash event not found trying to match and exclude parenthesis in grep

In a very long line I'll summarize with: (foo),(bar,baz(word,right),(end) I want to print only: (bar,baz(word,right To match the second parenthesis, I exclude the word that follows the ...
0
votes
3answers
44 views

How to make a grep which excludes a bunch of pipes?

I want to make a diff of hostnames using grep. I have 2 files. One yaml and one .pp. In the yaml I have domain names with ips and in the .pp file I have fqdn and ip in hash style. Like this: In the ...
2
votes
1answer
131 views

grep search expression and rename file

I have readable pdf files that all contain the string VAX followed by eight numbers, like VAX00002885. Now I want to find that expression and rename the corresponding pdf to VAX00002885.pdf (in this ...
2
votes
2answers
151 views

How to get grep exit code but print all lines?

I'd like to check whether a line printed by a command contains an error message, but I'd like to also print all the output from the command (for make logs). Is there some way to get all the output of ...
1
vote
3answers
277 views

“grep” the available space from “df” output

Hello I need to grep the output from df. Sadly awk is not an option (even though its the easy option) here I can only use grep. Filesystem 1K-blocks Used Available Use% Mounted on none ...
0
votes
1answer
83 views

Very basic H/W question using pipe and grep

I have two text files, F1a.txt and F1b.txt, with essentially the same data: the driver's surname and their nationality. For example: Hamilton GBR Alonso SPA I have concatenated these ...
0
votes
1answer
129 views

Using Grep Colour to look in a file, and output with a coloured answer in amongst the file contents

I am currently in the process of writing a program in raspberry pi that will allow me to look inside files and have what I entered displayed in colour I think I need to do something to do with ...
0
votes
5answers
167 views

df -k + how to match the root volume line

please advise how to match the "/" volume from df -k command? I tried the following: [root@machine /]# df -k | grep "/" /dev/vx/dsk/bootdg/rootvol 130926685 71378710 58238709 56% / /devices ...
1
vote
2answers
99 views

run grep cmd recursively and print path name and result

I have a log file that exists in multiple directories one for each client. I am trying to run the following: find . -iname "20140926.log" -exec cat {} \; | grep 123456 | grep 'food="100"' | wc -l ...
2
votes
2answers
93 views

redirect output of tee and grep for multiple logfiles

Redirecting output from a script STDOUT + STDERR toLogfile 1 and a grep to Logfile 2 ./run_test.sh 2>&1 | tee -a /var/log/log1.log | (grep 'START|END') > /var/log/myscripts.log How can I ...
0
votes
1answer
69 views

Finding words with one vowel group

I am trying to use a command to find all words in a list of words with one group vowels. Like four, for and far, but also words like ore and or. What I got so far: grep -i ...
2
votes
0answers
47 views

Using grep to locate string from a text file [closed]

I need to use it to so the program asks the user for the text file and then the string located in that file. Then the program will tell the user whether the string exists in the file.
2
votes
1answer
81 views

make grep confirm all matches

I am writing a "unit test" for a bunch of .tex files and want to check for incorrect entity names, e.g. "Data Items" instead of "DataItems". Currently, I use grep to search for the incorrect ...
5
votes
3answers
169 views

I am trying to find a better/easier way to count alphabetic characters in a file or shell variable

I have a file containing the following text: we are going to write something 1 2 3 wc tells me I have 1 line 9 words and 38 characters. I'm looking to count the 26 letters only (a-z, no numbers or ...
-1
votes
1answer
75 views

Match and combine records in Bash [duplicate]

Consider two csv files: $ cat given.csv 123,John 246,Paul 369,George 987,Ringo $ cat family.csv 246,McCartney 123,Lennon 987,Starr 369,Harrison Note that the lines are not ordered! How might I use ...
1
vote
2answers
148 views

Count how many lines start with which characters

I have a file with a number of lines in a file filename. I want to count how many lines start with character 'a', with 'b' and so on in one go. What command i should execute.?