Computing.Net > Forums > Unix > sed question

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

sed question

Reply to Message Icon

Name: Rusty Scalf
Date: September 3, 2003 at 13:38:29 Pacific
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




Sponsored Link
Ads by Google

Response Number 1
Name: David Perry
Date: September 4, 2003 at 04:46:10 Pacific
Reply:

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'


0

Response Number 2
Name: Frank
Date: September 4, 2003 at 04:50:34 Pacific
Reply:

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


0

Response Number 3
Name: James Boothe
Date: September 4, 2003 at 07:23:48 Pacific
Reply:

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


0

Response Number 4
Name: James Boothe
Date: September 4, 2003 at 07:28:55 Pacific
Reply:

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


0

Response Number 5
Name: Rusty Scalf
Date: September 4, 2003 at 10:36:21 Pacific
Reply:

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


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: sed question

Sed question www.computing.net/answers/unix/sed-question/6826.html

general sed question www.computing.net/answers/unix/general-sed-question/3935.html

sed question www.computing.net/answers/unix/sed-question/4207.html