sed command removes lines in file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed command removes lines in file
# 1  
Old 06-23-2011
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?
Code:
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

Last edited by Franklin52; 06-23-2011 at 05:04 PM.. Reason: Please use code tags
# 2  
Old 06-23-2011
$ 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
Code:
'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.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 06-23-2011
sed command to replace a value

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?

Code:
[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

Last edited by Franklin52; 06-23-2011 at 05:04 PM.. Reason: Please use code tags
# 4  
Old 06-23-2011
should be good enough with
Code:
sed 's/SCORE=.*/SCORE=/'

This User Gave Thanks to neutronscott For This Post:
# 5  
Old 06-23-2011
Quote:
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.
You're welcome. You might want to read it too.
Quote:
Originally Posted by Corona688
/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.
Quote:
Originally Posted by Corona688
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.
Code:
cat $file | sed 's/SCORE=.*$/SCORE=\r/g' > newfile

Code:
cat $file | sed 's/SCORE=.*$/SCORE=/' > newfile

That's also a useless use of cat, by the way, so:
Code:
sed 's/SCORE=.*$/SCORE=/' < "$file" > newfile

This User Gave Thanks to Corona688 For This Post:
# 6  
Old 06-24-2011
awk alternative.
Code:
awk '$0 ~ /^SCORE=/ { $0="SCORE=" } 1' scores.txt >newfile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed or awk removes attachment in email

Hi We have a requirement to send email using shell script.email should have html body and pdf attachment. We used uuencode for attaching files and sendmail option to acheive and it is working fine. However custoemr wants to make body of email slightly dynamic. E.g dear customer in html file... (3 Replies)
Discussion started by: Harish7586
3 Replies

2. UNIX for Dummies Questions & Answers

sed command to Insert a line before the last four lines of the file

By using sed command, How to insert a new line before the last four lines of the file. Old Line Old Line NEW LINE! Old Line Old Line Old Line Old Line (8 Replies)
Discussion started by: wridler
8 Replies

3. UNIX for Dummies Questions & Answers

Sort command -u removes non-unique item

I am wondering why the sort command removes the volume with 0 in it, but orders everything else. I have a list like this: cat list1 /vol/list_vol0 /vol/list_vol1 /vol/list_vol2 /vol/list_vol3 /vol/list_vol4 /vol/list_vol5 /vol/list_vol6 /vol/list_vol7 /vol/list_vol8 /vol/list_vol9... (2 Replies)
Discussion started by: newbie2010
2 Replies

4. Shell Programming and Scripting

Sed: removes \ from text which causes issues

Hi all, Hoping someone hoping someone might be able to help. i've got the following sed command which i'm using in a bash script that i'm trying to use to insert a new line into an already existing file so i don't have to manually enter it when setting stuff up. the existing script test2/3 are... (3 Replies)
Discussion started by: springs2
3 Replies

5. Shell Programming and Scripting

sed command to condense two lines into one

My eyes glaze over trying to understand multiline sed commands, so please forgive me if this has been asked before. I'd like to condense the following two lines: fooPATTERN1 PATTERN2bar into the following one line: foobar How is that done in sed? (7 Replies)
Discussion started by: scolfax
7 Replies

6. UNIX for Advanced & Expert Users

perl/sed -i removes link

hello, is it a behavior of or that "-i" removes unix link . example : i create a file "src_file" and link it to "link_file" and then i start "perl -i" the link is removed. does another option exists to change content of a file without temporary files ? UNIX-Version: HP-UX and... (2 Replies)
Discussion started by: bora99
2 Replies

7. UNIX for Dummies Questions & Answers

Sed to remove only first line erroneously removes last line too

Hello everyone, This is my first posting. I have read the rules of this forum. I have searched many various threads and haven't found one that applies to my situation or suggestions to fix the issue. I do appreciate the help. I am trying to execute a basic UNIX script in a Solaris... (4 Replies)
Discussion started by: dqrgk0
4 Replies

8. Shell Programming and Scripting

awk/sed Command : Parse parameter file / send the lines to the ksh export command

Sorry for the duplicate thread this one is similar to the one in https://www.unix.com/shell-programming-scripting/88132-awk-sed-script-read-values-parameter-files.html#post302255121 Since there were no responses on the parent thread since it got resolved partially i thought to open the new... (4 Replies)
Discussion started by: rajan_san
4 Replies

9. Shell Programming and Scripting

sed command to get the total lines

hi, I am using the following code to get the first field of all the lines in a file using sed command.But its not printing the last line.Why it is so and how can i read the different fields of each line ? code: for i in $(sed = filename.txt | sed 'N;s/\n/ /' | cut -d " " -f1) do val1=`sed... (5 Replies)
Discussion started by: Sharmila_P
5 Replies

10. Shell Programming and Scripting

using sed command to replace multiple lines

the file contains the follwoing lines /* * Copyright (C) 1995-1996 by XXX Corporation. This program * contains proprietary and confidential information. All rights reserved * except as may be permitted by prior written consent. * * $Id: xxx_err.h,v 1.10 2001/07/26 18:48:34 zzzz $ ... (1 Reply)
Discussion started by: radha.kalivar
1 Replies
Login or Register to Ask a Question