Author Topic: คำสั่ง find,awk,sed,grep ที่อาจจะเป็นประโยชน์ ไว้ใช้ในการ track ปัญหา  (Read 60924 times)

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
พอดีจะ gen password ครับโดยใช้ชื่อuser ที่มีอยู่แล้ว ตัดมา สามตัวแล้วก็เพิ่มคำว่า xxXX@234 ลงไปตัวท้ายสุด

less list2.txt | cut -c 1-4 | sed 's/.$/xxXX@234/g' | more

ตัวสุดท้ายของ line ใส่เป็น .$ แทนที่ด้วย xxXX@234

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
การ grep ค่ามากกว่า 1 ค่าขึ้นไปใน file string

เช่น
#less string

dn: mail=i@thailand.com,vd=thailand.com,o=hosting
structuralObjectClass: organization
entryUUID: c4311ae6-2fa2-102d-86e7-357b3082acba

เราต้องการบันทัดที่เป็น dn: mail กับ enrtyUUID สามารถใช้ grep ช่วยได้ดังนี้

#less string | grep -e "dn: mail" -e entryUUID | more

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
การแทนที่ ใน bash(linux) กับ sh(BSD)
ปกติเราจะคุ้นเคยกับ sed แต่ว่าวันนี้ จะแสดงการใช้งาน tr(translate character) ให้ดูนะครับ

สมมติว่าเราต้องการแทนค่า ด้วยอะไรซักอย่าง เป็นอีกค่า1 ทำได้ดังนี้

เช่น textfile มี

#less textfile

rtgfvbhyujnuj
rtgfvbhyujnuj
rtgfvbhyujnuj

เราต้องการแทนที่ ตัว "uj" ด้วยค่า บันทัดว่าง ในรูปแบบของ sed
#less textfile | sed 's/uj/\n/g'

หรือจะเป็นรูปแบบของ tr
#less textfile | tr 'uj' '\n'


golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
ทำการ grep แบบ pattern สองแบบในการค้นหา (bash sh)

#grep -E 'pattern1' filename | grep -E 'pattern2' filename

แล้วถ้าต้องการเอาค่า ที่อยู่เหนือบันทัด "aaa" ที่ grep มาแสดง ก็ (B1) หมายถึง before 1 line

#grep -B1 "aaa" filename

แล้วถ้าต้องการเอาค่า ที่อยู่หลังบันทัด "aaa" ที่ grep มาแสดง ก็ (A1) หมายถึง after 1 line

#grep -A1 "aaa" filename

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
$ grep  -v "^#\|^'\|^\/\/" comments
This file shows the comment character in various programming/scripting languages
If the Line starts with single hash symbol,
then its a comment in Perl and shell scripting.
The line should start with a single quote to comment in VB scripting.
Double slashes in the beginning of the line for single line comment in C.

Example 2. Character class expression

As we have seen in our previous regex article example 9, list of characters can be mentioned with in the square brackets to match only one out of several characters. Grep command supports some special character classes that denote certain common ranges. Few of them are listed here. Refer man page of grep to know various character class expressions.

[:digit:]    Only the digits 0 to 9
[:alnum:]    Any alphanumeric character 0 to 9 OR A to Z or a to z.
[:alpha:]    Any alpha character A to Z or a to z.
[:blank:]    Space and TAB characters only.

These are always used inside square brackets in the form [[:digit:]]. Now let us grep all the process Ids of ntpd daemon process using appropriate character class expression.

$ grep -e "ntpd\[[[:digit:]]\+\]" /var/log/messages.4
Oct 28 11:42:20 gstuff1 ntpd[2241]: synchronized to LOCAL(0), stratum 10
Oct 28 11:42:20 gstuff1 ntpd[2241]: synchronized to 15.11.13.123, stratum 3
Oct 28 12:33:31 gstuff1 ntpd[2241]: synchronized to LOCAL(0), stratum 10
Oct 28 12:50:46 gstuff1 ntpd[2241]: synchronized to 15.11.13.123, stratum 3
Oct 29 07:55:29 gstuff1 ntpd[2241]: time reset -0.180737 s

