Written by Wim Stockman - on 18 June 2022
Example File: #cat /tmp/file Line One Line Two Line Three Line Four Line Five
sed 's/^/PREFIX: /' /tmp/file
PREFIX: Line One PREFIX: Line Two PREFIX: Line Three PREFIX: Line Four PREFIX: Line Five
sed 's/$/ : Postfix/' /tmp/file
Line One : Postfix Line Two : Postfix Line Three : Postfix Line Four : Postfix Line Five : Postfix
sed 's/.*/PREFIX: & :Postfix/' /tmp/file
PREFIX: Line One :Postfix PREFIX: Line Two :Postfix PREFIX: Line Three :Postfix PREFIX: Line Four :Postfix PREFIX: Line Five :Postfix
The magic happens by using the ‶&″ which means : ‶the whole part of the input that was matched by the pattern″
sed '1i Before First Line' /tmp/file
Before First Line Line One Line Two Line Three Line Four Line Five
In Sed every command can be prepended by a line number.
So the command i inserts text before so 1i - means before line 1
A special case of the line number is the $ sign which means last line.
sed '$s/.$/LAST/' /tmp/file
sed '$s/.$//' /tmp/file
text/gemini; lang=en
This content has been proxied by September (3851b).