#ubuntu #terminal #findreplace #sed #grep
sed
Find - replace and save file as:
/ yerine | veya istenilen tek byte bir karakter ile kullanım da mümkündür:
Save on original file (-i: in-place)
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)
Sonuç: Ocak 2024
Sonucu yeni bir değişkene atamak için şu yöntem kullanılabilir:
Ayrıca tilda ` ile de sonuç almak mümkün:
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.
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:
Bulunan elemanın istenen değeri değiştirilip yeni değişkene atansın:
Ve son olarak eski değişken değeri yenisiyle yer değiştirsin ve sonuç yeni bir dosyaya yazılsın.
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.
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.
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:
Show matched count for each file:
Show total matches count
Show only matched files
Examples…
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
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;