LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 01-03-2015, 01:19 PM   #1
elliot01
Member
 
Registered: Jun 2009
Location: UK
Distribution: CentOS / RedHat
Posts: 89

Rep: Reputation: 16
How can I overwrite characters in a file?


Hi all,

I am looking for a way to overwrite (overtype?) characters in a file at a certain position programatically (i.e. from a Bash script).

For instance, say I have a file containing one line:
Code:
abc def ghi jkl mno
I want to script a process which writes '1234' to line 1, position 9 of my file. So the resulting file would be:
Code:
abc def 1234jkl mno
I have searched around and seen various suggestions of sed, awk, printf, but all the answers seem to be inserting text or find/replacing, thus resulting in:
Code:
abc def 1234 jkl mno
Imagine you load up a text processor and change the input mode from insert to overwrite - that's what I'm after, but programatically.

I'm sure I'm just wording my Google searches wrong, as I assumed this would be an easy task. Any advice or suggestions highly appreciated!

Cheers

Elliot

Last edited by elliot01; 01-04-2015 at 06:41 AM.
 
Old 01-03-2015, 02:08 PM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,140

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
How about

Code:
perl -pe '$a="1234"; substr($_,9,length($a))=$a if $. == 1' <myinput.file >tmpfile
mv -f tmpfile myinputfile
 
2 members found this post helpful.
Old 01-04-2015, 06:41 AM   #3
elliot01
Member
 
Registered: Jun 2009
Location: UK
Distribution: CentOS / RedHat
Posts: 89

Original Poster
Rep: Reputation: 16
Boom! Perfect!

Thank you very much, smallpond
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Print to File configuration - do not overwrite previous file taylorkh Linux - Software 2 11-12-2014 04:33 PM
can't overwrite a file because it's being used? jorx Linux - Software 5 02-17-2013 09:52 PM
How do you overwrite the contents of a file without changing the file size? lothario Linux - Software 3 07-13-2010 08:43 AM
using sed to insert line into file and overwrite the current file jadeddog Programming 3 06-11-2009 07:14 PM
How to overwrite/change characters in the same field with C. slzckboy Programming 6 05-23-2005 05:07 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:10 PM.

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
Open Source Consulting | Domain Registration