Here’s a question from one of our regular readers Mr. Manish. His question is, how to use multiple patterns at once with the Sed command in Linux for delete or replace operations.
Consider an example file containing the following text. (I’m using the same example the reader asked for).
SLURM_CLUSTER_NAME = test SLURM_JOB_ID = 525595 SLURM_JOB_NAME = test1 SLURM_JOB_USER = test SLURM_JOB_NODELIST = cn01 SLURM_JOB_UID = 5016 SLURM_JOB_PARTITION = standard-low SLURM_SUBMIT_DIR = /home/test SLURM_TASK_PID = 108983 SLURM_CPUS_ON_NODE = 1 SLURM_NTASKS = SLURM_TASK_PID = 108983
Now the reader wants a single
Sed command to delete the lines that match the pattern –
SLURM_SUBMIT_DIR from the file. So how do you do that? Here we go.
Delete line matching pattern and print with Sed
$ set '/PATTERN/d' filename
$ sed '/SLURM_JOB_USER/d' test.txt
The above command will delete the line matching the pattern ‘
SLURM_JOB_USER‘ and prints the output to the stdout.
Delete line matching pattern and modify the file with Sed
$ set -i '/PATTERN/d' filename
'-i' option will modify the file.
$ sed -i '/SLURM_JOB_USER/d' test.txt
Using multiple patterns at once with Sed
What if you want to delete multiple lines matching multiple patterns with a single Sed command? Here we go,
$ set -i '/PATTERN1/d;/PATTERN2/d' filename
$ sed -i '/SLURM_JOB_USER/d;/SLURM_SUBMIT_DIR/d' test.txt
The above command will delete the lines containing
SLURM_SUBMIT_DIR patterns and save the file. Note the semicolon (;) for inputting multiple patterns within the single command.
Replace pattern and print out with Sed
$ sed 's/PATTERN/REPLACE_STRING/g' test.txt
$ sed 's/SLURM_JOB_USER/REPLACE1/g' test.txt
'g' represents global – it replaces all instances of the pattern in each line, instead of just the first (which is the default behavior)
Replace pattern and modify the file with Sed
$ sed -i 's/SLURM_JOB_USER/REPLACE1/g' test.txt
'-i' will modify the file.
Use multiple patterns at once with Sed
$ sed -i 's/SLURM_JOB_USER/REPLACE1/g;s/SLURM_SUBMIT_DIR/REPLACE2/g' test.txt
Look out for the semicolon(;) to add more patterns within the single command.
Love it? Let us know if you have a query in the comments below and we will try covering it here.