Text stream editing with sed
I wanted to edit 123 text files by inserting a new line into each one. After some Googling I managed to create a shell script to do this:
LC_CTYPE=C find . -type f -exec sed -i '' '5i\
lang: en\
' {} \;
Explanations:
LC_CTYPE=C
resolves character encoding errors, but ignores Non-ASCII characters, so a different solution is needed to edit non-English textfind . -type f -exec [command] {} \;
finds all files and executes the command on them with{}
as the input for eachsed
is a unix command which stands for stream editor. It can edit text within files with regex, for example replace, insert or delete text.-i ''
is required forsed
to work on MacOS, it stores an intermediary/backup file, but I skipped that since I could use git to roll back any unwanted changes (withgit reset --hard
)'5i\[newline] lang:en\[newline]'
does the following: create a new line at line number 5 with the textlang:en
I did this to edit the front-matter of books I have read, which contains metadata for each one. I want to add a language filter for my book list, so I needed to add language information to every book.
As most of the books are in English, I added this line to all of them and then edited front matter for the the Non-English books manually.