Trace: sed

Sed

GNU/sed

Case transformation

GNU/sed has got the functionality to transform upper-case letters to lower-case letters and vice-versa. Such a transformation can be applied to single letters, or from a start point to an end point.

echo bee | \
sed 's/\([a-z]\)/\u\1/'
 
Bee

In the example above you see that the first letter matched by the character class '[a-z]' was 'b' and got translated to its upper-case counterpart 'B' by \u. The translation from upper-case to lower-case works the same way but instead with \l.

The translation of the first letter of every word to upper-case could be done with

echo 'hi this is the sed way of case transformation' | \
sed 's/\b\([a-z]\)/\u\1/g'
 
Hi This Is The Sed Way Of Case Transformation

Another way is to define a start point and an end point. To start a continuous transformation to upper-case you have to use \U instead of \u as start point. It ends at the \E or at the \L wich would start a transformation to lower-case from that point.

echo 'hi this is the sed way of case transformation' | \
sed 's/^\([^t]\+\)\(t[^s]\+s\)\(.*\)/\1\U\2\E\3/'
 
hi THIS is the sed way of case transformation

Substitution

N-th occurence

To substitute the second occurence of 'e' with '3'

$ echo 'hey there' | sed 's/e/3/2'
hey th3re

Substitute every 'e' with '3' but first

$ echo 'hey there' | sed 's/e/3/2g'
hey th3r3

To substitute the third 'e' with '3'

$ echo 'hey there' | sed 's/e/3/3'
hey ther3

Snippets

Extract text from .docx documents

unzip -p file.docx word/document.xml | \
sed -e 's/<\/w:p>/\n/g; s/<[^>]\{1,\}>//g; s/[^[:print:]\n]\{1,\}//g'

CommandlineFU

Join every line in file to single line

sed ':a; N; s/\n/ /; ta' <file>
  1. :a puts a label.
  2. N appends the next line into pattern space (which contains the current line)
  3. s/\n/ / substitues the newline character with a space character
  4. ta jumps to label a if it's previous command ends successfully

https://askubuntu.com/questions/164056/how-do-i-combine-all-lines-in-a-text-file-into-a-single-line/996766#996766

pub/tech/sed.txt · Last modified: 2024/02/10 14:32