Friday, 4 February 2022

How to use GNU Stream Editor (sed)

sed is a Unix tool, a GNU stream editor.
 
How to remove hex characters from the beginning of the file?
 
Example: The following command removes a BOM Unicode character (xEFBBBF) from the beginning of the file. Removal is done in-place:

$ sed -i '1s/^\xef\xbb\xbf//' commands.sql 
 
1s/ - only search the first line, other lines are unaffected
^ - the beginning of the line (only match at the start of the line)
\xEF\xBB\xBF - bytes to be removed - UTF-8 BOM (escaped hex string)
// - replace with empty string
 
 
If we wanted to keep the original file intact and create a new file, with all the changes:

$ sed '1s/^\xef\xbb\xbf//' < commands.sql > new_commands.sql


References:

No comments: