Vous êtes sur la page 1sur 5

Perintah Sed

1. Menggunakan perintah script sed dari terminal.


pens@debian:~$ cat > word
agus
irma
afiludin
candra
pens@debian:~$ sed -n -f /home/pens/word
pens@debian:~$ ls
arit1.sh aveline
findparis nlogins Screenshot.png
arit2
awk1.sh
for_loop
opt
sdata
arit2.sh awk2.sh
func1.sh opts
set1.sh
arit2..sh check_env
func2.sh
out1.sh set2.sh
arit3.sh check_env1.sh func3.sh
out2.sh set3.sh
arit4.sh check_env.sh func4.sh
out3.sh set4.sh
arit5
contoh1
func5.sh
out4.sh set5.sh
arit55.sh contoh2
func6.sh
out.sh set6.sh
arit5.sh contoh3
func7.sh
p1.sh set_env1.sh
arit5..sh data
func8.sh
p2.sh set_env.sh
arit6.sh dayofmonth getlin
p3.sh split.awk
arr1.sh dec1.sh
hits
par.awk Templates
arr2.sh defi1.sh
if1.sh
Pictures test
arr3.sh define
if2.sh
pilih test11.sh
arr4sh defi.sh
if2..sh
ploop.sh test1...sh
arr4.sh Desktop
index.awk pos1.sh test2.sh
arr5.sh devine.sh
invent
pos2.sh test3.sh
arr6.sh Documents
latihan-1 pos3.sh test4.sh
arr7.sh Downloads
latihan-2 pos4.sh test5.sh
arr8.sh dups
latihan-3 pos5.sh test.sh
arr9.sh exp.awk
lsdir
pos6.sh test..sh
arrA.sh expres1111.sh math
prn_pen tri.awk
arrB.sh expres111.sh Music
progsh tryrmsys
arrC.sh expres11.sh mydir
Public Videos
ave
expres1.sh myname
rev.awk while_loop
ave2
expres1..sh my-script.sh rmdups word
2. Menggunakan perintah sed dari terminal dengan opsi f dan -e.
pens@debian:~$ cat>remind
Jan today at 4
Call DEC
Add serial line for Brad
Home at five sharp
pens@debian:~$ cat > script
s/DEC/Dept. Environ. Cons./
/Jan/s/today/tomorrow/
li\
Werner and Raquel this weekend\
Feed polly for adam
4d
pens@debian:~$ sed -f script remind
Jan today at 4
Call DEC
Add serial line for Brad
Home at five sharp
pens@debian:~$ sed -e /DEC/d -e s/five/four/ remind
Jan today at 4
Add serial line for Brad
Home at four sharp

3. Menggunakan perintah sed dari terminal dengan beberapa opsi.


