m1gin 329

#ubuntu #terminal #findreplace #sed #grep

sed

Find - replace and save file as:

  • sed 's/Ekim 2014/Eylül 2014/' 1.svg >2.svg

/ yerine | veya istenilen tek byte bir karakter ile kullanım da mümkündür:

  • sed "s|Ekim 2014|Kasım 2014|g" 1.svg

  • sed "s/^Ekim 2014</^Kasım 2014</^g" 1.svg

Save on original file (-i: in-place)

  • sed -i 's/original/new/g' file.txt

s = the substitute command
original = a regular expression describing the word to replace (or just the word itself)
new = the text to replace it with
g = global (i.e. replace all and not just the first occurrence)

  • mb1="Ocak 2014"

  • echo $mb1 | sed "s/2014/2024/"

Sonuç: Ocak 2024


Sonucu yeni bir değişkene atamak için şu yöntem kullanılabilir:

  • mb2=$( $mb1 | sed "s/2014/2024/" )

Ayrıca tilda ` ile de sonuç almak mümkün:

  • mb2=`$mb1 | sed "s/2014/2024/"`

Searching & Replacing in specific line (ie: line 1):

sed -i "1 s|#ExternalChecksum|'#ExternalChecksum|" test.vb


Multiple find/replace at once:

  • echo 'abcdef' | sed 's|ab|01|g;s|ef|23|g'
    result: 01cd23
  • echo "abcdef" | sed 's|cd|-XYZT-|g;s|YZ|03|g'
    output: ab-X03T-ef
  • sed -r 's|\t"|\t|g;s|"$||g' test.txt


Print only specified line from a file:

  • sed -n '5 p' file.txt
    output: this is line 5

Delete specific lines from a file

  • sed -e "4d" file.txt
  • sed -e "2,4d" file.txt

Replace letter by letter

echo aşçı | sed "y/ışç/isc/"

output: asci

Uygulama: SVG dosyasındaki bir text kutusunun içeriğini değiştirip yeni bir resim oluşturmak ve PNG olarak çıktı almak.

  • sed "s|Ekim 2014</|Kasım 2014</|" 1.svg

  • sed "s/Ekim 2014<\//Kasım 2014<\//" 1.svg >2.svg


Yukarıdaki yöntem sonuç veriyor. Ancak bu değere sahip başka elemanlar da etkilenirler. Çoklu değişiklikleri minimize etme adına ilgili düğüm (node) kullanılsın:

  • nd_old=$( xmllint --xpath '//*[contains(@id,"txtdate")]' 1.svg )


Bulunan elemanın istenen değeri değiştirilip yeni değişkene atansın:

  • nd_new=`echo $nod_old | sed "s/Ekim 2014/Kasım 2014/"`


Ve son olarak eski değişken değeri yenisiyle yer değiştirsin ve sonuç yeni bir dosyaya yazılsın.

  • sed "s|$nod_old|$nod_new|" 1.svg >2.svg


Not: Değişkenler içerisinde, sed için / gibi özel olan karakterler olduğunda hata oluşuyor.

Ve son olarak inkscape kullanılarak, SVG dosyasından PNG dosyası üretilir.

  • inkscape --export-area-drawing --export-png=2.png --export-width=600 2.svg

Sadece genişlik belirtildiğinde, yükseklik otomatik olarak ayarlanıyor.


grep

Search text in files...

Ignore case and search for "play" text in .as?x and .js extensions.

  • grep -i "play" --include="*.as?x*" --include="*.js" ./

Search files recursively… Also ignore case..

  • grep -ir "play" --include="*.as?x*" --include="*.js" ./

  • grep -ir "play" --include=\*.{asax,ascx,vb.js} ./

Match only words:

  • grep -rw "play" --include="*.as?x*" --include="*.js" ./

Show matched count for each file:

  • grep -irc "play" --include="*.as?x*" --include="*.js" ./

Show total matches count

  • grep -ir "SpecialMessage" --include="*.as?x*" --include="*.js" ./ | grep -ic "SpecialMessage"

Show only matched files

  • grep -irl "SpecialMessage" --include="*.as?x*" --include="*.js" ./



Examples…

  • grep -ir "play" --include=*.as?x --exclude=*.aspx ./

  • grep -ir "play" --include=*.as{a,c,p}x ./


Show some lines before (-B) or after (-A) the matched line:

  • cat test.txt | grep -B 2 "satır dört"'

-B, --before-context=NUM print NUM lines of leading context
-A, --after-context=NUM print NUM lines of trailing context


List lines contains Control or VB and not contains AutoEventWireup

  • grep -inrP '(control|vb") (?!AutoEventWireup)' --include=*.{aspx,ascx} --exclude=*.{exclude,js} ./


extract url from content:

  • grep -inr "https://.*.png" play/test/play.htm | sed -r "s|.*(http.*.png).*|wget \1|"

awk

split text and capture groups:

echo group1x-xgroup2x-xgroup3 | awk -F'x-x' '{print $2}'

remove first 2 words of each line from a text file:

awk '{$2= ""; $1= ""; print $0}' captcha.txt

multiline with custom field and record splitter:

awk 'BEGIN{RS="\n\n";FS="\n";} {print $1,$2}' file.txt

split text by custom separators:

echo record 1 AAAA record 2 BBBB | awk 'BEGIN { RS = "\n|( *[[:upper:]]+ *)" } { print "Result =", $0,"and RT = [" RT "]" }'

read csv/tsv file:

awk -F ',|\t' 'NR<11{print $1 " : " $2 " : " $3}' data.csv

read tab-seperated values from file:

awk -F '\t' 'NR==11{print $1 " : " $2 " : " $3}' data.tsv

pass variable to awk:

ln=2; awk -v no="$ln" -F '\t' 'NR==no{print $1 " : " $3}' data.tsv

pass shell variable to awk:

l=4; c=2; awk -F '\t' 'NR=='$l'{print $1 " :: " $'$c'}' data.tsv

filter lines by exluding word:

awk '!/unwanted/ && (/searchfor/ || /alsolookatthis/)' file.txt

split and replace:

echo "'abc','n';'xyz'" | awk -F",|;" '{gsub(/'\''/,"",$0); print "id="$1"&mode="$2"&hash="$3}

split line with tab or more than 2 spaces:

cat file.txt | awk -F"\t|( ){2,}" '/DOKTOR\s/ {print $1 " \t " $3}'



Find in files using grep and replace in files using sed

  • grep -lir "cdn.mbirgin.com/files/mbirgin.com/js" --include="*.as?x*" --include=*.{vb,cs,js,master} --exclude=*.{txt,exclude} ./ | while read f; do echo $f; sed -i 's|http://cdn.mbirgin.com/files/mbirgin.com/js/|https://mbirgin.com/core/js/|g' "$f" ; done;



Add to: