Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's 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.
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.
Probably using an ancient sed ... in that case try
Code:
#!/bin/sh
sed '5i\
The middle' myexistingfile.txt > myexistingfile.txt.tmp
mv myexistingfile.txt.tmp myexistingfile.txt
Cheers,
Tink
Hey Tink, Thanks again.
I am at work at the moment so I havent got a shell handy. I took a look into iserting with sed using options "i" and "a" and found "i" appends before the line and "a" after the line, this should mean I can use the same script with '1i\ in my first script as well as in this one.
what does "#!/bin/sh" actually tell the shell at the start of the script? just so I know for future reference.
I am at work at the moment so I havent got a shell handy.
What a shame - you work in the wrong place! ;}
Quote:
Originally Posted by mike9287
what does "#!/bin/sh" actually tell the shell at the start of the script? just so I know for future reference.
If a text-file is marked executable and has a
#!/path/to/executable
in it the bash will try to use the executable in there
to run the rest of the file as a script .
Normally you'd have to invoke a script (if it didn't
have the line) e.g. like
sh my_script
instead of just
./my_script
^^ Tell me about it.. I work in retail and I am not even supposed to use the Internet but only the companys Intranet, I want out of this job so bad, thats why I am here trying to learn UNIX, I want to become a Java programmer and need to learn UNIX and SQL before the training company will learn me Java.
^^ Tell me about it.. I work in retail and I am not even supposed to use the Internet but only the companys Intranet, I want out of this job so bad, thats why I am here trying to learn UNIX,
Good on yah.
Quote:
Originally Posted by mike9287
I want to become a Java programmer and need to learn UNIX and SQL before the training company will learn me Java.
Mike
Sorry, can't resist that one; it's "will teach me Java",
learn is what you do, teach is what they do ;}
Sorry, can't resist that one; it's "will teach me Java",
learn is what you do, teach is what they do ;}
Cheers,
Tink
Hehe, reminds me of my english teacher when people use to ask to lend a ruler rather than borrow
I tried the script and still get the invalid option --i. I thought it might have been the shell I
was using which is part of the "putty" program which I use through my windows account, I then logged into my Mandriva Korn shell and got the same kind of trouble, in Mandriva I get this
Hehe, reminds me of my english teacher when people use to ask to lend a ruler rather than borrow :)
Aye - English is quite a neat and rich language, and it makes
me cringe to see the natives butcher it like that ;}
We have a shop call Mr Rentals here where you can hire all
kinds of appliances (dish-washers, TV sets, ... ) ... they recently
added PC hardware to their repertoire, and the new add says
"Mr Rentals now rents computers!". I was so tempted to ask them
how much a day they'd pay for my athlon. ;}
Quote:
Originally Posted by mike9287
I tried the script and still get the invalid option --i. I thought it might have been the shell I
was using which is part of the "putty" program which I use through my windows account, I then logged into my Mandriva Korn shell and got the same kind of trouble, in Mandriva I get this
I tried the ways you mentioned but still dont get the desired results, I am on the part of the course which covers sed and grep etc and it say the following syntax should be used with sed:
sed [-n][-e]'command' file(s)
and command being as : [address]i\text //to insert
which is basically what you showed me and what I have tried to do. I tried this on the command line which as far as I can see follows the above syntax:
sed -e '2i\put me in the middle' myfile
also this
sed '2i\put me in the middle' myfile
and this
sed -e '2i\put me in the middle\'myfile
and this
sed -e '2i\put me in the middle' > myfile
as well as using sh to execute my script after writing it in cat. One of the errors I am getting now is:
sed -e expression #1, char 4: extra characters after command
I managed to figure this out, I found a guide on the IBM website and with what I have learned from this thread I came up with this script:
echo '2i\
This is the line I have been trying to insert for ages' > tempfile
sed -f tempfile myfile
rm tempfile
and bingo it works, thanks to all who helped and many thanks to Tinkster the human grammar and syntax compiler who is watching your every sentence - so beware! thanks mate, would have gave up long ago if it wasnt for your help
Well, I'm glad it works in the end, but I still don't understand
why it doesn't do the version I posted. It's perfectly sane, and
works on three different distros (SLES9, Debian Sarge and Slack 10.2)
I can get my paws on the moment, plus in solaris 10 and 8 :}
What distro are you using again, with which version of sed?
Well, I'm glad it works in the end, but I still don't understand
why it doesn't do the version I posted. It's perfectly sane, and
works on three different distros (SLES9, Debian Sarge and Slack 10.2)
I can get my paws on the moment, plus in solaris 10 and 8 :}
What distro are you using again, with which version of sed?
Cheers,
Tink
Hi, I have windows on one partition and Mandriva on the other, although I tried early on with Mandriva I have been using a program (as recommended by the Academy) called "Putty" through my windows account, the Academy uses Suse on there servers. Putty is a telnet/ssh client and I use it to connect to the academys servers. In manual it says "sed(1) 1998" which I would figure is probably and old version.
Hi, I have windows on one partition and Mandriva on the other, although I tried early on with Mandriva I have been using a program (as recommended by the Academy) called "Putty" through my windows account, the Academy uses Suse on there servers. Putty is a telnet/ssh client and I use it to connect to the academys servers. In manual it says "sed(1) 1998" which I would figure is probably and old version.
I assume that you're copying & pasting from a winDOHs
browser to the linux box via putty?
In that case, the screwed-up error message may well
stem from the DOS CR/LF combos :}
In the ssh session try "dos2unix your_script" with the
original content, and then run it again.
I have that idea that the ^M DOS files will have on each
end of a line may be confusing sed ;}
Cheers,
Tink
Yo, Nope I am just using the good ol' keyboard, the shell has a fit when I tried to cut and paste. I will try what you said when I get back home though, I can't understand what I was doing wrong either, every guide on sed I found basically operated as you suggested but on the IBM site they recommended a script file that you would call with the -f option, even the academy tutorials were the same as you explained and I couldnt get them to work either.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.