LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-13-2010, 03:44 PM   #1
nushki
LQ Newbie
 
Registered: Mar 2009
Posts: 26

Rep: Reputation: 0
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
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 12-13-2010, 03:57 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
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.
 
2 members found this post helpful.
Old 12-13-2010, 04:11 PM   #3
nushki
LQ Newbie
 
Registered: Mar 2009
Posts: 26

Original Poster
Rep: Reputation: 0
thank you very much, it works!!!!!!!!!!!!!!!!!!!
 
Old 12-13-2010, 04:53 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
You're welcome!
 
Old 12-14-2010, 01:12 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,629

Rep: Reputation: 2944Reputation: 2944Reputation: 2944Reputation: 2944Reputation: 2944Reputation: 2944Reputation: 2944Reputation: 2944Reputation: 2944Reputation: 2944Reputation: 2944
Please mark as SOLVED once you have a working solution
 
Old 12-29-2010, 01:32 AM   #6
bbachu
LQ Newbie
 
Registered: Dec 2010
Posts: 5

Rep: Reputation: 0
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.
 
Old 01-27-2012, 05:36 AM   #7
Rostek
LQ Newbie
 
Registered: Jan 2012
Posts: 1

Rep: Reputation: Disabled
...and if I need to keep even the characters in the previous 20?

thx
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sed replacing a specific character with a specific number ieatbunnies Linux - Newbie 2 11-04-2010 11:14 AM
replace specific character after specific line by awk Syed Tarique Moin Programming 2 07-19-2010 02:47 PM
Using sed/awk to replace a string at a given position in anoopvraj Linux - Newbie 6 05-30-2009 08:59 AM
Spacing out on simple sed task: find position of substring in a variable? lumix Linux - General 2 10-16-2008 04:07 PM
SED - replace text in file on specific line 3saul Linux - Software 1 03-04-2006 08:01 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration