Solved

Unix example questions

Posted on 2006-06-12
10
658 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
Independent Software Vendors: 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: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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
format the code in java 6 117
Change to event 1 138
Bartender label printing - switch on and off graphics 3 94
moto 4g phone enable finger print 4 74
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Suggested Courses

752 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