Linux - DesktopThis forum is for the discussion of all Linux Software used in a desktop context.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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
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
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.