Home > Text Processing & Search > sed

sed -f: Process Text with a Script File

sed is a stream editor that reads data from text files or standard input, transforms it according to specified rules, and outputs the result. The -f option allows sed commands to be written into a script file for execution, which helps in efficiently managing complex or repetitive text processing tasks. It is useful for consolidating multiple sed commands into a single file for reusability.

Overview

sed is a powerful tool for editing text streams. Using the -f option, you can save sed commands in a separate script file and execute them. This is very useful for managing complex scripts or when you need to apply multiple sed commands at once.

Key Features

  • Easy management of complex scripts
  • Increased script reusability
  • Consolidation of multiple sed commands into one file
  • Facilitates version control and sharing

Key Options

These are the commonly used options when using sed -f.

Specifying the Script File

Other Useful Options

Generated command:

Try combining the commands.

Description:

`sed` Executes the command.

Combine the above options to virtually execute commands with AI.

Usage Examples

Various usage examples of the sed -f command.

Using a Basic Script File

echo 'This is an apple.' > input.txt
echo 's/apple/orange/g' > script.sed
sed -f script.sed input.txt

Write the command 's/apple/orange/g' in script.sed and apply it to input.txt.

Script File with Multiple Commands

echo -e 'This is an error.\n# This is a comment.\nAnother error.' > data.txt
echo -e 's/error/ERROR/g\n/^#/d' > multi.sed
sed -f multi.sed data.txt

Write multiple sed commands in multi.sed and apply them to data.txt. (e.g., replace 'error' with 'ERROR', and delete lines starting with '#')

In-Place File Editing with Backup

echo 'This is old_text.' > file.txt
echo 's/old_text/new_text/g' > replace.sed
sed -i.bak -f replace.sed file.txt
cat file.txt
cat file.txt.bak

Modify file.txt directly using commands from replace.sed and create a backup file with the .bak extension.

Printing Specific Lines with the -n Option

echo -e 'Line 1\nLine 2\nLine 3' > log.txt
echo '2p' > print_line.sed
sed -n -f print_line.sed log.txt

Write the command '2p' in print_line.sed to print only the second line of log.txt.

Tips & Precautions

Useful tips and points to note when using sed -f.

Script File Permissions

Script files executed with sed -f do not require execute permissions; only read permissions are necessary.

Debugging

If your sed script is not working as expected, you can use the -n option with the p command to check if specific patterns are matching, or use the l (list) command to view non-printable characters.

Combining -e and -f

You can specify simple commands directly using the -e option and use complex script files with the -f option. This allows for flexible script writing.

Backup File Management

It is safer to create backup files when using the -i option. It is advisable to periodically clean up unnecessary backup files.


Same category commands