LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop
User Name
Password
Linux - Desktop This forum is for the discussion of all Linux Software used in a desktop context.

Notices


Reply
  Search this Thread
Old 02-23-2008, 01:32 PM   #1
mcbenus
LQ Newbie
 
Registered: Feb 2007
Posts: 24

Rep: Reputation: 15
write text to specific location in file


Hi All,

I need to write text (a string) into a specific location in an existing text file (say, line 30 row 25). I am relatively new to unix, and so far have used only the > and >> symbols.

Alternatively, I can manually type text in the desired location, and then use a command to replace that text with the desired text.

Any help is greatly appreciated.

Ben
 
Old 02-23-2008, 01:46 PM   #2
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,141

Rep: Reputation: 168Reputation: 168
If you wanted to insert a string at line 35 of the file test.file:
Code:
sed -i '
35 i\
This text gets inserted at line 35
' test.file
Does that do what you wanted?
 
Old 02-23-2008, 02:01 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
The implication is that you need to do this on lots of files, or you need to otherwise do it many times. If you can describe the problem more completely, you'll get better help.

Here's one way to approach it:

sed '30 s/\(.\{24\}\)/\1 new stuff /' oldfile>newfile

Translation:
Read the file "oldfile", On line 30, replace the first 24 characters with themselves + the string " new stuff ", write the resulte to "newfile".

To help understand this construct, read up on "backreferences". In this example, this:
\(.\{24\}\)
remembers the 1st 24 characters, and this:
\1
re-inserts them.

If you don't like typing all the backslashes, use the -r option to turn on extended regexes:

sed -r '30 s/(.{24})/\1 new stuff /' oldfile>newfile
 
Old 02-28-2008, 06:40 AM   #4
mcbenus
LQ Newbie
 
Registered: Feb 2007
Posts: 24

Original Poster
Rep: Reputation: 15
didn't work

thanks for the advice - I have tried it on my file but it did not do anything. i am using cygwin on windows XP, could that be the reason? (I do have sed).

I am not familiar with sed, but saw the following somewhere:

sed 's/oldtext/newtext/' <old >new

to replace text in a file, which i could use.

my overall problem is - i am writing a script that runs a program that needs a text input. the program runs several times and the input file has to be changed slightly between each run. this is why i wanted to insert text at a specific location in a text file. what i think i can do now is right a master input file, copy it, change the text where needed using 'sed s', and so on. it's not the most elegant way, but that's my second priority...

thanks for introducing me to the existence of sed.

Best,
Ben


Quote:
Originally Posted by pixellany View Post
The implication is that you need to do this on lots of files, or you need to otherwise do it many times. If you can describe the problem more completely, you'll get better help.

Here's one way to approach it:

sed '30 s/\(.\{24\}\)/\1 new stuff /' oldfile>newfile

Translation:
Read the file "oldfile", On line 30, replace the first 24 characters with themselves + the string " new stuff ", write the resulte to "newfile".

To help understand this construct, read up on "backreferences". In this example, this:
\(.\{24\}\)
remembers the 1st 24 characters, and this:
\1
re-inserts them.

If you don't like typing all the backslashes, use the -r option to turn on extended regexes:

sed -r '30 s/(.{24})/\1 new stuff /' oldfile>newfile
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
location of a text file hadyy Linux - General 13 12-19-2007 01:34 PM
Read specific lines from a text file chobin Programming 8 06-14-2006 11:14 AM
SED - display text on specific line of text file 3saul Linux - Software 3 12-29-2005 04:32 PM
Untar File to a specific location ! Libu Linux - General 3 12-19-2005 07:14 PM
How to find and change a specific text in a text file by using shell script Bassam Programming 1 07-18-2005 07:15 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop

All times are GMT -5. The time now is 01:38 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
Open Source Consulting | Domain Registration