6. Editing on the fly
-
The
trprogram is used to transliterate characters:echo "lowercase letters" | tr a-z A-ZMultiple characters can be converted to a single character:
echo "lowercase letters" | tr [:lower:] AWith the option
-dit can delete characters:echo "lowercase letters" | tr -d eWith the option
-sit can squeeze repeated characters:echo "aaabbbccc" | tr -s abecho "abcabcabc" | tr -s ab -
As another example, let's use
trto implement ROT13 encoding (where each character is moved 13 places up the alphabet):echo "secret text" | tr a-zA-Z n-za-mN-ZA-MTo decode, perform the same translation a second time:
echo "frperg grkg" | tr a-zA-Z n-za-mN-ZA-M -
The program
sedmeans stream editor.echo "front" | sed 's/front/back/'The command
sstands for substitute.echo "front" | sed 's_front_back_'The character immediately after
sbecomes the delimiter.The commands in
sedcan be preceded by an address:echo "front" | sed '1s/front/back/'echo "front" | sed '2s/front/back/' -
Let's try some more examples on
distros.txt:sed -n '1,5p' distros.txtThe option
-ndoes not print lines by default, and thepcommand prints only the lines in the given range.sed -n '/SUSE/p' distros.txtPrints only the lines that match the given regular expression.
sed -n '/SUSE/!p' distros.txtPrints only the lines that do not match.
-
The command
ssubstitutes by default only the first occurrence on a matching line:echo "aaabbbccc" | sed 's/b/B/'We can append the modifier
g(global) to replace all the occurrences:echo "aaabbbccc" | sed 's/b/B/g' -
Let's change the date format from MM/DD/YYYY to YYYY-MM-DD on
distros.txt:sed -E 's#([0-9]{2})/([0-9]{2})/([0-9]{4})$#\3-\1-\2#' distros.txtFirst, we are using the option
-E, --regexp-extendedbecause there are lots of special characters like(,),{,}in the regular expression and escaping all of them by a\would make it really messy and unreadable.Then, we are enclosing in parentheses the parts of the regexp that match the month (
[0-9]{2}), the day ([0-9]{2}) and the year ([0-9]{4}).The strings that are matched by a subexpression can be used in the replacement like this:
\n, wherenis the number of the matching subexpression (pair of parentheses). -
It is possible to give several commands to the same
sedprogram, like this:echo "aaabbbccc" | sed -e 's/b/B/g' -e 's/a/A/g'However, sometimes it is preferable to list these commands in a
sedscript file, and call this script instead. For example:cat <<'EOF' > distros.sed
#sed script to produce a distro report
1 i\
\
Linux Distribution Report\
s#([0-9]{2})/([0-9]{2})/([0-9]{4})$#\3-\1-\2#
y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/
EOFdownload
wget https://linux-cli.fs.al/examples/lesson07/distros.sedcat distros.sedsed -E -f distros.sed distros.txtThe first line is a comment.
Then, the
icommand inserts something before the first line.The
scommand changes the date format.Finally, the
ycommand transliterates the lower case characters to upper case, similar to the commandtr. However, unliketr, it does not recognize character ranges or character classes, so we have to list all the letters of the alphabet.