LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Desktop (http://www.linuxquestions.org/questions/linux-desktop-74/)
-   -   write text to specific location in file (http://www.linuxquestions.org/questions/linux-desktop-74/write-text-to-specific-location-in-file-623340/)

mcbenus 02-23-2008 01:32 PM

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

gilead 02-23-2008 01:46 PM

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?

pixellany 02-23-2008 02:01 PM

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

mcbenus 02-28-2008 06:40 AM

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 (Post 3067476)
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



All times are GMT -5. The time now is 02:05 AM.