sed command removes lines in file

Hi,

I am modifying a file with sed command. i want to make SCORE= blank in the file whereever SCORE=somevalue.

What will *$ do in the below command?

cat $file | sed 's/SCORE=.*$/SCORE=\r/g' > newfile

The last line is also missing in the new file. How to make SCORE='100' to SCORE= blank in a file

What is the use of $ in sed command?

Thanks
Ashok

$ means "must match until the end of the line". .* means "match zero or more of any character". a* would mean "match 1 or more a's".

So

's/SCORE=.*$/SCORE=\r/g

means match SCORE (at any point in the line) plus zero or more characters, until the end of the line, and replace it with SCORE=\r. The /g tells it to do so multiple times on each line if there's more than one match, but if you're matching everything after SCORE= that's probably redundant.

Why the \r? That might be confusing the output since, when printed in a terminal, it will return to the beginning of the line without moving down one line, making it look like it's been deleted.

1 Like

Hi,

Thanks for your reply. Below is the content of my file. How to replace SCCORE=somevalue to SCORE=blank in all the places in the file.

Please note that SCORE will have different values at different places?

How too use sed command to replace SCORE=somevalue?

[KEY A]
TABLE=TableA
KEY=KEYA
DESC=VALUE
ORDERBY=Id
SCORE=C:\Common\TestscoreA
[KEY B]
TABLE=TableB
KEY=KEYA
DESC=VALUE
ORDERBY=Id
SCORE=C:\Common\TestscoreB
[KEY C]
TABLE=TableA
KEY=KEYA
DESC=VALUE
ORDERBY=Id
SCORE=C:\Common\TestscoreC

Thanks
Ashok

should be good enough with

sed 's/SCORE=.*/SCORE=/'
1 Like

You're welcome. You might want to read it too.

cat $file | sed 's/SCORE=.*$/SCORE=\r/g' > newfile
cat $file | sed 's/SCORE=.*$/SCORE=/' > newfile

That's also a useless use of cat, by the way, so:

sed 's/SCORE=.*$/SCORE=/' < "$file" > newfile
1 Like

awk alternative.

awk '$0 ~ /^SCORE=/ { $0="SCORE=" } 1' scores.txt >newfile