การประมวลผลข้อความ
บทนำสั้น ๆ
การประมวลผลข้อความคืออะไร?
ในความเห็นอันต่ำต้อยของฉัน ในบล็อกที่แล้ว ฉันเริ่มเรียนรู้เกี่ยวกับ การประมวลผลข้อความ แล้ว ซึ่งประมวลผลเอาต์พุตที่อยู่ในรูปแบบ ข้อความ ไปยังเอาต์พุตที่เราต้องการ เห็นไหม (คุณรู้ว่าฉันหมายถึงอะไรใช่ไหม เหมือนว่าเราไม่ได้ใช้ข้อมูลเอาต์พุตทั้งหมด แต่เพียงบางส่วนเท่านั้น) และในบล็อกนี้ ฉันจะสำรวจเครื่องมืออื่นๆ ที่สามารถใช้สำหรับการประมวลผลข้อความ
ทำไมเราถึงเรียนรู้สิ่งนั้น?
สิ่งนี้จะช่วยให้การค้นหาข้อความหรือข้อมูลของเราง่ายขึ้นตามข้อมูลที่เราได้รับจากผลลัพธ์
คำสั่ง
ตัด
หากคุณเคยเขียนโค้ดด้วยภาษา Java หรือ C (ภาษาการเขียนโปรแกรม) คุณคงเคยได้ยินเกี่ยวกับ “Substring” มาก่อน หากคุณเคยรวบรวมข้อมูลใน Microsoft Excel คุณคงเคยได้ยินเกี่ยวกับ “Mid()” นะอันนี้ก็แบบนี้แหละ
เราสามารถใช้ได้ทั้งไฟล์และคำสั่ง
Column 1: CUT Command. Format cut [args] [file] cut -c1 filename //C is for character, so this will cut the text in a file, 1 char at each row cut -c1,2,4 filename //This one will cut the text in a file and take only char 1, 2, and 4 for each row cut -c1-3 filename //This one will cut the text in a file and take char from 1 to 3 for each row cut -c1-3,6-8 filename //Merge char from 1 to 3 and 6 to 8 in a file for each row cut -b1-3 filename //B is cutting by byte size, basically it is the same as using C cut -d: -f 6 filename //D is for delimiter, so everything comes after D will be a delimiter that acts a splitter. Before and after that symbol will be treated as "field". So you have to declare -f to choose which field that will be the output cut -d: -f 5-7 filename //It is the same as before but this time we choose from field 5 to 7 ls -ltr / | cut -d ' ' 2 filename //If the sample before cut text from file, this one is cutting from command
อค
ย่อมาจากชื่อที่เป็นผู้เขียนคำสั่งนี้ ใคร, ไวน์เบอร์เกอร์, เคอร์นิแกน. สิ่งนี้ซับซ้อนกว่า การตัด เล็กน้อย แต่มีความสามารถเฉพาะตัวในแง่ของการประมวลผลข้อความ นอกจากนี้ยังใช้ค้นหาข้อความ ขนาดของข้อความ และอื่นๆ ได้ด้วย
Column 2: AWK Command. Format awk [args] [file] awk '{print $1}' filename //It will print column 1 of the content of the file ls -l | awk '{print $1,$3}' //Will only print column 1 and 2 from ls command ls -l | awk '{print $NF}' //Will only print the last column from ls command awk '/Jerry/ {print}' filename //Will search a word 'Jerry' in the directory listed and print its row awk -F: '{print $1}' /etc/passwd //Output only 1st field of /etc/passwd, F is like a field with : as delimiter. echo "Hello Tom" | awk '{$2="Sayful"; print $0}' //Replace words field word and print out the row cat filename | awk '{$1="Sayful"; print $0}' //Replace words field word in a file and print out the row awk 'length($0)>15' filename //Will search the lines that have more than 15 bytes long ls -l | awk '{if($9 == "Sayful") print $0;}' //Will search matching "Sayful in the PWD ls -l | awk '{print NF}' //Will print the number of fields present in this command each row
GREP/EGREP
คุณรู้จัก Regex ไหม Nah Linux มีเครื่องมือสองอย่างที่เกี่ยวข้องกับ Regular Expression.
GREP ย่อมาจาก Global Regular Expression Print,
EGREP ย่อมาจาก Extensed GREP แข็งแกร่ง>.
Column 3: GREP and EGREP commands grep [keyword] [file] //Search a keyword from a file grep -c [keyword] [file] //Count total keywords present from a file grep -i [keYWoRd] [file] //Search a keYWoRd with ignoring uppercase & lowercase grep -n [keyword] [file] //Search a keyword from a file include which line it is grep -v [keyword] [file] //display everything but keyword grep [keyword] [file] | awk '{print $1}' //Search a keyword from a file but only return the first field [command] | grep -i [keyword] //Search keYWoRd from the output of the command with ignoring cases. egrep -i "keYWorD1|KeYwORd2" [file] //Search keYWorD1 or KeYwORd2 in a file
ดังนั้นหากคุณต้องการค้นหาคำหลักมากกว่าหนึ่งคำ คุณสามารถใช้ egrep ได้ นอกจากนี้คุณยังสามารถใส่เอาต์พุตของ GREP แรกไปที่ GREP ที่สองได้
เรียงลำดับ/UNIQ
เรียงลำดับ ฉันคิดว่าคุณรู้จักสิ่งนี้โดยที่ฉันไม่ต้องบอก โดยจะเรียงลำดับเอาต์พุตตามลำดับตัวอักษรหรือตัวเลข คุณยังสามารถเลือกฟิลด์ที่คุณต้องการใช้เป็นฐานในการเรียงลำดับได้
UNIQ อันนี้เข้าใจใช่ไหม? วิธีนี้จะลบรายการที่ซ้ำกันของเอาต์พุต บอกว่ามีเอาต์พุตมากกว่าหนึ่งบรรทัดสำหรับ “Jeremiah” ลองเดาดูสิว่าระบบจะแสดงออกมาอย่างไรเมื่อเรารันคำสั่ง UNIQ? ใช่แล้ว จะมีการแสดง “เยเรมีย์” เพียงคนเดียวเท่านั้น แต่จำไว้ว่าคุณต้องใช้ SORT ก่อน และใช้ PIPE เพื่อใช้คำสั่งนี้ เพราะคำสั่งนี้จะไม่ลบรายการที่ซ้ำกันเมื่อกระจายออกจากกัน
Column 4: SORT and UNIQ commands sort [filename] //Will sort the contents of the file based on "each line" sort -r [filename] //Will sort the contents in reversed order sort -k2 [filename] //Will sort the contents of the file based on field number 2 [commands] | sort [args] //Will sort the output of command based on argument given ---------------------------- uniq [filename] //Will remove duplicated output based on "each line", please notes, if the duplicated is not in 1-line different, then the duplicated items will not be removed sort [filename] | uniq //Will sort the contents of the file and remove the duplicated items [commands] | sort [args] | uniq -c //Will sort the output of the command, sort it based on the arguments given, show only uniq line (items), and will count how many duplicated items each line (items)
WC
Word Count มันจะนับคำที่เขียนในคำสั่งหรือไฟล์
Column 5: WC command wc [filename] //Check the line count, word count, and byte count wc -l [filename] //Get the number of lines in a file wc -w [filename] //Grab the number of words in a file wc -c --bytes [filename] //Pick the number of bytes in a file wc [directory] //not allowed, because there is no content inside ls -l | wc -l //count the files grep [keywords] | wc -l //count the keyword lines on a file or commands.
ข้อสรุป
เพียงเท่านี้ วันนี้ฉันเรียนรู้คำสั่งที่น่าทึ่งใน Linux ฉันคิดว่า Linux จะเข้าใจยากมาก แต่ไม่ใช่! ลินุกซ์ถูกสร้างขึ้นโดยกลุ่มบุคคล พวกเขาก็เป็นมนุษย์ ฉันก็เหมือนกัน! ดังนั้นฉันต้องสามารถเรียนรู้สิ่งนี้ได้!
เจอกันคราวหน้า!
Read more of my stories Here! < My Previous Blog My Next Blog >