Solved

Unix example questions

Posted on 2006-06-12
10
637 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
 

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now