Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

a simple sed script displaying only changed lines

Tags:

regex

bash

sed

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?

like image 434
user3221028 Avatar asked Mar 21 '23 21:03

user3221028


2 Answers

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.

like image 135
Andrew Clark Avatar answered Apr 01 '23 10:04

Andrew Clark


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
like image 34
potong Avatar answered Apr 01 '23 08:04

potong