วันเสาร์ที่ 1 มีนาคม พ.ศ. 2551

sed - stream editor (2)

ต่อเลยดีกว่า เพื่อไม่ให้บทความมันยาวเกินไป เลยจะตัดๆเป็นช่วงๆไป
ต่อจากนี้จะลอง test ว่ามันทำงานอะไรได้บ้าง

1. Substitue - อันนี้เป็น function หลักเลยที่เราใช้อยู่ (ตอนนี้ใช้เป็นอยู่อย่างเดียว)
sed -e 's/line/section/g' test.txt
sed 's/line/section/' test.txt
sed 's/line/section/g' test.txt

สามตัวอย่างนี้ให้ผลลัพธ์เหมือนกันเลยอ่ะ ก็คือจะเปลี่ยนข้อมูลทั้งไฟล์ โดยเปลี่ยนข้อความ 'line' ให้เป็น 'section'

sed 's/line/section/g' test.txt > result.txt

ตัวอย่างสุดท้าย จะบอกว่า ถ้าไม่ระบุ output file - SED จะส่งผลลัพธ์ออกไปยัง STDOUT ซึ่งก็คือจอภาพ ถ้าระบุไว้มันก็จะ SAVE เข้า output file ซึ่งผลก็คือเราจะได้ file ใหม่ที่ผ่านการแก้ไขแล้ว โดยไม่ไปกวนกับไฟล์ต้นฉบับ
(ซึ่งก็ดี เพราะเกิดพลาดอะไรไป ก็จะได้ไม่ต้องเศร้ามากนัก)

sed 's/line/section/g' < test.txt > result.txt

ลองเช็ค syntax ของ SED
s/regexp/replacement/flags

นี่คือคำสั่งที่ใช้ในการทำ substitute
s แรกคือ s command - substitute command
regexp - ข้อความที่จะถูกแทนที่ - สามารถใช้เป็น Regular Expression ได้ด้วย (RegExp นี่เอาไว้จะเอามาเขียนด้วยดีกว่า เพราะใช้ประโยชน์ได้เยอะ)
replacement - ข้อความที่จะเอาไปแทนที่ - อันนี้ใช้ RegExp ไม่ได้อยู่แล้ว
flags - จะมีหรือไม่มีก็ได้

flags สำหรับ substitute
g - แทนทุกๆ RegExp ด้วย Replacement - แทนทุกอัน ทุกตำแหน่ง

บางครั้งคำสั่งบางตัวใน sed อาจทำให้เกิดความสับสน(มั้ง) ดังนั้นทางที่ดี ควรใส่ quote คร่อมเอาไว้ อย่างน้อยเพื่อความสบายใจ

เช่น
sed s/line/section/g testfile.txt
sed 's/line/section/g' testfile.txt
ทั้งสองตัว(ตอนนี้)ให้ผลเหมือนกัน แต่แบบมี quote ชัวร์กว่าว่าจะไม่ error

ยังมีอีก แต่แปะไว้ก่อนนา...

sed - stream editor (1)

sed - stream editor
text processing คุณภาพสูง
ใช้งานบ่อยใน shell script บน UNIX

เนื่องจากเป็นคำสั่งหนึ่งที่ยังไม่ค่อยรู้เรื่องเกี่ยวกับมันมากนัก
เพราะเห็นว่ามันทำงานได้มาก และน่าจะช่วยให้เวลาทำงานกับ script จะทำงานได้เร็วขึ้น เลยอยากจะศึกษามันซะหน่อย

อือม ก่อนอื่นคงต้องทำความรู้จักกับมันซักหน่อย
อันนี้เป็น version และ information ของมัน บน Fedora Core6

[thada@localhost ~]$ sed --version
GNU sed version 4.1.5
Copyright (C) 2003 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE,
to the extent permitted by law.

[thada@localhost ~]$ sed --version | grep version
GNU sed version 4.1.5

[thada@localhost ~]$ sed --version | grep version | awk '{print $4}'
4.1.5

[thada@localhost ~]$ sed
Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...

-n, --quiet, --silent
suppress automatic printing of pattern space
-e script, --expression=script
add the script to the commands to be executed
-f script-file, --file=script-file
add the contents of script-file to the commands to be executed
-i[SUFFIX], --in-place[=SUFFIX]
edit files in place (makes backup if extension supplied)
-l N, --line-length=N
specify the desired line-wrap length for the `l' command
--posix
disable all GNU extensions.
-r, --regexp-extended
use extended regular expressions in the script.
-s, --separate
consider files as separate rather than as a single continuous
long stream.
-u, --unbuffered
load minimal amounts of data from the input files and flush
the output buffers more often
--help display this help and exit
--version output version information and exit

If no -e, --expression, -f, or --file option is given, then the first
non-option argument is taken as the sed script to interpret. All
remaining arguments are names of input files; if no input files are
specified, then the standard input is read.

E-mail bug reports to: bonzini@gnu.org .
Be sure to include the word ``sed'' somewhere in the ``Subject:'' field.