pens@debian:~$ cat > f1
.CX 3
.DS
Twas Sol and solstice
Ad and Astra
Til Hic saw Sum
In Corporate
.DE
pens@debian:~$ cat > script1
/DS/b fail
/KS/b fail
/EQ/,/EN/p
b
: fail
s/.*/No displays allowed at start/p
q
pens@debian:~$ sed -n -f script1 f1
No displays allowed at start
pens@debian:~$ sed -e s/DS/EQ/ -e s/DE/EN/ -n -f script1 f1
.EQ
Twas Sol and solstice
Ad and Astra
Til Hic saw Sum
In Corporate
.EN
4. Menggunakan perintah sed untuk menampilkan bilangan random 10 digit.
pens@debian:~$ cat > sed4.sh
head -c4 /dev/urandom | od -N4 -tu4 | sed -ne '1s/.* //p'
exit 0
pens@debian:~$ chmod u+x sed4.sh
pens@debian:~$ ./sed4.sh
389355892
5. Menggunakan perintah sed untuk mencari data string.
pens@debian:~$ vi sed5.sh
#!/bin/bash
directory=/usr/bin/
fstring="Free Software Foundation"
for file in $( find $directory -type f -name '*' | sort )
do
strings -f $file | grep "$fstring" | sed -e "s%$directory%%"
done
exit 0
pens@debian:~$ chmod u+x sed5.sh
pens@debian:~$ ./sed5.sh
[: Copyright %s %d Free Software Foundation, Inc.
addr2line: Copyright 2009 Free Software Foundation, Inc.
alacarte: # License as published by the Free Software Foundation; either
amuFormat.sh: # the Free Software Foundation, either version 3 of the License, or
aot-compile: ## the Free Software Foundation; either version 2 of the License, or
applet-coherence: # the Free Software Foundation; either version 2, or (at your option)
aptdcon: # the Free Software Foundation; either version 2 of the License, or
aptdcon: # with this program; if not, write to the Free Software Foundation, Inc.,
apt-listchanges: # the Free Software Foundation; either version 2 of the License, or
ar: Copyright 2009 Free Software Foundation, Inc.
arch: Copyright %s %d Free Software Foundation, Inc.

6. Perintah sed untuk menampilkan data baris.


pens@debian:~$ cat > lines
10
20
30
pens@debian:~$ sed -n -e 'p
>g
> p' lines
10
20
30
7. Perintah sed untuk mengambil data dari suatu file dan disimpan ke file lain.
pens@debian:~$ cat >script
s/CUXBASIC/3/g
s/SCMDARGS/CSHONE.2/g
s/CSHONE/4/g
pens@debian:~$ cat >doc
Chapter CUXBASIC covers the fundamental
Feature of the URC language. However two
important features - - arguments and metaarguments - - aren't discussed until
Section SCMDARGS and section SMETA
pens@debian:~$ sed -f script doc
Chapter 3 covers the fundamental
Feature of the URC language. However two
important features - - arguments and metaarguments - - aren't discussed until
Section 4.2 and section SMETA
8. Sed untuk membaca semua baris pada sebuah file dan delete baris tertentu
pens@debian:~$ vi sed8.sh
#!/bin/bash
sed -e /^s/d "$1"
exit 0
pens@debian:~$ chmod u+x sed8.sh
pens@debian:~$ ./sed8.sh file1.txt
9. Menggunakan perintah sed untuk subtitusi pattern
pens@debian:~$ vi sed9.sh
#!/bin/bash
ARGS=3
E_BADARGS=65
if [ $# -ne "$ARGS" ]
then
echo "Usage: `basename $0` old-pattern new-pattern filename"
exit $E_BADARGS
fi
old_pattern=$1
new_pattern=$2
if [ -f "$3" ]
then
file_name=$3
else
echo "File \"$3\" does not exist."
exit $E_BADARGS

fi
sed -e "s/$old_pattern/$new_pattern/g" $file_name
exit 0
pens@debian:~$ cat word
agus
irma
afiludin
candra
pens@debian:~$ chmod u+x sed9.sh
pens@debian:~$ ./sed9.sh agus candra word
candra
irma
afiludin
candra
pens@debian:~$ ./sed9.sh candra agus word
agus
irma
afiludin
agus
10. Menggunakan perintah sed sebagai implementasi perintah grep
pens@debian:~$ vi sedb.sh
#!/bin/bash
E_BADARGS=65
if [ -z "$1" ]
then
echo "Usage: `basename $0` pattern"
exit $E_BADARGS
fi
echo
for file in *
do
output=$(sed -n /"$1"/p $file)
if [ ! -z "$output" ]
then
echo -n "$file: "
echo $output
fi
echo
done
echo
exit 0
pens@debian:~$ chmod u+x sedb.sh
pens@debian:~$ ./sedb.sh afiludin
sed: read error on Desktop: Is a directory
sed: read error on Documents: Is a directory
sed: read error on Downloads: Is a directory
sed: read error on Music: Is a directory
sed: read error on mydir: Is a directory
sed: read error on Pictures: Is a directory
sed: read error on Public: Is a directory
sed: read error on Templates: Is a directory
sed: read error on Videos: Is a directory
word: afiludin
pens@debian:~$ ./sedb.sh 200
sed: read error on Desktop: Is a directory
sed: read error on Documents: Is a directory
sed: read error on Downloads: Is a directory
sed: read error on Music: Is a directory

sed: read error on mydir: Is a directory


sed: read error on Pictures: Is a directory
progsh: BEGIN { printf "Program Shell awk 2007.\n"
sed: read error on Public: Is a directory
sdata: 200 200 18 0 200 -200 18 1 -100 -200 18 1 -100 200 18 1 200 200 18 1
sed: read error on Templates: Is a directory
sed: read error on Videos: Is a directory
11. Menggunakan perintah sed untuk pembuatan kolom
pens@debian:~$ vi sedc.sh
#!/bin/bash
(printf "PERMISSIONS LINKS OWNER GROUP SIZE MONTH DAY HH:MM PROGNAME\n" \;
ls -l | sed 1d) | column -t
exit 0
pens@debian:~$ chmod u+x sedc.sh
pens@debian:~$ ./sedc.sh
PERMISSIONS LINKS OWNER GROUP SIZE MONTH DAY HH:MM PROGNAME
-rwxr--r-- 1
pens pens 30 Oct 21 00:14 arit1.sh
-rw-r--r-- 1
pens pens 54 Oct 21 00:21 arit2
-rwxr--r-- 1
pens pens 55 Oct 21 00:19 arit2.sh
-rwxr--r-- 1
pens pens 42 Oct 21 00:23 arit2..sh
-rwxr--r-- 1
pens pens 55 Oct 21 00:28 arit3.sh
..................................................................................................
-rwxr--r-- 1
pens pens 173 Dec 2 00:27 tri.awk
-rw-r--r-- 1
pens pens 156 Dec 2 01:07 tryrmsys
drwxr-xr-x 2
pens pens 4096 Sep 2 22:58 Videos
-rw-r--r-- 1
pens pens 142 Dec 2 01:04 while_loop
-rw-r--r-- 1
pens pens 26 Dec 15 23:03 word
pens@debian:~$ ls -l
total 560
-rwxr--r-- 1 pens pens 30 Oct 21 00:14 arit1.sh
-rw-r--r-- 1 pens pens 54 Oct 21 00:21 arit2
-rwxr--r-- 1 pens pens 55 Oct 21 00:19 arit2.sh
-rwxr--r-- 1 pens pens 42 Oct 21 00:23 arit2..sh
-rwxr--r-- 1 pens pens 55 Oct 21 00:28 arit3.sh
................................................................................................
-rwxr--r-- 1 pens pens 173 Dec 2 00:27 tri.awk
-rw-r--r-- 1 pens pens 156 Dec 2 01:07 tryrmsys
drwxr-xr-x 2 pens pens 4096 Sep 2 22:58 Videos
-rw-r--r-- 1 pens pens 142 Dec 2 01:04 while_loop
-rw-r--r-- 1 pens pens 26 Dec 15 23:03 word