grep

Linux command line global search and replace

I'm trying to search and replace a string in all files matched by grep on a linux machine. I've got some pieces of what I want to do, but I'm unsure how best to string them all together. grep -n 'foo' * will give me output in the form: [filename]:[line number]:[text] For each file returned by grep, I'd like replace "foo" with "bar" ...

Using sed, how do you print the first 'N' characters of a line?

With sed, what is a one liner to print the first n characters. I am doing the following. grep -G 'defn -test.*' OctaneFullTest.clj | sed .... ...

lgrep and rgrep in Emacs

I am having problems with the greps in Emacs. a) grep doesnt seem to understand the .[ch] for searching .c and .h files. This is a default option provided by Emacs with the lgrep command. The example is searching for the word "global" in .c/.h files. grep -i -nH "global" *.[ch] grep: *.[ch]: No such file or directory Grep exited abn...

What's the difference between grep and map in Perl?

In Perl both grep and map take an expression and a list, and evaluate the expression for each element of the list. What is the difference between the two? ...

Looking to replace option value="" with the text between <option value="">this text</option>

I have this html, and I want to replace the numeric value within value="##" with the value between <option>value</option> For example: <option value="16">Accounting</option>, I want to know the regex it'd take to automatically change it to <option value="Accounting">Accounting</option> I plan on doing it to this entire list. <option v...

Filenames and linenumbers for the matches of cat and grep

My code $ *.php | grep google How can I print the filenames and linenumbers next to each match? ...

How can I grep for a count of pipes ('|')?

I have tried: grep -c "\|" *.* But it didn't work, since it gives an incorrect count of consecutive pipes. How can I accomplish this? ...

An encoding-savvy grep replacement?

I am frustrated that grep fails to find a word like "hello" in my UTF-16 documents. Can anyone recommend a version of grep that attempts to guess the file encoding and then properly handle it? Thanks! ...

Egrep acts strange with -f option

Hi all, I've got a strangely acting egrep -f. Example: $ egrep -f ~/tmp/tmpgrep2 orig_20_L_A_20090228.txt | wc -l 3 $ for lines in `cat ~/tmp/tmpgrep2` ; do egrep $lines orig_20_L_A_20090228.txt ; done | wc -l 12 Could someone give me a hint what could be the problem? No, the files did not changed between executions. The expected ...

Bash history re-runs: possible command to avoid using !bang! ?

Scenario: You are doing your daily Bash shell stuff. You want to run a previous command so you type: history | grep foocommand Then you get a list of all the foocommand stuff you did for however long your history has kept track, in a list like so: 585 foocommand --baz --bleet 750 foocommand | grep quux 987 history grep | fooco...

Grep Regular-Expressions on more lines

Dear all, I am writing a python program that is retrieving edifact log messages from a .gz file... An example of 2 logs are the following: 2009/03/02 12:13:59.642396 siamp102 mux1-30706 Trace name: MSG Message sent [con=251575 (APEOBEinMux1), len=2106, CorrID=000182C42DE0ED] UNB+IATB:1+1ASRPFA+1A0APE+090302:1213+0095JQOL2 2009/03/02 12...

zcat pipe to grep

ls -ltr|grep 'Mar 4'| awk '{print $9 }'|zcat -fq |grep 12345 I want to find all files modified on a certain date and then zcat them and search the fiels for a number string. the above doesn't work because it searches the file name for the string not the file itself. Any help? M ...

zcat to grep with file name

ls -ltr|grep 'Mar 4'| awk '{print $9 }'|xargs zcat -fq |grep 12345 I'm now using this command to list the records that contain my numeric string how can i alos get this command to print the name of the file the strings were found in? thanks ...

What's the most compact version of "match everything but these strings" in the shell or regex?

Linux: I want to list all the files in a directory and within its subdirectories, except some strings. For that, I've been using a combination of find/grep/shell globbing. For instance, I want to list all files except those in the directories ./bin ./lib ./resources I understand this can be done as shown in this question and this othe...

Am I understanding the sequence of events in this Ruby grep example?

I'm trying to understand how grep works in this example. The code works but I'm not 100% sure in what sequence the events take place or whether I'm correctly understanding what's being returned when and where. cars = [:Ford, :Toyota, :Audi, :Honda] ucased_cars = cars.collect do |c| c.to_s end .grep(/^Ford/) do |car| puts car.upcase ...

How do I search a directory of files using another file as input and sending the output to another file?

I am working on a Unix system. I have a directory of files called MailHistory. Each file in the directory contains all of the emails for the previous day. The files are created at midnight and named with the timedatestamp. So, a typical filename is 20090323000100. I have a file that has a list of names. Using this file as input, I...

Regular expression for finding a regular expression?

Does anyone have code for finding a file that contains a regular expression? I would assume you could have two different flavors, one for BREs and one for EREs. You would think some kind of test suites would have something like an isRegex() test. Can anyone have any code? Looking for something comprehensive of course. I see this was...

Use lines in a file as filenames for grep?

I have a file which contains filenames (and the full path to them) and I want to search for a word within all of them. some pseudo-code to explain: grep keyword or cat files.txt > grep keyword cat files txt | grep keyword the problem is that I can only get grep to search the filenames, not the contents of the actual files Thanks for...

How do you search all source code in Vim?

When using Vim, and given a directory filled with code (e.g. ~/trunk/) with a number of sub-directories, is there a way to grep/search for instances of text/regexes across the entire source code? At the moment I use: :lcd ~/trunk :grep "pattern" *.py */*.py */*/*.py */*/*/*.py (Obviously I'm limiting this to Python files, which is a ...

Windows recursive grep command-line

I need to do a recursive grep in Windows, something like this in Unix/Linux: grep -i 'string' `find . -print` or the more-preferred method: find . -print | xargs grep -i 'string' I'm stuck with just cmd.exe, so I only have Windows built-in commands. I can't install Cygwin, or any 3rd party tools like UnxUtils on this server unfort...