Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Possible Solution to Question 1

Posted on 2007-10-04
17
Medium Priority
?
325 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!!
0
Comment
Question by:FirstMan
17 Comments
 
LVL 46

Accepted Solution

by:
Kent Olsen earned 2000 total points
ID: 20013938
Hi FirstMan,

This is apparently homework so we can't give you the total solution.  Still, help is here.  :)

These problems are nearly identical.  The only difference being where the text is inserted.

In the first problem you could copy the text to a new file, then copy the contents of the old file.  When the new file is built, copy it back over the original.

In the second problem, you'll need to copy part of the file, insert the text, then copy the rest of the file to the new file.  Again, the new file will need to be copied over the old file.

Both of these can be solved with the same script.  You can used one of the streaming editors (like sed) to insert the text, or you can use the head/tail commands to copy each part of the file.  head/tail are easier to understand so let's start there.

head -n 0 oldfile > newfile
echo "New Text"
tail -n +1 > newfile

Now modify these three lines so that the line number is "programmable".  For the first problem, substitute 0 and 1 for the two positions in head and tail.  (They are already in the script, but trust me.)  Then call wc to get the file length, compute the midpoint, and pass it to the script substituting for the positions in head and tail.



Good Luck,
Kent
0
 

Author Comment

by:FirstMan
ID: 20014052
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?

0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 20014107
Hi FirstMan,

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


Good Luck,
Kent
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:FirstMan
ID: 20015690
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??
0
 
LVL 85

Expert Comment

by:ozo
ID: 20015718
man head
man tail
man sed
0
 

Author Comment

by:FirstMan
ID: 20015864
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.
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 20015900
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
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 20015962
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
0
 

Author Comment

by:FirstMan
ID: 20016278
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??
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 20016522
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
0
 

Author Comment

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

Author Comment

by:FirstMan
ID: 20016690
sorry discard the previous comment, if you want to question the question by not using sed or awk, how would you go about it?
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 20016729
Hi Kdo,

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


Good Luck,
Kent
0
 

Author Comment

by:FirstMan
ID: 20017284
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??
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 20017550
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
0
 
LVL 9

Expert Comment

by:Suhas .
ID: 21169375
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
0
 
LVL 1

Expert Comment

by:Computer101
ID: 21197685
Forced accept.

Computer101
EE Admin
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A metadevice consists of one or more devices (slices). It can be expanded by adding slices. Then, it can be grown to fill a larger space while the file system is in use. However, not all UNIX file systems (UFS) can be expanded this way. The conca…
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded‚Ķ
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
Suggested Courses
Course of the Month13 days, 11 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question