[sed] replace the first line with certain info

 

If you want to use sed to replace the first line of a file with

<div id="lexicoEntryPage">

then the proper command is:

sed '1s/.*/<div id="lexicoEntryPage">/' inputfile > outputfile

Explanation:

  • 1 → applies the substitution only to line 1

  • s → means substitute

  • .* → matches the entire contents of the line

  • The replacement text is <div id="lexicoEntryPage">

  • The redirection > outputfile saves the modified result to a new file

If you want to edit the file in place (i.e., overwrite it):

sed -i '1s/.*/<div id="lexicoEntryPage">/' inputfile

⚠️ On macOS, sed -i requires an empty string argument to avoid backup creation:

sed -i '' '1s/.*/<div id="lexicoEntryPage">/' inputfile

Would you like the command to also ensure a matching closing </div> tag at the end of the file (if not already present)?

 

posted @ 2025-10-23 18:17  profesor  阅读(3)  评论(0)    收藏  举报