LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 04-14-2004, 10:42 PM   #1
wildcat22
Member
 
Registered: Nov 2003
Location: USA
Distribution: Ubuntu
Posts: 102

Rep: Reputation: 15
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...
 
Old 04-15-2004, 01:01 AM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
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.
 
Old 04-15-2004, 07:33 AM   #3
wildcat22
Member
 
Registered: Nov 2003
Location: USA
Distribution: Ubuntu
Posts: 102

Original Poster
Rep: Reputation: 15
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.
 
Old 04-15-2004, 10:23 AM   #4
ugge
Senior Member
 
Registered: Dec 2000
Location: Gothenburg, SWEDEN
Distribution: OpenSUSE 10.3
Posts: 1,028

Rep: Reputation: 45
You can apend text to the end of a file easily with the redirection >>

echo 'Text to be inserted' >> file.txt
 
Old 04-15-2004, 11:11 AM   #5
Muzzy
Member
 
Registered: Mar 2004
Location: Denmark
Distribution: Gentoo, Slackware
Posts: 333

Rep: Reputation: 30
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.
 
Old 04-15-2004, 03:45 PM   #6
wildcat22
Member
 
Registered: Nov 2003
Location: USA
Distribution: Ubuntu
Posts: 102

Original Poster
Rep: Reputation: 15
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.
 
Old 04-15-2004, 04:15 PM   #7
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,986
Blog Entries: 11

Rep: Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880
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
 
Old 04-15-2004, 05:37 PM   #8
mikshaw
LQ Addict
 
Registered: Dec 2003
Location: Maine, USA
Distribution: Slackware/SuSE/DSL
Posts: 1,320

Rep: Reputation: 45
http://space.xs4all.nl/OR_UNIX/sedawk/index.htm
 
Old 04-25-2004, 07:51 AM   #9
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 46
#!/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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Shortcut keys for inserting text/code in Linux Horos23 Programming 1 02-03-2005 03:50 PM
inserting the data thru php in a text file suchi_s Programming 5 02-02-2005 03:28 AM
Gimp-1.3 inserting text nadine.mauch Linux - Software 0 07-26-2004 03:07 AM
inserting/deleting characters into a text file ananthbv Programming 7 07-13-2004 11:40 PM
inserting text into a file DavidPhillips Programming 5 08-15-2003 04:53 PM


All times are GMT -5. The time now is 11:52 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration