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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
04-14-2004, 10:42 PM
|
#1
|
|
Member
Registered: Nov 2003
Location: USA
Distribution: Ubuntu
Posts: 102
Rep:
|
Inserting Text
Does anyone know if and how it would be possible to go through many files and add a few lines of text at a certain position in the file?
Weird I realize...but worth a shot...
Thanks...
|
|
|
|
04-15-2004, 01:01 AM
|
#2
|
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
I'm not the greatest at shell programming, but here's a shot at it. Others can feel free to verify or correct it:
echo `head -c 5000 original_file` this is new text `tail -c +5000 original_file` > new_file
That's assuming the new text should go after the 5000th character in the original_file.
|
|
|
|
04-15-2004, 07:33 AM
|
#3
|
|
Member
Registered: Nov 2003
Location: USA
Distribution: Ubuntu
Posts: 102
Original Poster
Rep:
|
I haven't tried it yet, but I was thinking, concatenate would almost work,
cat File1 File2
Prints the combination to standard output, but what I need it to do is print it to File2, which is a devastating error. I guess it writes the first file to ouput and clears everything that was there, and then if the second file is also the output, it reads what was written to output, and just adds it, effectively duplicating.
I could write it to a new file, but then i have to copy files after anyways, no gain.
I'll give that code a shot though.
Thanks.
|
|
|
|
04-15-2004, 10:23 AM
|
#4
|
|
Senior Member
Registered: Dec 2000
Location: Gothenburg, SWEDEN
Distribution: OpenSUSE 10.3
Posts: 1,028
Rep:
|
You can apend text to the end of a file easily with the redirection >>
echo 'Text to be inserted' >> file.txt
|
|
|
|
04-15-2004, 11:11 AM
|
#5
|
|
Member
Registered: Mar 2004
Location: Denmark
Distribution: Gentoo, Slackware
Posts: 333
Rep:
|
If you want to do something more advanced you can read up on sed.
Examples of what sed can do:
insert Hello World just before line 3:
sed "3iHello World" file > output
insert Hello World just before any line containing the string "foo":
sed "/foo/iHello World" file > output
You get the idea... try 'man sed'
Regards,
Mark.
|
|
|
|
04-15-2004, 03:45 PM
|
#6
|
|
Member
Registered: Nov 2003
Location: USA
Distribution: Ubuntu
Posts: 102
Original Poster
Rep:
|
sed looks like it could be a lot of fun and potentially very powerful.
BUT, the man page is about as readable as a greek dictionary.
I'm attempting to look for a bit of a tutorial...anyone know of one?
Thanks.
|
|
|
|
04-15-2004, 04:15 PM
|
#7
|
|
Moderator
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,903
|
Quote:
Originally posted by wildcat22
...the man page is about as readable as a greek dictionary.
|
Nothing wrong with greek dictionaries ...
andra'gathon alaethos men genestai chalepon...
Get a hold of Ellie Quigley's book
UNIX Shells by Example
She covers sed & awk in a pretty pictorial
way, describing features and their use very
well ...
Cheers,
Tink
|
|
|
|
04-15-2004, 05:37 PM
|
#8
|
|
LQ Addict
Registered: Dec 2003
Location: Maine, USA
Distribution: Slackware/SuSE/DSL
Posts: 1,320
Rep:
|
|
|
|
|
04-25-2004, 07:51 AM
|
#9
|
|
Senior Member
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802
Rep:
|
#!/bin/bash
#WARNING: This is untested code.
for FILE in file1 file2 file3
do
dd if=$FILE of=tempfile bs=1 count=100
cat insertfile >> tempfile
dd if=$FILE of=tempfile2 bs=1 skip=100
cat tempfile2 >> tempfile
mv $FILE $FILE.bak
mv tempfile $FILE
rm -f tempfile2
done
I'm sure there is a utility just for this sort of thing but right off the top of my head I come up with this kludge.
You take the first 100 bytes of $FILE and put in tempfile. Then add insertfile. Then add the rest of $FILE. Rename everything and get rid of the mess you made. To insert somewhere else in the file just change count and skip.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 04:00 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|