grep etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
grep etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

4 Kasım 2021 Perşembe

grep General Output Control - Dosya İsimleri veya Çıktıyı Kontrol Eder

Giriş
Çıktıyı kontrol etmek içindir.
--color
Açıklaması şöyle.
Surround the matched (non-empty) strings, matching lines, context lines, file names, line numbers, byte offsets, and separators (for fields and groups of context lines) with escape sequences to display them in color on the terminal.  ...  WHEN is neveralways or auto.
Varsayılan değer auto olduğu için şöyle yaparız.
grep --color ...
-L,--files-without-match
Açıklaması şöyle. Eşleşmeyen dosyaları verir
-L, --files-without-match

Suppress normal output; instead print the name of each input file from which no output would normally have been printed. The scanning will stop on the first match.

-l, --files-with-matches

Suppress normal output; instead print the name of each input file from which output would normally have been printed. The scanning will stop on the first match.
Örnek
Şöyle yaparız. Önce 'cat' kelimesini içeren dosyaların isimlerini alırız. Daha sonra bu dosyalarda 'dog' kelimesi içermeyenleri buluruz.
grep -L 'dog' $(grep -l 'cat' <list of files>)
-l,--files-with-matches
Açıklaması şöyle
grep -l lists the names of files with matching strings
Örnek
Şöyle yaparız. Bulunduğum dizin ve altındaki her şeyi tarar içinde abc geçen dosyaları listeler
grep -lRe abc .
-q, --quite, --silent
quite anlamına gelir. stdout'a çıktı yazmaz. If koşullarında kullanılır. Şöyle yaparız
if grep -q 'release 7\.[56] ' /etc/redhat-release
then ...
-o
Genellikle düzenli ifadeler ile kullanılır. Sadece eşleşen kısım gösterilir. Açıklaması şöyle
-o, --only-matching
Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.
Burada non-empty kısmı önemli çünkü bazen düzenli ifadeler ile boş string çıktısı gelebiliyor. Açıklaması şöyle
Many regular expressions can match empty text; for example:$ grep -E '.?' <<<""

outputs a blank line because the empty line matches. However, adding -o results in no output at all:$ grep -Eo '.?' <<<""

because -o ignores empty matches.

Specifying that -o ignores empty matches means the implementers don’t need to decide what to do with zero-length matches, which would otherwise produce rather lengthy (and useless) output in many circumstances.
Örnek
Şöyle yaparız.
$ str='25 results [22 valid, 2 invalid, 1 undefined]'
grep -E -o '[0-9]+' <<<"$str"

2 Haziran 2020 Salı

grep komutu

Giriş
grep komutundaki seçenekler
- Matcher Selection
- Matching Control
- General Output Control
- Output Line Prefix Control
- Context Line Control
- File and Directory Selection
- Other Options

başlıkları altına toplanmış.

Exit Status
Örnek
Şöyle yaparız. Dosya yoksa  2 değeri döner
grep "i am here" real-file

# Returns: 0 (via: echo $?)

grep "i am not here" real-file

# Returns: 1

grep "i am not here" not-a-file

# Returns: 2 (No such file or directory)
General Output Control
General Output Control yazısına taşıdım

Matching Control
Matching Control yazısına taşıdım

Matcher Selection
-E seçeneği
Extended regex kullan. Şöyle yaparız.
$ str='25 results [22 valid, 2 invalid, 1 undefined]'
grep -E -o '[0-9]+' <<<"$str"
Other Options
--line-buffered seçeneği
Normalde grep kendi içindeki buffer dolduktan sonra çıktı verir. Bunu satır seviyesine düşürmek için şöyle yaparız.
( echo "LINE 1" ; sleep 1 ; echo "LINE 2" ; ) | grep --line-buffered LINE | cat
-U
Treat the file as binary
Örnek
Şöyle yaparız
echo "[INFO] Checking if ddl sources have UPPERCASE file names..."
/bin/ls -1 -I *svn* src/main/resources/ddl | grep -U -e "[a-z]"
if [ $? -eq 0 ]; then
  echo "[ERROR] The above files have lowecase letters in their names!"
  exit 1
else
  echo "[INFO] ...done."
fi
-z, --null-data
Açıklaması şöyle
  -z, --null-data
          Treat  input  and  output  data  as  sequences  of  lines,  each
          terminated by a zero byte (the ASCII NUL character) instead of a
          newline. 
Açıklaması şöyle. Dolayısıyla normal kullanımda linefeed karakterini grep'lemek mümkün değil
Since grep is (by default) line-based, the linefeed characters are stripped out before pattern matching takes place.
Örnek
Elimizde şöyle iki dosya olsun
$ cat file1
this is a test:
test1

test2
and another one

$ cat file2
this is a test:

test1

test2

and another one
Şöyle yaparız. Burada sanki tüm dosya tek satır gibi düşünülebilir. İlk dosya eşleşmiyor çünkü test2'den sonra 2 tane \n karakteri yok.
$ grep -Pz '\n\ntest1\n\ntest2\n\n' file1
$ 

