Tagged Questions
3
votes
3answers
116 views
Longest common substring using grep
I have a huge text file called dictionary.txt with entries like
ABC_SEQ_NUM This represents....
ABC_RANK This represents....
ABC_BSC_ID This represents...
PQR_TA_DATE_AF This ...
2
votes
2answers
102 views
Grep: search and replace full line
The command
grep "foo" myfile.txt
prints all matching lines in my file.
Now I want to replace the full line with another string. How can I do that?
0
votes
2answers
119 views
What is the python equivalent of grep -v?
I like grep -v. I use it all the time. But I am also doing some text processing in python, and there is one crucial thing that I lack.
Usually, I use grep -v to take extraneous stuff out of text.
...
1
vote
3answers
110 views
grep equivalent of the kwrite regex [A-Z][A-Z]+
So, it took me ages, but I finally learned to think in terms of regular expressions, thanks to using them in kwrite.
But I still don't know how to translate that knowledge to grep. I love my grep, ...
6
votes
1answer
113 views
How to detect and delete lines containing ˆ@
I have a simple problem:
In my file, the are lines containing the string ˆ@ˆ@ˆ@ˆ@ˆ@ˆ@. I just want to delete all lines with this string, using for example the sed or grep commands.
And I would like ...
1
vote
2answers
227 views
How to search and replace text in all php-files in a directory and it's subdirectories
I am looking for a shell script that recursively traverses all .php files in a directory and performs a search & replace of a particular text pattern.
The search pattern is quite long ( > 5000 ...
2
votes
5answers
102 views
evaluate multiple patterns from program output and write into pattern specific files
I have a script outputting some value/numbers and I want to split those into two files. I am looking at something like:
./runme.sh | grep 'ook' >> ook.out | grep 'eek' >> eek.out
Where ...
2
votes
3answers
125 views
Returning only the portion of a line after a matching pattern
I have a file (file_name) which contains exactly one occurance of the string Result:, at the start of a line. I want to print all the characters after the string Result: in that line until I encounter ...
3
votes
4answers
126 views
Find a string (like grep -q) within only a section of a file
I want to write some Bash that can verify that a string exists in a configuration file. I can't change the file format, it belongs to a different application.
The file is subdivided into groups named ...
11
votes
3answers
234 views
How can I redirect matching lines to a file, and non-matching lines to a different file?
Is there a script/program/utility already available for the following requirement in a optimised way?
someCommand |
tee >(grep "pattern" > LinesWhichMatch) |
grep -v "pattern" > ...
3
votes
3answers
245 views
How do I grep without leading whitespaces?
I'm greping through a large codebase, and leading whitespaces and tabulation seem to be quite annoying. Is there any way to get rid of it?
grep -R "something" ./
E.g, instead of:
foo/bar.cpp: ...
3
votes
2answers
164 views
grep surrounding characters of a match
I'm looking to do a find and replace within a giant database dump, and it's not doing what I think should happen. I'd like to grep for my target string in the file, and then see the surrounding 8 ...
2
votes
4answers
481 views
Deleting Lines Containing a Specified String in Different Files
Suppose there are two files in web/ named foo.php and bar.php. The 1st line of foo.php is "sdajgeSTRINGdsad" and the 10th line of bar.php is "gdfhu98324STRING". The task is to first locate these two ...
2
votes
1answer
68 views
Is there a way to get the effect of combining grep -v with grep -A?
Is there a way, either with grep or something else, to (in effect) combine grep -v and grep -A? That is, rather than include everything for x lines after a string, exclude everything.
Quick reminder ...
3
votes
3answers
1k views
How do I recursively grep through compressed archives?
I'm trying to find out what modules use Test::Version in cpan. So I've used minicpan to mirror it. My problem is that I need to iterate through the archives that are downloaded, and grep the files ...
7
votes
3answers
619 views
Can grep output only specified groupings that match?
Say I have a file:
# file: 'test.txt'
foobar bash 1
bash
foobar happy
foobar
I only want to know what words appear after "foobar", so I can use this regex:
"foobar \(\w\+\)"
The parenthesis ...
5
votes
1answer
83 views
Search for a text in Linux but don't display the complete line in results
I'm using grep to do this, like:
grep someText file.log
But this returns the complete line in results. If someText is found in the middle of a line I don't want to ouput the whole line, just the ...
6
votes
5answers
569 views
Is there any alternative to grep's -A -B -C switches (to print few lines before and after )?
grep -A 2 -B 3
prints 2 lines after the grep string and prints 3 lines before.
grep -C 3
prints 3 Lines before and 3 lines after
Unfortunately, the grep I'm using does not support these ...
14
votes
6answers
848 views
Is there a way to modify a file in-place?
I have a fairly large file (35Gb), and I would like to filter this file in situ (i.e. I don't have enough disk space for another file), specifically I want to grep and ignore some patterns — is there ...
1
vote
3answers
249 views
Switch from grep to sed
I have a files students.txt, with lines of the form:
Surname, Forename: Day.Month.Year: Degree
For example:
Smith, John: 15.01.1986: MSc IT
Taylor, Susan: 04.05.1987: MSc IT
Thomas, Steve: ...
2
votes
1answer
100 views
The cursor to a match, edit, the cursor to the next match, edit — like in a directory?
Algorithmic blackout:
problem: find the files that match
problem: stack/buffer the matches
move the cursor to the next file of the stack and thereby to the location of the match
repeat step 2
...
8
votes
5answers
483 views
How intelligent are unix utilities?
Unix utilities like sort, find, grep, diff et al are very handy to perform quick tasks, sometimes without writing any code at all.
I wanted to know what algorithms do they internally use and how do ...
2
votes
3answers
709 views
Extracting tokens from a line of text
Using bash scripting and grep/awk/sed, how can I split a line matching a known pattern with a single character delimiter into an array, e.g. convert token1;token2;token3;token4 into a[0] = token1 … ...
8
votes
2answers
954 views
How to remove multiple lines per occurrence in a file?
Say I have this 857835 line file, containing stuff like this:
a1
rubbish1
rubbish2
rubbish3
rubbish4
a1
rubbish5
rubbish6
rubbish7
rubbish8
And I wish to remove all occurences of a1 and the next ...
3
votes
4answers
254 views
How do I check if a file already has line with “contents” in it?
I need to know if a file aready has line with contents X in it, if not append line. here's the code I've tried.
if ! $(grep 'eval $(perl -I$HOME/foo/lib/perl5 -Mlocal::lib=$HOME/foo)' ~/.bashrc)
then
...
1
vote
1answer
153 views
How can I grep through files whose name begins with `-`?
I've tried this.
grep -r thanks * slave-iv
grep: invalid option -- '@'
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.
Here's ...
5
votes
1answer
114 views
How do I take a list and remove it from a file?
I have a long list of domain names that I need to remove from /etc/remotedomains. They're probably not in any particular order in the file. Each domain is on one line.
How could I iterate through the ...