Cara Find and Replace di Shell Dengan Sed
- Categories:
- console
Perintah grep
dan sed
dapat digunakan untuk melakukan find and replace di Shell Linux atau macOS. Berbeda sistem operasi memiliki sintaks yang berbeda.
Berikut cara melakukan find and replace di satu atau banyak file.
# macOS
# mengganti satu file
sed -i '' -e 's/oldstring/newstring/g' example/path/file.txt
# mengganti banyak file
grep -rl oldstring example/path | xargs sed -i '' -e 's/oldstring/newstring/g'
# linux
# mengganti satu file
sed -i 's/oldstring/newstring/g' example/path/file.txt
# mengganti banyak file
grep -rl oldstring example/path | xargs sed -i 's/oldstring/newstring/g'
Separator perintah sed
dapat diganti dengan +
atau #
jika kata yang ingin kita ganti memiliki karakter /
.
# macOS
sed -i '' -e 's+oldstring+newstring+g' example/path/file.txt
grep -rl oldstring example/path | xargs sed -i '' -e 's+oldstring+newstring+g'
sed -i '' -e 's#oldstring#newstring#g' example/path/file.txt
grep -rl oldstring example/path | xargs sed -i '' -e 's#oldstring#newstring#g'
# linux
sed -i 's+oldstring+newstring+g' example/path/file.txt
grep -rl oldstring example/path | xargs sed -i 's+oldstring+newstring+g'
sed -i 's#oldstring#newstring#g' example/path/file.txt
grep -rl oldstring example/path | xargs sed -i 's#oldstring#newstring#g'
Referensi
Recent Posts
AWS Secrets Manager
Explanation about AWS Secrets Manager with example code.
Envelope Encryption
Envelope encryption is the practice of encrypting plaintext data with a data key, and then encrypting the data key under another key.
Tutorial - "su username vs su - username" - A Security Perspective
The main difference between `su username` and `su - username` lies in the environment variables that are loaded when switching to the specified user.
Subdomain Hijacking
My dormant subdomain was recently hijacked, redirecting it to a online gamble registration page.
C# DbContext ServiceLifeTime
my note about C Sharp ServiceLifeTime