gpt-3.5-turbo-0301· OpenAI
WildChat-4.8M (AI2)
With sed, how can I add a line before a match and remove a line after a match?
- User
With sed, how can I add a line before a match and remove a line after a match?
- Assistant
You can use the following syntax to add a line before a match:
sed '/pattern/ i new_line' file.txtFor example, if you want to add the line "bar" before every line that contains "foo" in the file "file.txt", you can run the following command:
sed '/foo/ i bar' file.txtTo remove a line after a match, you can use the following syntax:
sed '/pattern/ {N; d;}' file.txtFor example, if you want to remove the line after every line that contains "foo" in the file "file.txt", you can run the following command:
sed '/foo/ {N; d;}' file.txt