LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 03-03-2003, 08:42 PM   #1
JStew
Member
 
Registered: Oct 2002
Location: North Atlanta
Distribution: LFS
Posts: 229

Rep: Reputation: 30
Appending a string to a file


what command can i use that would add the string, "#! /usr/bin/perl -w" to a file?
 
Old 03-03-2003, 09:10 PM   #2
Crashed_Again
Senior Member
 
Registered: Dec 2002
Location: Atlantic City, NJ
Distribution: Ubuntu & Arch
Posts: 3,503

Rep: Reputation: 57
Something like this would append a string to a file:

echo "#! /usr/bin/perl -w" >> test.txt

Carefull though. If you use one > instead of two >> then it will erase everything and just write that one line.
 
Old 03-03-2003, 09:27 PM   #3
JStew
Member
 
Registered: Oct 2002
Location: North Atlanta
Distribution: LFS
Posts: 229

Original Poster
Rep: Reputation: 30
i will let you know if this helps... i wrote this script for my linux box and i am viewing this from windows :-(
 
Old 03-03-2003, 09:35 PM   #4
neo77777
LQ Addict
 
Registered: Dec 2001
Location: Brooklyn, NY
Distribution: *NIX
Posts: 3,704

Rep: Reputation: 56
it will do no good if the file isn't empty, it will append the line at the end of file, what would you need to do if you are a perl inclined person is open the file fo reading and read all the contents of the file into an array, then close it and open it again for writing and print #!/usr/bin/perl -w first and then all the every line of the array, something like this should work
Code:
 #!/usr/bin/perl
open (READ,"filename");
@lines=<READ>;
close READ;
open (FILE,">filename") or die "$!:\n";
print FILE "#!/usr/bin/perl -w\n";
foreach $line (@lines){
print FILE $line;
}
close FILE;
 
Old 03-03-2003, 09:37 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Probably won't do what you want ...

it appends the line, not prepends...

But there's vi, and with a little luck
it will even work in the sucky windows
telnet emulation :}

Cheers,
Tink
 
Old 03-03-2003, 09:42 PM   #6
Crashed_Again
Senior Member
 
Registered: Dec 2002
Location: Atlantic City, NJ
Distribution: Ubuntu & Arch
Posts: 3,503

Rep: Reputation: 57
Oh very neat neo77777. I figured that the line "#!/usr/bin/perl -w" was meant to go in the beginning of the file but JStew did say "How do I APPEND to a file"

Its always neat to see some nice perl scripting. Thanks.
 
Old 03-03-2003, 09:47 PM   #7
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
sed -e 'i#!/usr/bin/perl -w' <old_file> > <new_file>

Cheers,
Tink
 
Old 03-03-2003, 10:18 PM   #8
cuckoopint
Member
 
Registered: Feb 2003
Distribution: Debian
Posts: 797

Rep: Reputation: 30
Quote:
Originally posted by Tinkster
sed -e 'i#!/usr/bin/perl -w' <old_file> > <new_file>
perl was soooo overkill for this task.
 
Old 03-04-2003, 06:01 AM   #9
JStew
Member
 
Registered: Oct 2002
Location: North Atlanta
Distribution: LFS
Posts: 229

Original Poster
Rep: Reputation: 30
"perl was soooo overkill for this task."- cuckoopoint

oh... but this question is part of a bigger plan...

append...input... it's all the same damn thing :-)
 
Old 03-04-2003, 07:48 AM   #10
arnold
Member
 
Registered: Dec 2002
Posts: 226

Rep: Reputation: 30
no temp/intermediate files:

ex FILE <<!
0i
#! /usr/bin/perl -w
.
wq
!
 
Old 03-04-2003, 11:17 AM   #11
neo77777
LQ Addict
 
Registered: Dec 2001
Location: Brooklyn, NY
Distribution: *NIX
Posts: 3,704

Rep: Reputation: 56
Why I provided perl script is for the assumption that there are more than one file that needed editing, so dancing around this perl script you can edit multiple files in one shot, as for straight unix way to do it I agree sed is the most robust, and you can always use old cat to concatinate two files one being a single string file containing #!/usr/bin/perl -w and another is the perl script that required editing (file1).
echo '#!/usr/bin/perl -w'>> file2
cat file1 >> file2
 
Old 03-04-2003, 03:07 PM   #12
arnold
Member
 
Registered: Dec 2002
Posts: 226

Rep: Reputation: 30
"more than one file that needed editing"

for f in FILE_LIST
do
ex $f <<!
0i
#! /usr/bin/perl -w
.
wq
!
done
 
Old 03-04-2003, 03:23 PM   #13
JStew
Member
 
Registered: Oct 2002
Location: North Atlanta
Distribution: LFS
Posts: 229

Original Poster
Rep: Reputation: 30
Tinkster wins! The sed solution is by far the most efficient one. Thanks for the help Tinkster!
 
Old 03-04-2003, 03:48 PM   #14
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Cheers mate, pleasure ;)

Tink
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Substitute String or line in a file dimsh Linux - Newbie 4 09-21-2005 03:26 AM
Sed(?); Appending a comma-delineated file ice_hockey Linux - General 2 05-27-2005 08:42 AM
SSL VPN appending /etc/hosts file zajelo3 Linux - Security 0 02-06-2005 06:31 PM
Appending current date and time to a file frankietomatoes Linux - General 5 11-18-2002 02:09 PM
sending a mail appending a file saavik Linux - Newbie 4 07-12-2002 03:50 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:00 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