Link to home
Start Free TrialLog in
Avatar of FirstMan
FirstMan

asked on

Possible Solution to Question 1

i asked questions before with no knowledge of shell scripting/ variables. But i have read some information on it and have come up with a solution. I do not whelther it' s right or not but i need help with it.

This is the question
1)a)
Write a script named 'firstline' that takes two arguments. The first being a line of text, the second being your newly created file. The script should take the first argument and insert it into the very top (the first line) of the file named in your second argument.

Note! The file must retain the original name

1)b)
Take a copy (backup) and amend the above script so that it now inserts the first argument into the middle of the file. Please note that no matter how many lines there are in your script it should always place the line of text in the middle. Name this script 'middleline'.

Note! Again the original file must retain it original name

The situation is that i have a script and a solution and it doesnt input what i except

my solution

variable=$(wc -l file | cut -f1 -d' ')

my script

If I ruled the world Imagine that
I'd free all my sons,I love em love em baby
Black diamonds and pearls
Could it be, if you could be mine, we'd both shine
If I ruled the world
Still livin for today, in these last days and times

Help anyone please!!
ASKER CERTIFIED SOLUTION
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of FirstMan
FirstMan

ASKER

Hello thanks  for the tip and yeah it is homework. Before looking at this possible solution, i gathered some more knowledge and the program began to make sense. I was attemping to get a line of text and insert into a new file. i came up with this,

wc -l script | cut script"" -f1 -d' '

and this is the result

If
I'd
Black
Could
If
Still

i sense that i am nearly there but i need a little push to the finish line. can u help?

Hi FirstMan,

cut isn't really the right tool for this.  head/tail are.


Good Luck,
Kent
i was wondering if you can explain the head /tail function because until it was written to me in your solution i have never heard of it. also im using vi editor, what is a streaming editor??
man head
man tail
man sed
i still don't understanding what it means. i have been studyin unix more particular shell scripting for 4 days. i still have problems with this question. i feels that i was close before and now that that head/tail is introducted, im completely lost.
Hi FirstMan,

Ozo's dead on that you need to read up on these items.  :)  The man pages have a lot more to offer than a few lines in this thread.

A 1 sentence description of each of these is that head copies the head of a file (lines starting from the beginning of the file), tail copies from the tail of the file (lines starting backwards from the end of the file), and sed (short for streaming editor) allows you to edit a file (copying it to a new file) where the edit commands are formed into a string and passed as a parameter to sed.

That said, sed is the hardest to learn because it involves "regular expressions".  To the newbie, these can cause migraines.  But the experienced unix geek can't live without them.  Passing a line number to head or tail is a much easier task to understand.


Kent
Hi FirstMan,

consider a file with these 5 lines.

line1
line2
line3
line4
line5

You can copy the first two lines of the file using head

head -n 2 somefile > newfile


The line that you need to insert can be appended to the file with echo (or any of several commands).  Note that '>>' is used to write to the end of the file. Using '>' causes the file to be overwritten.

echo "this is the new line" >> newfile


Now the end of the original file can be copied with tail.  Note again the use of '>>'.

tail -n +3 somefile >> newfile


The new file contains

line1
line2
this is the new line
line3
line4
line5


At this point most of the work is done.  All you need to do is determine whether the line is inserted at the beginning or in the middle.  If in the middle, execute wc to determine how many lines are in the original file, divide that by 2, and pass that value to head, and that value+1 to tail.


Good Luck,
Kent
Thanks for that example, it  was very useful.  i read an tutorial on the internet, that there are a number of ways that it can be done. I have not been studying unix for long, but i hope that the solution given to me are  SED or AWK??
Hi FirstMan,

Yes, sed can do this, too.  But the syntax is far from obvious.  If you want to use this you might have to convince your instructor that you understand enough about regular expressions to be dangerous

   sed '3i/this is the new line/' somefile > newfile

That will give you the exact results shown above.


Good Luck,
Kent
ok thank you.if for instance i want to use the same question but for in sed or awk. how would i go about??
sorry discard the previous comment, if you want to question the question by not using sed or awk, how would you go about it?
Hi Kdo,

Either sed, or the head/echo/tail series does fine.


Good Luck,
Kent
thank you for help. i have an another question

Create a script named 'username' that takes 1 argument being a file, in this instance we will use the newly created file above xx. Read the input file and look for occurrences of the current user who is executing the script. On finding an occurrence of the username take that line and append it to a file and display a line number and a bracket against the saved line.

I have already created 'usename' file, obtain the environment, apeend the occurence of the username but i am having difficulties in numbering the line with brackets. can u help??
Hi FirstMan,

You'll want to use grep.  Do a 'man grep' for all of the options available to you.

To search your file for 'FirstMan', you could do

grep FirstMan file1


Good Luck,
Kent
Hi FirstMan,
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I will leave the following recommendation for this question in the Cleanup Zone:
Accept: Kdo {http:#20013938}
Any objections should be posted here in the next 4 days. After that time, the question will be closed.

Suhas
Experts Exchange Cleanup Volunteer
Forced accept.

Computer101
EE Admin