Solved

Unix example questions

Posted on 2006-06-12
10
647 Views
Last Modified: 2013-12-26
Hi Experts,

Thought i'd have a bash at learning UNIX as I mostly know scripting languages or higher level OO based languages. Got to admin its all a little confusing and I am attempting to example questions I found from various sites. First one is to write a script that takes two arguments a file and a string and inputs the string at top of file and in middle line of file.

I know how to get the number of lines from a file i.e.

wc -w File

But I am unsure how to use that as a variable in my script. All that I have read about variables is simple assignment i.e.

myVariable=This

I dont know how to assign the return of a function to the variable i.e.

myVariable=wc -w File

doesnt seem to work. And I dont think its possible to redirect to variables is it? i.e.

wc -w File > $LineCount

Once I have the line numbers I can determine where to add the string but I wouldnt know how to do this either.  I would guess that to add the string in the middle line of the file I would first assign the head lines of a variable then append the var to add the sting then append it to add the tail values. But again I dont know how to assign the results of the Tail and Head returns to a file i.e.

$First=$LineCount/2
$Text= Tail -$First

Am I completely off the mark here with what I am wanting to do? I think i probably am and the other questions are alot harder still.

Thanks in advance,
Chris.
0
Comment
Question by:icb01co1
  • 4
  • 4
10 Comments
 
LVL 51

Expert Comment

by:ahoffmann
ID: 16889172
> I dont know how to assign the return of a function to the variable i.e.

myVariable="`wc -w File`"

> Once I have the line numbers I can determine where to add the string but I wouldnt know how to do this either.
sed -e "1,$myVariable p" file > head
sed -e "$myVariable,"'$ p' file > tail
( cat head; echo your line; cat tail) > new file

a bit cumbersome in shell, but something you asked for ;-)
0
 

Author Comment

by:icb01co1
ID: 16889210
Ok now I know that I should be using the sed command and have figured out how to add a new line to a file i.e:

sed '/1/G' myFile

But am still stumped
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 16889231
stumped at which part?
What's wronh with my suggestion?
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:icb01co1
ID: 16889264
Thanks ahoofmann, just one problem here, it says dont use external files and your redirecting to the head and tail files. Is there not a simpler way to add a string to file using sed?

Seems as if I can add a new line to a file with sed i.e.

sed '/3/G' myFile > myFile

I must be able to add a string. I am not sure about the sed syntax though, theres all this talk of

[address[,address]][!]{
command1
command2
}

Which may as well be greek to me.
Thanks, Chris.
0
 

Author Comment

by:icb01co1
ID: 16889480
ahoffmann, Ive a problems actually.

"wc -w File" doesnt just return an integer.

It is returning something like this: "      6 File"

Should I use sed on this to remove the spaces and the occurance of the filename?

0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 16889593
> .. it says dont use external files  ..
who says? your teacher?

> Is there not a simpler way to add a string to file using sed?
I already sad: cumbersome.
If you stick on sed, then
  man sed
it's possible, but more cumbersome as I suggested
otherwise
  man awk

> "wc -w File" doesnt just return an integer.
myVariable="`wc File|awk '{print $2}'`"

0
 

Author Comment

by:icb01co1
ID: 16896684
My teacher?  

Ok just read up about awk, thats quite a usefull language. I am still having trouble with the first part though, here:

sed -e "1,$myVariable p" file > head

I dont nessacarily want an answer, just a link to better explain sed and maybe awk too. I cant find anything thats making any sense.

Thanks, Chris.



0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 125 total points
ID: 16897210
> sed -ne "1,$myVariable p" file > head
prints from line 1 to line in $myVariable

Bugfix: just seen that I missed the -n option for the sed commands, please use instead
sed -ne "1,$myVariable p" file > head
sed -ne "$myVariable,"'$ p' file > tail


http://www.cs.bell-labs.com/cm/cs/awkbook/index.html

>  I cant find anything thats making any sense.
use perl, it can do it all in one
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Generic progress indicator 6 117
sum28 challenge 31 112
scoreUp challenge 14 63
Problem to event 3 97
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

777 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