Example 3. M to N occurences ({m,n})

A regular expression followed by {m,n} indicates that the preceding item is matched at least m times, but not more than n times. The values of m and n must be non-negative and smaller than 255.

The following example prints the line if its in the range of 0 to 99999.

$ cat  number
12
12345
123456
19816282

$ grep  "^[0-9]\{1,5\}$" number
12
12345

The file called “number” has the list of numbers, the above grep command matches only the number which 1 (minimum is 0) to 5 digits (maximum 99999).

Note: For basic grep command examples, read 15 Practical Grep Command Examples.
Example 4. Exact M occurence ({m})

A Regular expression followed by {m} matches exactly m occurences of the preceding expression. The following grep command will display only the number which has 5 digits.

$ grep  "^[0-9]\{5\}$" number
12345

Example 5. M or more occurences ({m,})

A Regular expression followed by {m,} matches m or more occurences of the preceding expression. The following grep command will display the number which has 5 or more digits.

$ grep "[0-9]\{5,\}" number
12345
123456
19816282

Note: Did you know that you can use bzgrep command to search for a string or a pattern (regular expression) on bzip2 compressed files.
Example 6. Word boundary (\b)

\b is to match for a word boundary. \b matches any character(s) at the beginning (\bxx) and/or end (xx\b) of a word, thus \bthe\b will find the but not thet, but \bthe will find they.

# grep -i "\bthe\b" comments
This file shows the comment character in various programming/scripting languages
If the Line starts with single hash symbol,
The line should start with a single quote to comment in VB scripting.
Double slashes in the beginning of the line for single line comment in C.

Example 7. Back references (\n)

Grouping the expressions for further use is available in grep through back-references. For ex, \([0-9]\)\1 matches two digit number in which both the digits are same number like 11,22,33 etc.,

# grep -e '^\(abc\)\1$'
abc
abcabc
abcabc

In the above grep command, it accepts the input the STDIN. when it reads the input “abc” it didnt match, The line “abcabc” matches with the given expression so it prints. If you want to use Extended regular expression its always preferred to use egrep command. grep with -e option also works like egrep, but you have to escape the special characters like paranthesis.

Note: You can also use zgrep command to to search inside a compressed gz file.
Example 8. Match the pattern “Object Oriented”

So far we have seen different tips in grep command, Now using those tips, let us match “object oriented” in various formats.

$ grep "OO\|\([oO]bject\( \|\-\)[oO]riented\)"

The above grep command matches the “OO”, “object oriented”, “Object-oriented” and etc.,
Example 9. Print the line “vowel singlecharacter samevowel”

The following grep command print all lines containing a vowel (a, e, i, o, or u) followed by a single character followed by the same vowel again. Thus, it will find eve or adam but not vera.

$ cat input
evening
adam
vera

$ grep "\([aeiou]\).\1" input
evening
adam

Example 10. Valid IP address

The following grep command matches only valid IP address.

$ cat input
15.12.141.121
255.255.255
255.255.255.255
256.125.124.124

$ egrep  '\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)' input
15.12.141.121
255.255.255.255

In the regular expression given above, there are different conditions. These conditioned matches should occur three times and one more class is mentioned separately.

    If it starts with 25, next number should be 0 to 5 (250 to 255)
    If it starts with 2, next number could be 0-4 followed by 0-9 (200 to 249)
    zero occurence of 0 or 1, 0-9, then zero occurence of any number between 0-9 (0 to 199)
    Then dot character

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
0000549C08B: Nov 19 , 17:11:59 size=79482, from=<adelynong@nocbcc>,to=<steve.wong@nocbcc>, to=<peggyheng@nocbcc>,
0002749C061: Nov 19 , 00:54:39 size=2330, from=<prvs=966952adee=kmn-zabicom@nocbcc>,to=<ashir@nocbcc>,
0009149C05F: Nov 18 , 10:37:23 size=3611, from=<trouble-bounces@xxx>,to=<trouble@xxx>,

