Skip to content

Instantly share code, notes, and snippets.

@kitzberger
Created March 14, 2024 08:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kitzberger/5281d1293c54ad2175c4ab5e1f7d911a to your computer and use it in GitHub Desktop.
Save kitzberger/5281d1293c54ad2175c4ab5e1f7d911a to your computer and use it in GitHub Desktop.
Useful sed commands

Duplicate a line and modify it

sed '/pattern/{p;s/pattern/replacement/}' input_file

Explanation:

  • /pattern/: This specifies the pattern that identifies the line you want to duplicate and modify.
  • {p;s/pattern/replacement/}: This is a group of commands enclosed within {}.
    • p is used to print the line as it is, and
    • s/pattern/replacement/ is used to substitute the pattern with the replacement string.

Delete everything after a matching line

sed '/pattern/,$d' input_file

Explanation:

  • /pattern/,$: This specifies a range from the line containing the pattern to the end of the file ($).
  • d: This command deletes the range specified.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment