Skip to main content
Add stuff
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

To edit a file, use a scriptable text editor, such as Ed or Ex (both POSIX editors). The syntax is very similar.

printf '%s\n' '/^import matplotlib as mpl$/d' 's/mpl/plt' 'w' 'q' | ed -s file
printf '%s\n' '/^import matplotlib as mpl$/d' 's/mpl/plt' 'x' | ex file
  • printf '%s\n' supplies commands to the editor.

  • /^import matplotlib as mpl$/d deletes the first line matching the pattern.

  • s/mpl/plt performs the substitution on the next line.

  • w and q or x save the changes.

If you really want Sed,

sed '/^import matplotlib as mpl$/N; s/.*\nmpl/plt/' file

Addressing your expanded question:

find /path/to/dir/ -type f -exec sh -c '
    printf "%s\n" "/^import matplotlib as mpl\$/d" "s/mpl/plt" "w" "q" |
    ed -s "$1"' sh {} \;

To edit a file, use a scriptable text editor, such as Ed or Ex (both POSIX editors). The syntax is very similar.

printf '%s\n' '/^import matplotlib as mpl$/d' 's/mpl/plt' 'w' 'q' | ed -s file
printf '%s\n' '/^import matplotlib as mpl$/d' 's/mpl/plt' 'x' | ex file
  • printf '%s\n' supplies commands to the editor.

  • /^import matplotlib as mpl$/d deletes the first line matching the pattern.

  • s/mpl/plt performs the substitution on the next line.

  • w and q or x save the changes.

If you really want Sed,

sed '/^import matplotlib as mpl$/N; s/.*\nmpl/plt/' file

To edit a file, use a scriptable text editor, such as Ed or Ex (both POSIX editors). The syntax is very similar.

printf '%s\n' '/^import matplotlib as mpl$/d' 's/mpl/plt' 'w' 'q' | ed -s file
printf '%s\n' '/^import matplotlib as mpl$/d' 's/mpl/plt' 'x' | ex file
  • printf '%s\n' supplies commands to the editor.

  • /^import matplotlib as mpl$/d deletes the first line matching the pattern.

  • s/mpl/plt performs the substitution on the next line.

  • w and q or x save the changes.

If you really want Sed,

sed '/^import matplotlib as mpl$/N; s/.*\nmpl/plt/' file

Addressing your expanded question:

find /path/to/dir/ -type f -exec sh -c '
    printf "%s\n" "/^import matplotlib as mpl\$/d" "s/mpl/plt" "w" "q" |
    ed -s "$1"' sh {} \;
Sed alternative, anchor line
Source Link
Quasímodo
  • 19.4k
  • 4
  • 41
  • 78

To edit a file, use a scriptable text editor, such as Ed or Ex (both POSIX editors). The syntax is very similar.

printf '%s\n' '/^import matplotlib as mpl$/d' 's/mpl/plt' 'w' 'q' | ed -s file
printf '%s\n' '/^import matplotlib as mpl$/d' 's/mpl/plt' 'x' | ex file
  • printf '%s\n' supplies commands to the editor.

  • /^import matplotlib as mpl$/d deletes the first line matching the pattern.

  • s/mpl/plt performs the substitution on the next line.

  • w and q or x save the changes.

If you really want Sed,

sed '
    /^import matplotlib as mpl$/N
   N; s/.*\n.**\nmpl/plt.style.use(mpl_plt_default_template)/
 ' file

To edit a file, use a scriptable text editor, such as Ed or Ex (both POSIX editors). The syntax is very similar.

printf '%s\n' '/^import matplotlib as mpl$/d' 's/mpl/plt' 'w' 'q' | ed -s file
printf '%s\n' '/^import matplotlib as mpl$/d' 's/mpl/plt' 'x' | ex file
  • printf '%s\n' supplies commands to the editor.

  • /^import matplotlib as mpl$/d deletes the first line matching the pattern.

  • s/mpl/plt performs the substitution on the next line.

  • w and q or x save the changes.

If you really want Sed,

sed '
    /^import matplotlib as mpl$/N
    s/.*\n.*/plt.style.use(mpl_plt_default_template)/
 ' file

To edit a file, use a scriptable text editor, such as Ed or Ex (both POSIX editors). The syntax is very similar.

printf '%s\n' '/^import matplotlib as mpl$/d' 's/mpl/plt' 'w' 'q' | ed -s file
printf '%s\n' '/^import matplotlib as mpl$/d' 's/mpl/plt' 'x' | ex file
  • printf '%s\n' supplies commands to the editor.

  • /^import matplotlib as mpl$/d deletes the first line matching the pattern.

  • s/mpl/plt performs the substitution on the next line.

  • w and q or x save the changes.

If you really want Sed,

sed '/^import matplotlib as mpl$/N; s/.*\nmpl/plt/' file
Sed alternative, anchor line
Source Link
Quasímodo
  • 19.4k
  • 4
  • 41
  • 78

To edit a file, use a scriptable text editor, such as Ed or Ex (both POSIX editors). The syntax is very similar.

printf '%s\n' '/import^import matplotlib as mplmpl$/d' 's/mpl/plt' 'w' 'q' | ed -s file
printf '%s\n' '/import^import matplotlib as mplmpl$/d' 's/mpl/plt' 'x' | ex file
  • printf '%s\n' supplies commands to the editor.

  • /import^import matplotlib as mplmpl$/d deletes the first line matching the pattern.

  • s/mpl/plt performs the substitution on the next line.

  • w and q or x save the changes.

If you really want Sed,

sed '
    /^import matplotlib as mpl$/N
    s/.*\n.*/plt.style.use(mpl_plt_default_template)/
' file

To edit a file, use a scriptable text editor, such as Ed or Ex (both POSIX editors). The syntax is very similar.

printf '%s\n' '/import matplotlib as mpl/d' 's/mpl/plt' 'w' 'q' | ed -s file
printf '%s\n' '/import matplotlib as mpl/d' 's/mpl/plt' 'x' | ex file
  • printf '%s\n' supplies commands to the editor.

  • /import matplotlib as mpl/d deletes the first line matching the pattern.

  • s/mpl/plt performs the substitution on the next line.

  • w and q or x save the changes.

To edit a file, use a scriptable text editor, such as Ed or Ex (both POSIX editors). The syntax is very similar.

printf '%s\n' '/^import matplotlib as mpl$/d' 's/mpl/plt' 'w' 'q' | ed -s file
printf '%s\n' '/^import matplotlib as mpl$/d' 's/mpl/plt' 'x' | ex file
  • printf '%s\n' supplies commands to the editor.

  • /^import matplotlib as mpl$/d deletes the first line matching the pattern.

  • s/mpl/plt performs the substitution on the next line.

  • w and q or x save the changes.

If you really want Sed,

sed '
    /^import matplotlib as mpl$/N
    s/.*\n.*/plt.style.use(mpl_plt_default_template)/
' file
Source Link
Quasímodo
  • 19.4k
  • 4
  • 41
  • 78
Loading