Solved

Unix example questions

Posted on 2006-06-12
10
667 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
 Watch the Recording: Learning MySQL 5.7

MySQL 5.7 has a lot of new features. If you've dabbled with an older version of MySQL, it is definitely worth learning.

 

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

Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

626 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