PEMROSESAN TEKS

INTRO SINGKAT

Apa itu Pemrosesan Teks?

Menurut saya, pada blog sebelumnya saya sudah mulai belajar tentang Pengolahan Teks yang mengolah output yang berformat teks menjadi output yang ingin kita hasilkan. lihat (Anda tahu maksud saya, kan? Sepertinya kita tidak menggunakan semua informasi keluaran, tetapi hanya sebagian). Dan di blog ini, saya akan menjelajahi alat lain yang dapat digunakan untuk pemrosesan teks.

Mengapa kita mempelajarinya?

Hal ini akan memudahkan kita dalam mencari teks atau informasi berdasarkan data yang kita peroleh dari outputnya.

PERINTAH

MEMOTONG

Jika Anda pernah membuat kode dalam Java atau C (Bahasa Pemrograman), Anda pasti pernah mendengar tentang “Substring”. Jika Anda pernah mengkompilasi data di Microsoft Excel, Anda pasti pernah mendengar tentang “Mid()”. Nah yang ini seperti itu.

Kita dapat menggunakannya untuk file dan perintah.

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

AWK

Singkatan dari nama penulis perintah ini. Siapa, Weinberger, Kernighan. Ini sedikit lebih rumit daripada potong, namun memiliki kemampuan unik dalam hal pemrosesan teks. Bisa juga digunakan untuk mencari teks, ukuran teks, dan lain-lain.

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

Tahukah Anda Regex? Nah Linux memiliki dua alat yang terkait dengan Ekspresi Reguler.
GREP adalah singkatan dari Global Regular Expression Print,
EGREP adalah singkatan dari Expand 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

Jadi jika Anda ingin mencari lebih dari satu kata kunci, Anda bisa menggunakan egrep. Anda juga dapat memasukkan keluaran GREP pertama ke GREP kedua.

SORT/UNIQ

SORT, saya rasa Anda mengetahuinya tanpa saya beri tahu. Ini mengurutkan keluaran berdasarkan urutan abjad atau numerik. Anda juga dapat memilih bidang mana yang ingin Anda gunakan sebagai dasar pengurutan.

UNIQ, yang ini, Anda mengerti, kan? Ini akan menghapus item keluaran yang diduplikasi. Dikatakan bahwa ada lebih dari satu baris keluaran untuk “Jeremiah”, coba tebak bagaimana sistem akan menampilkannya ketika kita menjalankan perintah UNIQ? Benar, hanya SATU “Yeremia” yang akan ditampilkan. Namun ingat, Anda harus menggunakan SORT terlebih dahulu, dan menggunakan PIPE untuk menggunakan perintah ini. Karena perintah ini tidak akan menghapus item duplikat ketika tersebar satu sama lain.

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

Jumlah Kata, ini akan menghitung kata-kata yang tertulis dalam perintah atau file.

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.

KESIMPULAN

Sekian untuk hari ini, saya mempelajari perintah-perintah luar biasa di Linux. Saya pikir Linux akan sangat sulit untuk dipahami, tapi ternyata tidak! Linux dibuat oleh sekelompok orang. Jadi mereka manusia, aku juga! Jadi, saya harus bisa mempelajari hal ini!

Sampai jumpa lain waktu!

                     Read more of my stories Here!
< My Previous Blog                                    My Next Blog >