How could I make a separate sed script (let's call it script.sed) that would display only the changed lines without having to use the -n
option while executing it? (Sorry for my English)
I have a file called data2.txt with digits and I need to change the lines ending with ".5" and print those changed lines out in the console.
I know how to do it with a single command (sed -n 's/.5$//gp' data2.txt
), however our university professor requires us to do the same using sed -f script.sed data2.txt
command.
Any ideas?
The following should work for your sed script:
s/.5$//gp
d
The -n
option will suppress automatic printing of the line, the other way to do that is to use the d
command. From man page:
d Delete pattern space. Start next cycle.
This works because the automatic printing of the line happens at the end of a cycle, and using the d
command means you never reach the end of a cycle so no lines are printed automatically.
This might work for you (GNU sed):
#n
s/.5$//p
Save this to a file and run as:
sed -f file.sed file.txt
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With