LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 05-25-2006, 04:39 PM   #1
mike9287
Member
 
Registered: May 2006
Location: North England
Distribution: Mandriva
Posts: 64

Rep: Reputation: 15
Help with a basic UNIX script


Hi there!

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.



TIA

Mike

BTW, I am using the BASH shell and VI editor.
 
Old 05-25-2006, 04:54 PM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,750

Rep: Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928
Per the rules:
Do not expect LQ members to do your homework - you will learn much more by doing it yourself.

Where are you needing help?
 
Old 05-25-2006, 05:02 PM   #3
mike9287
Member
 
Registered: May 2006
Location: North England
Distribution: Mandriva
Posts: 64

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by michaelk
Per the rules:
Do not expect LQ members to do your homework - you will learn much more by doing it yourself.

Where are you needing help?

Hi,

I have tried the code below:

echo this is the line I want to insert >> /home/(myname)/script1

but when I do this it puts the line at the end of the file, is using the above even scripting? should I send the file to VI first?

Mike

P.S. I take your point also, I shouldnt have been so lazy and posted the way I did.
 
Old 05-25-2006, 05:36 PM   #4
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,499
Blog Entries: 2

Rep: Reputation: 68
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.

The linux commands are with you.

good luck,
 
Old 05-29-2006, 07:56 AM   #5
mike9287
Member
 
Registered: May 2006
Location: North England
Distribution: Mandriva
Posts: 64

Original Poster
Rep: Reputation: 15
Hi there, thanks to those who replied.

I came up with this script which seems to work, s1 being the pre-written file I needed to append too.

echo "The title" > tempfile
cat s1 >> tempfile
cat tempfile > s1

Is there a smarter way to do this?

Thanks again

EDIT: forgot to add "rm tempfile" as last line of script.

Mike

Last edited by mike9287; 05-29-2006 at 07:58 AM.
 
Old 05-29-2006, 08:18 AM   #6
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
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
Code:
awk 'BEGIN { print "your_extra_line"; print; }' s1 > tempfile
mv tempfile s1
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.
 
Old 05-29-2006, 09:11 AM   #7
mike9287
Member
 
Registered: May 2006
Location: North England
Distribution: Mandriva
Posts: 64

Original Poster
Rep: Reputation: 15
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.

I amended my script to this:

echo "The title" > tempfile
cat s1 >> tempfile
move tempfile s1

Thanks!

Last edited by mike9287; 05-29-2006 at 09:18 AM.
 
Old 05-29-2006, 05:14 PM   #8
mike9287
Member
 
Registered: May 2006
Location: North England
Distribution: Mandriva
Posts: 64

Original Poster
Rep: Reputation: 15
Anyone give me a clue on how to append text to the middle of a file?

regards

Mike
 
Old 05-29-2006, 06:05 PM   #9
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by mike9287
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.

I amended my script to this:

echo "The title" > tempfile
cat s1 >> tempfile
move tempfile s1

Thanks!
You might be able to get rid of using the temporary file by using pipes for redirection:
Code:
echo "The title" | cat - s1 > s1
The hyphen after cat tells it where to put the data from the pipe (which would normally be stdin).

Last edited by osor; 05-29-2006 at 06:07 PM.
 
Old 05-29-2006, 06:07 PM   #10
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by mike9287
Anyone give me a clue on how to append text to the middle of a file?

regards

Mike
A really inelegant way would involve the head and tail commands.
 
Old 05-30-2006, 02:26 AM   #11
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
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...
 
Old 05-30-2006, 02:56 AM   #12
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
Or plain old sed ...

If you need to insert after some text:
Code:
sed -i '/text after which to insert/i\
here comes the new text\
of several lines' textfile.txt

Or, if the line number is what you have
rather than content, e.g. line 5
Code:
sed -i '5i\
here comes the new text\
of several lines' textfile.txt

Cheers,
Tink
 
Old 05-30-2006, 06:46 AM   #13
mike9287
Member
 
Registered: May 2006
Location: North England
Distribution: Mandriva
Posts: 64

Original Poster
Rep: Reputation: 15
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

Last edited by mike9287; 05-30-2006 at 06:49 AM.
 
Old 05-30-2006, 08:59 AM   #14
mike9287
Member
 
Registered: May 2006
Location: North England
Distribution: Mandriva
Posts: 64

Original Poster
Rep: Reputation: 15
I am not at home to try this code below but does it look likely that it will work?

echo "The middle" | sed -i 5i >> myexistingfile.txt

Regards

Mike
 
Old 05-30-2006, 01:29 PM   #15
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
Quote:
Originally Posted by mike9287
I am not at home to try this code below but does it look likely that it will work?

echo "The middle" | sed -i 5i >> myexistingfile.txt

Regards

Mike
Nope
sed -i tells sed to modify the file in-place, and the text to
be inserted comes behind the insert-command (i).

sed -i '5i\
The middle' myexistingfile.txt

is what you want to do.


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Basic commands in unix/linux sathish80 Linux - Newbie 5 03-24-2006 01:45 PM
unix shell script cxy0481 Programming 9 11-20-2005 08:15 AM
Basic Unix commands req dark_light Linux - Newbie 7 04-04-2002 06:33 PM
How to schedule unix script periodically from unix os level??? gopi_20us Programming 2 03-11-2002 06:45 AM
Basic Unix Program for Linux Daniel Programming 10 07-05-2001 12:17 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:22 PM.

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