Computing.Net > Forums > Unix > sed question

sed question

Reply to Message Icon

Original Message
Name: Rusty Scalf
Date: September 3, 2003 at 13:38:29 Pacific
Subject: sed question
OS: windows2000
CPU/Ram: Pent IV 3.06/1 gig
Comment:

Hello,

I know this is a Unix page and I am using sed for windows. But I don't see another more appropriate place for my question.

I have a file in which certain lines, spaced variably apart (from 4 to 53 lines apart) are in the form

100902,1,

in which 100902 can be any string of six digits followed by ,1,

I want to replace all these lines by

end
100902 (in the above example)

or

end
123456 if the original lines is 123456,1,


make sense?

I am using sed -f subrd.txt where subrd.txt is a script

Thanks,

Rusty Scalf
Calif Dept of Health Services



Report Offensive Message For Removal


Response Number 1
Name: David Perry
Date: September 4, 2003 at 04:46:10 Pacific
Subject: sed question
Reply: (edit)

You have lines that look like "100902,1," and you want to replace this line with the two lines that look like

"end
100902 "
?

Is there any more to these lines or is that the whole line?

How about this:

sed -e "s/\(^[0-9]\{6\}\),1,/end#\1/" datafile.txt | tr '#' '\012'


Report Offensive Follow Up For Removal

Response Number 2
Name: Frank
Date: September 4, 2003 at 04:50:34 Pacific
Subject: sed question
Reply: (edit)

Hi Rusty,

I am not sure if I got your question, but if you only would like to remove the first occurence of ,1, you can use
sed 's/,1,//' input.txt

Hope it helps.

No RISK no fun
Frank


Report Offensive Follow Up For Removal

Response Number 3
Name: James Boothe
Date: September 4, 2003 at 07:23:48 Pacific
Subject: sed question
Reply: (edit)

A caution on David's solution is that all # characters will get changed to newlines by the tr command, not just the ones inserted by sed.

It's unfortunate that sed cannot include a newline in its substitution strings. I tried several ways to do this.

I propose this solution:

sed -e "/^[0-9]\{6\},1,/!b" \
    -e "s/,1,$//;h;s/.*/end/;G" infile

For a non-targeted line, branch to end with no modifications to that line.

For a targeted line:
   take ,1, off the end
   store in hold buffer
   change main buffer to "end"
   append hold buffer to main buffer


Report Offensive Follow Up For Removal

Response Number 4
Name: James Boothe
Date: September 4, 2003 at 07:28:55 Pacific
Subject: sed question
Reply: (edit)

And I need to make sure that pattern ends the line, so this amended code includes an end-of-line anchor:

sed -e "/^[0-9]\{6\},1,$/!b" \
    -e "s/,1,$//;h;s/.*/end/;G" junk.txt


Report Offensive Follow Up For Removal

Response Number 5
Name: Rusty Scalf
Date: September 4, 2003 at 10:36:21 Pacific
Subject: sed question
Reply: (edit)

Thanks for your help. Late last night I stumbled upon someone's script, tried it, and it worked. Success by imitation.

It is in two steps:

s/.*,1,/end\r\n&\r\n/
s/,1,//g

the first finds any string preceeding a ,1, and replaces it with:

end
string,1,

the second deletes the ,1,

Seems the Gnu version of sed for Windows will insert carriage returns with \r\n

I will study your answers and use them as learning aids. My programming is far more primitive. I have been away from Unix (Suns) for seven years now and have forgotten most of what I knew.

Thanks again,

-Rusty


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: sed question 

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




How often do you use Computing.Net?

Every Day
Once a Week
Once a Month
This Is My First Time!


View Results

Poll Finishes In 3 Days.
Discuss in The Lounge