Sed (stream editor) merupakan utility unix yang dapat digunakan untuk mengganti teks/string dengan teks/string lain dalam sebuah file.

Syntax:
# sed -n ‘ADDRESS’p filename
# sed -n ‘/PATTERN/p’ filename

Pertama buatlah file thegeekstuff.txt
# vi thegeekstuff.txt
1. Linux – Sysadmin, Scripting etc.
2. Databases – Oracle, mySQL etc.
3. Hardware
4. Security (Firewall, Network, Online Security etc)
5. Storage
6. Cool gadgets and websites
7. Productivity (Too many technologies to explore, not much time available)
8. Website Design
9. Software Development
10.Windows- Sysadmin, reboot etc.

Coba Anda lihat isi filenya:
# cat thegeekstuff.txt

Sed Address Format 1: NUMBER
sed -n ‘N’p filename
Contoh:
# sed -n ‘3’p thegeekstuff.txt
3. Hardware

Sed Address Format 2: NUMBER1~NUMBER2
sed -n ‘M~N’p filename
Contoh
# sed -n ‘3~2’p thegeekstuff.txt
3. Hardware
5. Storage
7. Productivity (Too many technologies to explore, not much time available)
9. Software Development

Sed Address Format 3: START,END
sed -n ‘M,N’p filename
Contoh:
# sed -n ‘4,8’p thegeekstuff.txt
4. Security (Firewall, Network, Online Security etc)
5. Storage
6. Cool gadgets and websites
7. Productivity (Too many technologies to explore, not much time available)
8. Website Design

Sed Address Format 4: ‘$’ Last Line
sed -n ‘$’p filename
Contoh:
# sed -n ‘$’p thegeekstuff.txt
10.Windows- Sysadmin, reboot etc.

Sed Address Format 5: NUMBER,$
sed -n ‘N,$p’ filename
Contoh:
sed -n ‘4,$’p thegeekstuff.txt
4. Security (Firewall, Network, Online Security etc)
5. Storage
6. Cool gadgets and websites
7. Productivity (Too many technologies to explore, not much time available)
8. Website Design
9. Software Development
10.Windows- Sysadmin, reboot etc.

Sed Pattern Format 1: PATTERN
sed -n /PATTERN/p filename
Contoh:
# sed -n /Sysadmin/p thegeekstuff.txt
1. Linux – Sysadmin, Scripting etc.
10.Windows- Sysadmin, reboot etc.

Sed Pattern Format 2: /PATTERN/,ADDRESS
sed -n ‘/PATTERN/,Np’ filename
Contoh:
# sed -n ‘/Hardware/,6p’ thegeekstuff.txt
3. Hardware
4. Security (Firewall, Network, Online Security etc)
5. Storage
6. Cool gadgets and websites

Sed Pattern Format 3: ADDRESS,/PATTERN/
sed -n ‘N,/PATTERN/p’ filename
Contoh:
# sed -n ‘3,/Security/p’ thegeekstuff.txt
3. Hardware
4. Security (Firewall, Network, Online Security etc)

Sed Pattern Format 4: /PATTERN/,$
sed -n ‘/PATTERN/,$p’ filename
Contoh:
# sed -n ‘/Website/,$p’ thegeekstuff.txt
8. Website Design
9. Software Development
10.Windows- Sysadmin, reboot etc.

Sed Pattern Format 5: /PATTERN/,+N
sed -n ‘/PATTERN/,+Np’ filename
Contoh:
# sed -n ‘/Storage/,+2p’ thegeekstuff.txt
5. Storage
6. Cool gadgets and websites
7. Productivity (Too many technologies to explore, not much time available)

Sed Pattern Format 6: /PATTERN/,/PATTERN/
sed -n ‘/P1/,/P2/p’ filename
Contoh:
# sed -n ‘/Storage/,/Design/p’ thegeekstuff.txt
5. Storage
6. Cool gadgets and websites
7. Productivity (Too many technologies to explore, not much time available)
8. Website Design

Sumber:

Manual disini
http://www.thegeekstuff.com/2009/09/unix-sed-tutorial-printing-file-lines-using-address-and-patterns/

Advertisements