LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   replace specific position with sed (https://www.linuxquestions.org/questions/programming-9/replace-specific-position-with-sed-849983/)

nushki 12-13-2010 02:44 PM

replace specific position with sed
 
Hi.

i need to overwrite each line of the file starting from a specific position to the end of the line
example:
ATOM 981 N PRO B 159 55.662 7 1 1.47 0.75

i need to write " 1.00 0.00 " starting from 20position

i tried

sed -i 's/^\(.\{20\}\)*/\ 1.00 0.00 ' filename

but it doesnt work :"(

i'd appreciate your help

colucix 12-13-2010 02:57 PM

Option -r of sed let you write extended regular expressions without escaping special character, so that your line is more readable and less prone to errors. By the way, you missed a dot before the asterisk, to match the rest of the string after the 20th position. Furthermore, if you want to start overwriting from the 20th position you need to retain only the first 19 characters, as in:
Code:

sed -r 's/^(.{19}).*/\1 1.00 0.00 /' file
Hope this helps.

nushki 12-13-2010 03:11 PM

thank you very much, it works!!!!!!!!!!!!!!!!!!!

colucix 12-13-2010 03:53 PM

You're welcome! :)

grail 12-14-2010 12:12 AM

Please mark as SOLVED once you have a working solution :)

bbachu 12-29-2010 12:32 AM

I would like to replace 'xxxx' with 'yyyy' which is in a file xyz.csproj
not sure of what 'xxxx' is, it can be 3055, 4056, 7089 etc. I know it always appears at line # 5 and at character 50.

<Reference Include="System.Management" />
<Reference Include="System.ServiceProcess" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
<Reference Include="Utilities, Version=4.4.0.xxxx, Culture=neutral, PublicKeyToken=c944cec9fb0a3d4a, processorArchitecture=x86" />

Any help is highly appreciated.

Rostek 01-27-2012 04:36 AM

...and if I need to keep even the characters in the previous 20?

thx


All times are GMT -5. The time now is 07:55 AM.