Tag Info

Hot answers tagged

22


14

By default, grep uses Basic Regular Expressions, you need to escape the braces to make grep match multiple characters: grep 't\{2\}' textfile Alternatively, you can use the -E option (or -P option for GNU grep, which uses Perl Compatible Regular Expressions) making grep use Extended Regular Expressions, which can use braces without escaping them: grep -E ...


8

The sed advantage of grep easy to see in such examples sed -n '/a/{/b/{/c/p;};}' file or: sed '/a/!d;/b/!d;/c/!d' file


8

Note the double space in bash's error message before "grep": that probably means you've typed an unbreakable space (AltGr+space), which can happen quite easily if your keyboard requires AltGr to produce the pipe symbol. Try dropping the spaces around the pipe symbol: ps aux|grep xscreensaver In your updated examples: [root@Hostname ~]# ps aux | grep ...


7

You need to escape the braces: grep 't\{2\}' textfile Otherwise { and } are treated as literal characters.


7

You need to use the -E (--extended-regexp) or -P (--perl-regexp) option (with GNU grep). According to the GNU grep(1) man page about Basic vs Extended Regular Expressions, i.e. if you choose not to use these options: In basic regular expressions the meta-characters ?, +, {, |, (, and ) lose their special meaning; instead use the backslashed versions \?, ...


6

awk '/a/ && /b/ && /c/' file Or with grep (which wouldn't scale well, though): grep -e 'a.*b.*c' -e 'a.*c.*b' -e 'b.*a.*c' -e 'b.*c.*a' -e 'c.*a.*b' -e 'c.*b.*a' file


6

Try find and grep instead, with -exec: find path_to_tex_files_directory -name "*.tex" -exec grep -- "->-" {} \; or with xargs find path_to_tex_files_directory -name "*.tex" | xargs grep -- "->-"


5

Let's compare all proposed solutions! I have a text file test.txt of size ~230M. I'm on Mac Mini, updated to 10.10. 1) awk solution by Hauke Laging (better not...): $ time bash -c "awk '/a/ && /b/ && /c/' >> /dev/null" 19.51 real 19.23 user 0.20 sys 2) "bruteforced" grep by Raghuraman R and Hauke Laging (better, but ...


5

Through grep which accept -P (Perl-regexp) parameter. $ grep -P '^(?=.*a)(?=.*b)(?=.*c)' file abc bca cab fhfhfhfabcjdfjdjfk ahfhfbkjfjdjffc Explanation: ^ Matches the start of a line (?=.*a) Only if the string going to be matched must contain a letter a (?=.*b) Must contain b (?=.*c) Must contain c


5

I would do this in perl instead: $ perl -ne 'print if /a/ && /b/ && /c/' file abc bca cab fhfhfhfabcjdfjdjfk ahfhfbkjfjdjffc If you just want to check whether each line matches all three letters (without printing the line itself), you could do: $ perl -lne '$k++ if /a/ && /b/ && /c/; END{$k==$. ? print "yes" : print ...


5

There is other way to make things faster: Use grep -f file1 file2 >output.txt. You could also use gnu parallel: http://www.gnu.org/software/parallel/parallel_tutorial.html


5

The problem is that -R tells grep to recursively search through all files in the directory. So, you can't combine it with a specific group of files. Therefore, you can either use find as suggested by @KM., or shell globbing: $ shopt -s globstar $ grep -- "->-" **/*.tex The shopt command activates bash's globstar feature: globstar ...


5

grep -v '^[0-9]' Will output all the lines that do not (-v) match lines beginning ^ with a number [0-9] For example $ cat test string string123 123string 1string2 $ grep -v '^[0-9]' test string string123 or if you want to remove all the words that begin with a digit sed 's/[[:<:]][[:digit:]][[:alnum:]_]*[[:>:]]//g' or with shortcuts and ...


4

The format of the file is probably little-endian UTF-16. Some apps on Windows seem to default to this, and it causes a lot of porability problems. vi represents ASCII-Nul (numerically zero) valued bytes as '^@' (control-At). You can actually enter zero-valued bytes in vim with the control-shift-@ chord. grep must see the ACII-Nul bytes, rather than ...


4

Yes, grep sw the ^@ characters. cat is printing the characters to the terminal, but they are characters you can't see. Just because you can't see the characters doesn't mean they aren't there. Your choice/preference, depending on which one works best for what you need. Remember, though, that vi has the possibility to change the file. ^@ is not a natural ...


4

From (GNU) grep(1) man page: -w, --word-regexp Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or preceded by a non-word constituent character. Similarly, it must be either at the end of the line or followed by a non-word constituent ...


3

If it is just a,b,c then we can use a mix of 'grep -o' and 'grep -e' option as below grep -e "a.*b.*c" -e "a.*c.*b" -e "b.*a.*c" -e "b.*c.*a" -e "c.*a.*b" -e "c.*b.*a" file You can also check already asked question at http://stackoverflow.com/questions/1546711/can-grep-show-only-words-that-match-search-pattern


3

grep -F -f needles.txt haystack.log is what you want. -F makes grep use simple pattern matching, if you do not need full regex. This tends to be a HUGE speedup. Depending on what you're searching for, you might not need regex, so might as well get the benefits of faster execution of simpler code. -f is a file of patterns you're looking for. So instead ...


3

printf 'aabbbccddd\nabcdef' | grep '\([a-z]\)\1\1' Output: aabbbccddd The bracket pair \(\) makes a backreference, which is referenced by \1


3

This should work: grep 'ing$' soi catis not needed. Try with single quotes. Single quotes prevent the shell from interpreting $ as the beginning of a variable name.


3

If your grep supports it1, you could use the --include switch: grep -R --include '*.tex' -- "->-" or grep -R --include='*.tex' -- "->-" 1: Available at least in GNU grep: --include=GLOB Search only files whose base name matches GLOB and OSX grep: --include If specified, only files matching the given filename pattern are ...


3

You should do: grep -v '2 *$' mysampledata.txt The -v option reverses the match. This supports lines with spaces at the end, since this is the case on your file and you do not seem to want Susy oranges 12 $ to be output (the $ here marks the end of line).


3

It means that grep should search for ABC string only at the beginning of the line OR after space, moreover this string has to end with another space OR the end of the line. In other words someone wanted to search for a strings which form whole words. However this regexp has many issues, namely there could be many other characters before and after word (at ...


2

You can specifiy multiple directories in grep: grep -r "string" app/assets/javascripts spec/javascripts Alternatively - sometimes more useful is list files to grep by find, and then grep them, for example find app/assets/javascripts spec/javascripts -type f -print0 | xargs -0 grep "string" or find app/assets/javascripts spec/javascripts -type f ...


2

> awk -v RS=$'\n\n' -v FS=$'\n' -v ORS=$'\n\n' '$2 ~ /123$/ {print}' abc.txt abc 123 abcd 123 abcde 12345


2

Use grep -B2 -P '^\t' logfile to pick up two lines before each set of matches. You may need to mask out the group separator "--" that is inserted between matches. Results $ grep -B2 -P '^\t' /tmp/t time=2 time=3 at com.test.com.... at com.test.com.... at com.test2.com.... -- time=8 time=9 at org.badstuff.com... at ...


2

Just use -After context, -Before context or -Context option in grep, e.g. to fit to your example: grep -B2 '^\t' file


2

grep -Hr "<keyword>" <directory> It recursively searches files under <directory> and prints matches with filenames.


2

Solution based on matching section headings To print the text starting with Cell 13 but stopping before Cell 14, use: sudo iwlist wlan0 scan | awk '/Cell 13/{f=1} /^ *Cell 14/{f=0} f' Solution based on monitoring the indent level This will print all lines starting with the one that contains Cell 13 and continuing with all the lines that follow that have ...



Only top voted, non community-wiki answers of a minimum length are eligible