Link to home
Start Free TrialLog in
Avatar of icb01co1
icb01co1

asked on

Unix example questions

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.
Avatar of ahoffmann
ahoffmann
Flag of Germany image

> 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 ;-)
Avatar of icb01co1
icb01co1

ASKER

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
stumped at which part?
What's wronh with my suggestion?
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.
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?

> .. 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}'`"

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.



ASKER CERTIFIED SOLUTION
Avatar of ahoffmann
ahoffmann
Flag of Germany 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