LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Help with a basic UNIX script (https://www.linuxquestions.org/questions/linux-newbie-8/help-with-a-basic-unix-script-448549/)

mike9287 05-25-2006 04:39 PM

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.

michaelk 05-25-2006 04:54 PM

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?

mike9287 05-25-2006 05:02 PM

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. :)

marozsas 05-25-2006 05:36 PM

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,

mike9287 05-29-2006 07:56 AM

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

timmeke 05-29-2006 08:18 AM

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.

mike9287 05-29-2006 09:11 AM

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!

mike9287 05-29-2006 05:14 PM

Anyone give me a clue on how to append text to the middle of a file?

regards

Mike

osor 05-29-2006 06:05 PM

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).

osor 05-29-2006 06:07 PM

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.

timmeke 05-30-2006 02:26 AM

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...

Tinkster 05-30-2006 02:56 AM

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

mike9287 05-30-2006 06:46 AM

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

mike9287 05-30-2006 08:59 AM

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

Tinkster 05-30-2006 01:29 PM

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

mike9287 05-30-2006 03:12 PM

Quote:

Originally Posted by Tinkster
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


Thanks for reply, I tried what you said and I get error: invalid option --i

Any idea what I might be doing wrong? I tried it on command line and in a file I made with cat then executed with 'sh'

Thanks

Mike

Tinkster 05-30-2006 03:33 PM

Quote:

Originally Posted by mike9287
Thanks for reply, I tried what you said and I get error: invalid option --i

Any idea what I might be doing wrong? I tried it on command line and in a file I made with cat then executed with 'sh'

Thanks

Mike

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

mike9287 05-31-2006 04:24 AM

Quote:

Originally Posted by Tinkster
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.

Thanks

Mike

Tinkster 05-31-2006 04:48 AM

Quote:

Originally Posted by mike9287
Hey Tink, Thanks again.

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


Cheers,
Tink

mike9287 05-31-2006 05:47 AM

Quote:

Originally Posted by Tinkster
What a shame - you work in the wrong place! ;}


^^ 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.

Mike

Tinkster 05-31-2006 01:21 PM

Quote:

Originally Posted by mike9287
^^ 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 ;}


Cheers,
Tink

mike9287 05-31-2006 04:28 PM

Quote:

Originally Posted by Tinkster
Good on yah.


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

sed: -e expression #1 char 10: unknown command `m'

my script was run from a file saved in cat as follow:

#!/bin/sh
sed '2i\
add me to the middle'
myfile > myfile.tmp
mv myfile.tmp myfile


Thanks

Mike

Tinkster 05-31-2006 04:41 PM

Quote:

Originally Posted by mike9287
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

sed: -e expression #1 char 10: unknown command `m'

my script was run from a file saved in cat as follow:

#!/bin/sh
sed '2i\
add me to the middle'
myfile > myfile.tmp
mv myfile.tmp myfile


Thanks

Mike

That's a weird error message to get there; the script IS faulty, but it
shouldn't run to completion.

You need to either a) join these lines
Code:

add me to the middle'
 myfile > myfile.tmp

to look like
Code:

add me to the middle' myfile > myfile.tmp
or b) add a backslash to the first one (like so)
Code:

add me to the middle' \
 myfile > myfile.tmp


Cheers,
Tink

mike9287 06-01-2006 02:46 PM

Hi again.

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

Anyone got any ideas what I am doing wrong?

Thanks for looking

Mike

mike9287 06-01-2006 03:51 PM

whooo hooo!!

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:)

Mike

Tinkster 06-01-2006 04:19 PM

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

mike9287 06-01-2006 04:38 PM

Quote:

Originally Posted by Tinkster
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.

Thanks again

Mike

http://www.chiark.greenend.org.uk/~sgtatham/putty/

Tinkster 06-01-2006 05:43 PM

Quote:

Originally Posted by mike9287
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.

Thanks again

Mike

http://www.chiark.greenend.org.uk/~sgtatham/putty/

Hi Mike ...

OK, now my last question :)

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

mike9287 06-02-2006 02:14 AM

Quote:

Originally Posted by Tinkster
Hi Mike ...

OK, now my last question :)

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.

ta
Mike


All times are GMT -5. The time now is 02:30 PM.