Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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

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'

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.txtHope it helps.
No RISK no fun
Frank

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" infileFor 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

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

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,//gthe 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

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |