We help IT Professionals succeed at work.

Possible Solution to Question 1

FirstMan
FirstMan asked
on
353 Views
Last Modified: 2010-04-21
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!!
Comment
Watch Question

Data Warehouse / Database Architect
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
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?

Kent OlsenData Warehouse / Database Architect
CERTIFIED EXPERT

Commented:
Hi FirstMan,

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


Good Luck,
Kent

Author

Commented:
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??
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
man head
man tail
man sed

Author

Commented:
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.
Kent OlsenData Warehouse / Database Architect
CERTIFIED EXPERT

Commented:
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
Kent OlsenData Warehouse / Database Architect
CERTIFIED EXPERT

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

Author

Commented:
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??
Kent OlsenData Warehouse / Database Architect
CERTIFIED EXPERT

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

Author

Commented:
ok thank you.if for instance i want to use the same question but for in sed or awk. how would i go about??

Author

Commented:
sorry discard the previous comment, if you want to question the question by not using sed or awk, how would you go about it?
Kent OlsenData Warehouse / Database Architect
CERTIFIED EXPERT

Commented:
Hi Kdo,

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


Good Luck,
Kent

Author

Commented:
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??
Kent OlsenData Warehouse / Database Architect
CERTIFIED EXPERT

Commented:
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
Suhas .Senior QA Manager
CERTIFIED EXPERT

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

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.