ถ้าต้องการที่จะ sort แล้วเอา เรียงตัวแปร column ที่ 3 และ 5

#less newlog | sort | uniq | sort -k3 -k5 | more
0009149C05F: Nov 18 , 10:37:23 size=3611, from=<trouble-bounces@xxx>,to=<trouble@xxx>,
0002749C061: Nov 19 , 00:54:39 size=2330, from=<prvs=966952adee=kmn-zabicom@nocbcc>,to=<ashir@nocbcc>,
0000549C08B: Nov 19 , 17:11:59 size=79482, from=<adelynong@nocbcc>,to=<steve.wong@nocbcc>, to=<peggyheng@nocbcc>,
« Last Edit: พฤศจิกายน 22, 2012, 10:34:44 PM by golfreeze »

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
1.ถ้าต้องการ ลบ คำว่า sex_delete ที่มีเขียนอยู่ในไฟล์ ซึ่งเราไม่รู้ว่า มีทั้งหมดกี่ไฟล์ ใน folder "/home/www" จัดได้ดังนี้

#grep -lir 'sex_delete' /home/www/ | xargs rm -fv
หรือ
#find /home/www -type f -exec grep -l 'sex_delete' {} \; | xargs rm -fv


2.ถ้าต้องการลบ ข้อความ ในเมลที่ใช้งาน exim
# grep -rl 'sex_delete' /var/spool/exim/input/ | sed -e 's/^.*\/\([a-zA-Z0-9-]*\)-[DH]$/\1/g' | xargs exim -Mrm

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
ใช้จริงๆ ไม่เยอะหรอกครับ อยากได้อะไรก็ man กับ google เอา

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
##Check size more than 10M
less /var/log/mail.log | grep "size="  | awk -F 'size=' '{print $1 " " $2}' | sed 's/,//g' | awk '{ if ($8>10000000) print } '| more

##Check size mail between 7.? K and delete in 1 command
ls -lah | grep "7\.[0-9]K" | grep "142"  | awk '{print "rm -f " $9}' | more

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
มีโจทย์ว่าต้องการหา สคิป php ที่โดนเรียกในเว็บ packetlove.com ถ้ามาจาก IP เดียวกันเกิน 10 ครั้งขึ้นไปให้แสดง ชื่อ สคิปพร้อมทั้ง IP ที่เรียกมาให้ดู​
#less /var/log/packetlove.com.log | grep '.php HTTP/1.1" 200' | grep -v wp-login.php | grep -v admin-ajax.php | awk '{print $1" "$7}' | uniq -c | sort -nr | awk '{if ($1>10) print }' | more

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
มีโจทย์อยู่ว่า อยากรู้ว่า มีการใช้งาน CPU ไปกี่ % ณ ปัจจุบันขณะ
ใน Linux เราสามารถประยุกต์หาค่า การใช้งาน จากค่า idle CPU ได้

#top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1}'

ถ้าค่า idle = 45% แสดงว่ามีการใช้งานไป 100 - 45 เหลือ 55% ครับ
ลองเอาใช้งานกันดูนะครับ

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
ต้องการค้นหา คำว่า packetloveold ในหลายๆไฟล์
หลังจากค้นเจอ ให้ทำการ เปลี่ยน text ของคำว่า packetloveold เป็น newpacketlovenew

$grep -rl ‘packetloveold’ ./ | xargs sed -i ‘s/packetloveold/newpacketlovenew/g’

ก็ลองดูนะครับผม

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
หา ช่องว่าง ในไฟล์โดนใช้ grep

Make sure you quote your expression. I think shell wordsplitting is one of your problems here, but you should always quote your regex, as all kinds of things will go wrong otherwise.

$ grep ' \.pdf' example
grep .pdf
Or if there might be multiple spaces (we can't use * as this will match the cases where there are no preceding spaces)

grep ' \+\.pdf' example
+ means "one or more of the preceding character". In BRE you need to escape it with \ to get this special function, but you can use ERE instead to avoid this

grep -E ' +\.pdf' example
You can also use \s in grep to mean a space

grep '\s\+\.pdf' example