Linux kullanıcıları için kullanışlı bir komut bütünü, Satır İçindeki Belli Kelimeleri Silme için;
1 |
find / -maxdepth 1 -xdev -type f -exec grep -i "stringtofind" -l {} \; -exec sed -i '/./d' {} \; |
Linux kullanıcıları için kullanışlı bir komut bütünü, Satır İçindeki Belli Kelimeleri Silme için;
1 |
find / -maxdepth 1 -xdev -type f -exec grep -i "stringtofind" -l {} \; -exec sed -i '/./d' {} \; |
Bir dosyada belirli bir patern geçen satırı silmenin *nix sistemlerde bir çok yöntem ve araç kullanabilirsiniz.Bunlardan bazılarını aşağıdaki gibidir.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
[root@omeroner ~]$cat sed_data omer oner selim eda ceren mihriban erol kemal deniz kerem mahmut [root@omeroner ~]$sed -i '/omer/d' sed_data [root@omeroner ~]$cat sed_data ceren mihriban erol kemal deniz kerem mahmut [root@unitednations ~]$sed -i '/omer/Id' sed_data [root@omeroner ~]$cat sed_data ceren mihriban erol kemal deniz kerem mahmut [root@unitednations ~]$ |
1 2 3 4 5 6 7 8 9 |
[root@omeroner ~]$cat dosyaadi ceren mihriban erol kemal deniz kerem mahmut omer oner selim eda [root@unitednations ~]$grep -v "omer" dosyaadi > dosyaadi2; mv dosyaadi2 dosyaadi mv: overwrite `sed_data'? y [root@unitednations ~]$cat dosyaadi ceren mihriban erol kemal deniz kerem mahmut |
Dosyamızın satırlarında birden fazla boşluğu tek boşluk ile değiştirmek istersek aşağıdaki kalıbı kullanabiliriz.
1 |
sed 's/ */ /g' DOSYA |
1 2 3 4 5 6 7 8 |
[root@omeroner ~]$cat sed_data omer oner selim eda ceren mihriban erol kemal deniz kerem mahmut [root@omeroner ~]$sed 's/ */ /g' sed_data omer oner selim eda ceren mihriban erol kemal deniz kerem mahmut |
1 2 3 4 5 |
[root@omeroner ~]$sed 's/ */ /g' sed_data o m e r o n e r s e l i m e d a c e r e n m i h r i b a n e r o l k e m a l d e n i z k e r e m m a h m u t [root@unitednations ~]$ |
Cümle içerisinde geçen her kelimenin ilk harflerini büyük yapmak istendiğinde , *nix ortamında farklı yöntem/araçlar ile bu işlem yapılabilir.
1 2 3 |
[root@omeroner~]$cumle="hayata karşı serin duruş..." [root@omeroner~]$echo $cumle | sed 's/\([a-z]\)\([a-zA-Z0-9]*\)/\u\1\2/g' Hayata Karşı Serin Duruş... |
1 2 3 |
[root@unitednations ~]$cumle="hayata karşı serin duruş..." [root@unitednations ~]$echo $cumle | awk '{ for ( i=1; i <= NF; i++) { sub(".", substr(toupper($i),1,1) , $i) } print }' Hayata Karşı Serin Duruş... |
Bir dosyada bulunan son satırları silmek isteyebiliriz. Bunun için aşağıdaki kalıbı kullanabiliriz.
1 |
sed -r 's/.{1}$//' |
1 2 3 4 5 6 7 8 9 10 11 |
[root@unitednations ~]$cat sed_data omer oner 2000 3 kemal ozlu 2001 5 can urasli 300 4 omer oner 4000 5 [root@omeroner ~]$sed -r 's/.{1}$//' sed_data omer oner 2000 kemal ozlu 2001 can urasli 300 omer oner 4000 [root@omeroner ~]$ |
Herhangi bir dosyada belli bir patern’in geçtiği satırın altına yeni bir satır eklemek istediğinizde sed imdadınıza yetişir. Bunun için aşağıdaki kalıbı kullanabilirsiniz. Sed ile Belli Patern’in Altina Yeni Satır Ekleme için;
1 |
sed '/PATERN/a yeni_satır' DOSYA_ADI |
Örneğin aşağıdaki örnekte sed_data isimli dosyasındaki omer patern’i geçen satırının altına kadir oner 300 1 satırını ekliyoruz.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
[root@omeroner~]$cat sed_data omer oner 2000 3 kemal ozlu 2001 5 can urasli 300 4 omer oner 4000 5 [root@omeroner~]$sed '/omer/a kadir oner 300 1' sed_data omer oner 2000 3 kadir oner 300 1 kemal ozlu 2001 5 can urasli 300 4 omer oner 4000 5 kadir oner 300 1 [root@omeroner~]$ |
Unix ve Türevlerinde Dosyadaki Sayıların Toplamını Hesaplatma için birden fazla yöntem mevcut.
1 2 3 4 5 6 7 8 |
[root@omeroner /tmp]$cat /tmp/file.txt 286 255564800 609 146 671290 [root@omeroner /tmp]$ |
Yukarıda belirttilen dosyadaki tüm numaraların toplanması için;
1 2 |
[root@omeroner /tmp]$awk '{s+=$0} END {print s}' /tmp/file.txt 256237131 |
Awk Kullanarak Sum (Toplama İşlemi), Awk kullanarak ls komutunun çıktısının,dosya boyutların toplamını hesaplayabiliriz.
uzantısı .php olan tüm dosyaların listelenmesi ve MB cinsinden toplamını hesaplatmak için;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
[root@web/var/www/html/omeroner/deals]$ls -l *php;ls -l *php | awk '{ SUM += $5} END { print SUM/1024/1024 }' -rw-r--r-- 1 apache apache 25606 Mar 12 2013 blockDealCard.php -rw-r--r-- 1 apache apache 25593 Mar 12 2013 blockDealCardVacation.php -rw-r--r-- 1 apache apache 8777 Jun 20 2012 blockDealDetails.php -rw-r--r-- 1 apache apache 17240 Mar 25 2013 blockDeals_new.php -rw-r--r-- 1 apache apache 36112 Mar 22 2013 blockDeals.php -rw-r--r-- 1 apache apache 8598 Nov 23 2012 blockDealVacation2.php -rw-r--r-- 1 apache apache 9629 Mar 4 2013 blockDealVacation.php -rw-r--r-- 1 apache apache 23332 Mar 14 2013 blockLanding.php -rw-r--r-- 1 apache apache 9351 Mar 4 2013 blockNewDeals.php -rw-r--r-- 1 apache apache 2547 Jun 26 2012 blockOldDeal.php -rw-r--r-- 1 apache apache 2118 Mar 22 2013 blockOtherDeals.php -rw-r--r-- 1 apache apache 3161 Mar 12 2013 blockRetargeting.php -rw-r--r-- 1 apache apache 1697 Feb 23 2012 bonus.php -rw-r--r-- 1 apache apache 12077 Nov 12 2013 brand.php -rw-r--r-- 1 apache apache 2001 Jun 28 2013 campaignDeal.php -rw-r--r-- 1 apache apache 1187 Jul 15 2013 cart.php -rw-r--r-- 1 apache apache 1248 May 8 2012 citys.php -rw-r--r-- 1 apache apache 1101 Feb 5 2013 dealarchive.php -rw-r--r-- 1 apache apache 25931 Nov 6 2013 deal.php -rw-r--r-- 1 apache apache 6077 Sep 5 2012 deals_new.php -rw-r--r-- 1 apache apache 10128 Nov 26 2013 deals.php -rw-r--r-- 1 apache apache 746 May 22 2012 dealsRedirect.php -rw-r--r-- 1 apache apache 4451 May 7 2013 image.php -rw-r--r-- 1 apache apache 1193 Jul 27 2012 keywords.php -rw-r--r-- 1 apache apache 7217 Oct 10 2013 list.php -rw-r--r-- 1 apache apache 2922 Jan 17 2012 memcache.php -rw-r--r-- 1 apache apache 24284 Mar 22 2013 product.php -rw-r--r-- 1 apache apache 16767 Nov 26 2013 products.php -rw-r--r-- 1 apache apache 26416 Jul 26 2013 searchDeal.php -rw-r--r-- 1 apache apache 2022 Aug 1 2012 searchDealResult.php -rw-r--r-- 1 apache apache 9769 Mar 8 2012 section.php -rw-r--r-- 1 apache apache 29640 Mar 8 2012 single.php -rw-r--r-- 1 apache apache 1693 Jul 24 2013 specialForm.php -rw-r--r-- 1 apache apache 1116 Sep 5 2012 support.php 0.344989 |
Awk ile basit string işlemlerine örnek olması için örnek bir awk.sh adında bir shell script hazırladım . awk_data isimli dosyamız verilerimizi içersin ve içeriği aşağıdaki gibi olsun.
1 2 3 4 5 6 |
[root@omeroner ~]$cat data_file kelime1="Mazhar" kelime2="Fuat" kelime3="Ozkan" kelime2="Siyah" kelime3="Beyaz" kelime1="Yol" kelime4="Su" kelime3="Elektrik" kelime4="Athos" kelime1="Porthos" kelime3="Aramis" kelime2="Dartanyan" kelime3="Metin" kelime1="Ali" kelime2="Feyyaz" |
Görüldüğü gibi
İstenen;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#!/bin/bash sed 's/\"//g' awk_data | awk ' BEGIN { FORMAT="%-15s%-15s%-15s%s\n" printf FORMAT,"[kelime1]","[kelime2]","[kelime3]","[kelime4]" } { if (match(tolower($0),"kelime1=[A-Za-z]*") > 0){ kelime1=(substr($0,RSTART+8,RLENGTH-8)) }else { kelime1="-" } if (match(tolower($0),"kelime2=[A-Za-z]*") > 0){ kelime2=(substr($0,RSTART+8,RLENGTH-8)) }else { kelime2="-" } if (match(tolower($0),"kelime3=[A-Za-z]*") > 0){ kelime3=(substr($0,RSTART+8,RLENGTH-8)) }else { kelime3="-" } if (match(tolower($0),"kelime4=[A-Za-z]*") > 0){ kelime4=(substr($0,RSTART+8,RLENGTH-8)) }else { kelime4="-" } printf FORMAT,kelime1,kelime2,kelime3,kelime4 }' |
output;
1 2 3 4 5 6 7 8 |
[root@omeroner ~]$bash awk.sh [kelime1] [kelime2] [kelime3] [kelime4] Mazhar Fuat Ozkan - - Siyah Beyaz - Yol - Elektrik Su Porthos Dartanyan Aramis Athos Ali Feyyaz Metin - [root@omeroner~]$ |