rss logo

Some GNU sed examples


According to wikipedia, sed is a Unix utility that parses and transforms text, using a simple, compact programming language.


Modify an element inside files

For example, we modify each 011.html term by xperia_bouygues.html :

user@host:~$ sed -i 's/011.html/xperia_bouygues.html/' *.html

Print file without comments

user@host:~$ cat myfile | sed '/^#/d'

Print file without blanks lines

user@host:~$ cat myfile | sed '/^$/d'

Keep only lines with matching term

user@host:~$ sed -n -i '/vmail/p' /etc/courier/authlib/userdb/system

Delete carriage returns

user@host:~$ cat file | sed ':a;N;$!ba;s/\n//g'

Delete empty lines repetition

user@host:~$ cat file | sed 'N;s/^\n$//;P;D'

Uppercase first letter of files names

user@host $ for i in *mp3; do I=$(echo "$i" | sed -e "s/\b\(.\)\([a-z]* \)/\u\1\2/g" -e "s/ \(.\)\([a-z]*\).mp3/ \u\1\2.mp3/g"); mv "$i" "$I"; done
Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Contact :

contact mail address