Computing.Net > Forums > Unix > spefic SED replace command

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.

spefic SED replace command

Reply to Message Icon

Name: matt_gibson
Date: March 11, 2004 at 04:02:01 Pacific
OS: UNIX
CPU/Ram: P3 128mb
Comment:

HI all

I need to replace the 23rd character on all lines beginning with P , with a blank character. Ive heard that the SED command is the best option, Can anybody help with the specific command line that would perform this operation.



Sponsored Link
Ads by Google

Response Number 1
Name: aigles
Date: March 11, 2004 at 06:03:16 Pacific
Reply:

The solution with sed :

sed 's/^\(P.\{21\}\)./\1 /' input_file

Jean-Pierre.


0

Response Number 2
Name: nails
Date: March 11, 2004 at 10:54:41 Pacific
Reply:

Jean-Pierre:

Good solution. Can you explain what you are doing for the benefit of us non-sed experts?

Thanks!

Nails


0

Response Number 3
Name: Dlonra
Date: March 11, 2004 at 17:57:52 Pacific
Reply:

i'm like in awe of Jean-Pierre's solution. I had to
man, info and browse to understand it. i'd like to try to explain so I will really learn it.
The \ are shell escapes (except \1),so consider
sed 's/^(P.{21})./\1 /' input_file

(reg_exp) is a "grouping" of a regular expression pattern match and \1 is the value of match 1 - match 2 would be the 2nd set of parens, if there were any.

r{n} is an "interval expression", so the preceding regular expression r is repeated n times. In this case: .{21} is 21 of any character.

^P.{21} is the 1st 22 characters of lines beginning with P

Note the "." in })./\1 - this is the 23rd character- it is not in the () grouping.

So sed 's/^\(P.\{21\}\)./\1 /' input_file says replace the 1st 23 characters of lines that start "P" with the 1st 22 characters + space

very neat


0

Response Number 4
Name: aigles
Date: March 12, 2004 at 00:42:04 Pacific
Reply:

Good explanation Dlonra.

sed is powerful, but very cryptic

Jean-Pierre.


0

Response Number 5
Name: Dlonra
Date: March 12, 2004 at 05:31:56 Pacific
Reply:

correction:
in sed 's/^\(P.\{21\}\)./\1 /' input_file
\ are not "shell escapes", they are "sed escapes" so that sed will recognize them as special characters, e.g.:
echo ab{cd|sed 's/{//' -> abcd



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: spefic SED replace command

need help for sed www.computing.net/answers/unix/need-help-for-sed/7979.html

Sed - replace block of text - but h www.computing.net/answers/unix/sed-replace-block-of-text-but-h/7063.html

sed:replace whitespace with newline www.computing.net/answers/unix/sedreplace-whitespace-with-newline/7612.html