$ grep -Pz '\n\ntest1\n\ntest2\n\n' file2
this is a test:

test1

test2

and another one
File and Directory Selection
-a seçeneği
Açıklaması şöyle
-a, --text
Process  a  binary  file  as  if  it  were  text;  this  is  equivalent to the --binary-files=text option.

Output Line Prefix Control

-A seçeçeneği
After anlamına gelir. Eşleşen satırdan sonraki satırları da gösterir.
Örnek
Şöyle yaparız. -m1 ile ilk eşleme seçilir. Bu eşleşmeden sonra gelen 10 satırı görmek için -A10 kullanılır
grep -m1 -A10 PATTERN filename
-B seçeçeneği
Before anlamına gelir. Eşleşen satırdan önceki satırları da gösterir.
Örnek
Eşleşen satırdan önceki 5'inci satırı da çıktıda görmek için şöyle yaparız.
grep -r -i -B 5 -A 5 "match" 
Örnek
Elimizde şöyle bir veri olsun.
cat db_systems.txt

  "db-unique-name": "p00z5bj_iad2bj",
  "db-workload": "OLTP",
  "defined-tags": {},
  "freeform-tags": {},
  "id": "dfadfasfsadfasdfasdf",
  "lifecycle-details": null,
  "lifecycle-state": "AVAILABLE",
  ...
Burada lifecycle-state alanı AVAILABLE olan ve db-unique-name aradığımı değer olan kayıtları bulmak isteyelim. Şöyle yaparız. lifecycle-state alanına göre arama yapar ve çıktıya 6 satır öncesini de dahil ederiz. Daha sonra bu çıktı üzerinde bir daha grep yaparız.
grep -B6 AVAILABLE file | grep db-unique-name
 "db-unique-name": "p00z5bj_iad2bj",
Diğer
grep çıktısının renkleri GREP_COLORS ortam değişkeni ile atanır. Renkler şöyledir
Red:  ms=01;31
Green:  ms=01;32
Yellow:  ms=01;33
Blue:  ms=01;34
Çıktıyı kırmızı yapmak için şöyle yaparız.
export GREP_COLORS='ms=01;31'
Sarı için şöyle yaparız.
export GREP_COLORS='ms=01;33:mc=01;31:sl=:cx=:fn=35:ln=32:bn=32:se=36'

29 Mayıs 2020 Cuma

grep Matching Control - Eşleşmenin Nasıl Olacağını Kontrol Eder

Giriş
Bu seçenekler eşleşmenin nasıl olacağını kontrol eder.

-e seçeneği
Birden fazla örüntü belirtilebilir.

Örnek
Şöyle yaparız.
root@grep -e tire_id -e appID /path/to/*/vehicle/production.json
/path/to/000001_000002/vehicle/production.json:    "tire_id": "1305436516186552",
/path/to/000001_000002/vehicle/production.json:        "appID": "1164562920689523",
/path/to/000001_000079/vehicle/production.json:    "tire_id": "1815123428733289",
/path/to/000001_000079/vehicle/production.json:        "appID": "18412365908966538",
/path/to/000001_000088/vehicle/production.json:    "tire_id": "138477888324",
-f FILE seçeneği
Her satırda aranacak pattern olan dosya belirtilir.

Örnek ver

-i, --ignore-case seçeneği
Örnek
Şöyle yaparız. Büyük küçük harf farkı gözetmeden tüm "sahil" kelimelerini bulur
[root@localhost playground]# cat file1.log 
hello 
i am sahil 
i am software engineer 
Sahil is a software engineer
sahil is a software engineer
 
[root@localhost playground]# grep -i 'sahil' file1.log 
i am sahil 
Sahil is a software engineer
sahil is a software engineer
[root@localhost playground]# 

-v seçeneği 
invert match anlamına gelir.  Aranan şeye uymayanları gösterir.
Örnek
Elimizde şöyle iki dosya olsun.
$ echo -e "a\nb\nc\nd" > list1
$ echo -e "c\nd\ne\nf" > list2
list1'de olup list2'de olmayanları görmek için şöyle yaparız. Burada komuta parametre olarak önce list2 geçiliyor.
$ grep -vxFf list2 list1
a
b

-w, --word-regexp seçeneği

Eşleşmeyi sadece kelimeler için yapar. Açıklaması şöyle
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.
Örnek
Şöyle yaparız. Burada -E ile bir regex verilmiş ancak sadece kelimelere uygulanıyor
$ echo '1341 5415 fdad' |  grep -wE "[1-9]{1,5}" -o
1341
5415
Yani aslında şununla aynı şey. regex'in başına ve sonuna \b getirilmesi ile gibi
echo '1341 5415 fdad' | grep -E -o '\b[1-9]{1,5}\b'
Örnek
Şöyle yaparız.
if grep -wq -- "$user" "$file1" && grep -wq -- "$user" "$file2" ; then
   echo "string avail in both files"
elif grep -wq -- "$user" "$file1" "$file2"; then
   echo "string avail in only one file"
fi

-x, --line-regexp seçeneği

Örnek ver