Skip to content

Instantly share code, notes, and snippets.

@flashvoid
Last active May 23, 2023 02:42
Show Gist options
  • Save flashvoid/e26b7111e89cd85cb868dc98b89435ee to your computer and use it in GitHub Desktop.
Save flashvoid/e26b7111e89cd85cb868dc98b89435ee to your computer and use it in GitHub Desktop.
[void@stas-console ~]$ cat /tmp/t
foo=bar
boo=baz
zee=roo=zag
# this prints changed file but doesn't write changes to file
[void@stas-console ~]$ sed -e 's/=/="/' -e 's/$/"/g' -e 's/^/export /g' /tmp/t
export foo="bar"
export boo="baz"
export zee="roo=zag"
# this prints changes and writes them into the file
[void@stas-console ~]$ sed -i -e 's/=/="/' -e 's/$/"/g' -e 's/^/export /g' /tmp/t
export foo="bar"
export boo="baz"
export zee="roo=zag"
# logic
-e <>, sed regex to apply to file, can be provided multiple times to apply multiple regexes
-i, write changes into file
# regexes
this regex substitutes substring within file, common structure is 's/<from_string>/<to_string>/g', 's' means substitute, 'g' means apply to whole file (by deafult only first instance.
* 's/=/="/g', changes every = to ="
* 's/$/"/g', changes end-of-line to " (same as inserting to the end of the string)
* 's/^/export /g', changes start-of-line symbol to 'export ' (same as inserting into the beginning of the line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment