User Tools

Site Tools


grep

If you want to send us your comments, please do so. Thanks
More on comments


grep

Alternatives

NameDescriptionRemark
agrepApproximate matching (e.g., finding misspelled words), boolean queries, limited forms of regular expressionsPart of glimpse. Virtual package
glimpseGLobal IMPlicit SEarch. Fast indexing and query system. Supports most of agrep's options
onlygrep-like tool for filtering on words or lines

Exclude multiple strings

-v : exclude
-e : or function
agrep : and function (can do)

somecommand | grep -e "sda" -e "sdb" -e "sdc" # Only lines containing sda, sdb or sdc
somecommand | grep -v -e "sda" -e "sdb" -e "sdc" # All lines except those with sda, sdb or sdc
somecommand | grep -v -e sda -e sdb -e sdc
somecommand | grep -v -e "hello" -e "something" -e "aword"
grep -v -e "hello" -e "something" -e "aword" filetosearchin.txt
grep -Ev 'hello.hello|some thing' filetosearchin.txt
grep -v 'hello.hello\|some thing' filetosearchin.txt
fdisk -l | grep Disk | grep -v -e label -e model -e identifier # Show harddisk information

Hidden files

grep " \."

How to grep on a hex string

This can be used to show where all symbolic links in a directory listing point to

ls -l | grep -UaP "\x2D\x3E"

To show only the links use in a directory with the ls command, and make an alias of it

alias lsl='ls -l | grep -UaP "\x2D\x3E" | tr -s " " | cut -d " " -f 9,10,11'

Multiple items

grep 'Item1\|Item2' *

Square brackets

The -e option is needed

grep -i -r -l -e '\[\[sometextstring' /home/user

Regular expressions

Find the strings, if they exist

sometekst.1.txt
sometekst.2.txt
sometekst.3.txt

in the file

atekstfile.txt

with

grep sometekst.\[1,2,3\].txt sometekstfile.txt

Other example:

grep string1 * | grep string2 | grep [aa,bb] | sort -k 2 --field-separator=$ --uniq | less -S

AND

Find pattern-1 AND pattern-2 in a file

grep -P '^(?=.*pattern-1)(?=.*pattern-2)' *

agrep can also do an AND function
tre-agrep is an improved version of agrep

agrep 'pattern-1;pattern-2' *

Find whole word

i=Hello; grep -E "^$i$" somefile.txt

Star

Use single quotes and a backslash

grep -rl '\*2\*'

In file with a certain file extension

grep -irl searchword *.txt

does not work. It yields

grep: *.txt: No such file or directory

This can be solved by using find. See Find. Example:
find . -name “*.txt*” -exec grep -l “textstring” '{}' \;

Last charaters of each line

 grep -o ....$

The output is the 4 last characters of the input line

Other examples

lsof | grep "[[:digit:]]\+w"

Removing

Remove blank lines which do not contain spaces

grep . file.txt
grep -Ev "^$" file.txt

Remove blank lines even if they include spaces

grep "\S" file.txt

agrep

Boolean operations:
Supports an `and' operation `;' and an `or' operation `,', but not a combination of both. For example, 'fast;network' searches for all records containing both words.

Text search tool with support for approximate patterns, a version of standard grep with the following enhancements:

  • the ability to search for approximate patterns
  • it is record oriented rather than just line oriented
  • multiple patterns with AND OR logic queries

agrep contains glimpse's (4.x) last free version of grep.

glimpse

search quickly through a large set of files, the file system, very quickly. Supports most of agrep's options

zgrep

Search in compressed or gzipped files


Main subjects on this wiki: Linux, Debian, HTML, Microcontrollers, Privacy

RSS
Disclaimer
Privacy statement
Bugs statement
Cookies
Copyright © : 2014 - 2024 Webevaluation.nl and the authors
Changes reserved.

This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies
grep.txt · Last modified: 28-02-2024 10:28 by wim