Selain untuk search and replace string, ternyata command sed bisa juga digunakan untuk membalik kalimat. Berikut script untuk membalikkan kata atau kalimat dengan sed.
1. Tulis script di bawah ini di vim
#!/usr/bin/sed -f
#if only 1 char restart cycle, print pattern space and read next input line. If 2 char or more, the process to the next step.
/../! b # Reverse a line. Begin embedding the line between two newlines
s/^.*$/\n&\n/
# Move first character at the end. The regexp matches until
# there are zero or one characters between the markers
tx
:x
s/\(\n.\)\(.*\)\(.\n\)/\3\2\1/
tx
# Remove the newline markers
s/\n//g
2. Save script diatas dengan reverse.sed (ext bebas)
Cara penggunaannya:
1. Buka Terminal di Linux ( CTRL+ ALT+T)
2. Ketik perintah berikut
sed -f reverse.sed kalimat.txt
atau
echo "Indonesia raya" | sed -f reverse.sed
3. Dan hasilnya adalah ayar aisenodnI
No comments:
Post a Comment