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.
I need to write a basic script based on the following criteria:
First of all I create a file called "script1" using the cat command and then I add a few lines to it and save it.
Next, I need to write a script to do the following:
a script which takes to arguments, the first being a line of text, the second being the newly created file (script1). The script should take the first argument and insert it into the very top of the file (script1) name in your seconds argument. The file must then be saved and keep its original name.
There is no way to add lines at top of a file.
You need to do a trick:
Create a new file, with the string on it, and append to the end of that file, the contents of the other file.
After that, rename the files.
1. Use command line parameters, rather than hardcoded stuff. But you probably figured that one out yourself.
2. Don't use "cat tempfile > s1", as this may yet again read all lines in the file tempfile and print them into file "s1" (line by line). Using "mv" instead of cat (ie "mv tempfile s1") may be slightly faster, especially when you're dealing with big text files.
3. There are plenty of alternatives: Perl scripting, dd, sed, awk, ... but those can be a little tricky and may not be as fast as your current approach.
ie
Haven't tested it, but it could work. However, awk will still read the entire file s1, line-by-line, so it's doubtful that it'll be faster than your "cat >>" approach.
Thanks for the info, is it possible to move text like i did but instead plant it in the middle of a file? I havent learned about awk, sed and greb etc yet. Thats later on in the course.
Thanks for the info, is it possible to move text like i did but instead plant it in the middle of a file? I havent learned about awk, sed and greb etc yet. Thats later on in the course.
A combination of one or more "cat", "grep", "sed", "head" and/or "awk" commands should do the trick.
A Perl scripting might help too.
It really depends on what you want exactly.
ie a simple Shell script solution would be:
Code:
#!/bin/bash
insertAt=$1; #line number after which we want to insert text
source=$2 #file in which we want to insert lines at position $insertAt.
insert=$3 #file with text we are going to insert into $source.
head -${insertAt} > /tmp/temp1 #get the first part of the file
lines=`wc -l $source`;
linesLeft=$(($lines - $insertAt));
tail -${linesLeft} $source > /tmp/temp2
cat /tmp/temp1 $insert /tmp/temp2 > $2
rm /tmp/temp1 /tmp/temp2
It'll need some error & input checks for robustness and it's not very elegant, but maybe it'll work.
In short: play around with the commands we gave you (read their man pages for starters) and you'll see the light someday...
Hi, thanks for the replies, do you think should I will need to use the "wc -l" and then take the result and divide it by 2 to get half way down the page and then insert from there? is that possible with the right code and it is practical or should I look at other options.
Thanks again, your help is much appreciated!
Mike
EDIT: oops, had not read the 2 posts made recently by timmeke and tinster. Thanks
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.