Solved

Referencing a text line in Linux

Posted on 2011-09-19
5
276 Views
Last Modified: 2012-08-14
Hi,

I'm new to Linux and I’m trying to write a simple script to reference lines in a text file and perform math. I have the basics of what I from doing some research online but I cant find how to reference text within a text file.

#!/bin/bash
var2= "needs to referance line 4 of file calc2"
var3= "needs to referance line 5 of fil calc3"
var4=$[$var2 / $var3]
echo calculation is $var4
0
Comment
Question by:JacobC
  • 3
  • 2
5 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 36563622
Hi,

this can be done with sed:

var2=$(sed -n '4p' calc2)
var3=$(sed -n '5p' calc3)

var4=$(($var2 / $var3))    # Integer math only!
echo calculation is $var4

Or with awk:

var2=$(awk 'NR==4 {print}' calc2)
var3=$(awk 'NR==5 {print}' calc3)

var4=$(awk -v v2=$var2 -v v3=$var4 'BEGIN {printf "%f\n", v2/v3}')  # Floating point math!
echo calculation is $var4

wmp
0
 

Author Comment

by:JacobC
ID: 36563729
awk says
awk: fatal: division by zero attempted

sed says
awk: fatal: division by zero attempted
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 36563748
OK,

so line 5 of calc3 contains zero. I'd claim not being responsible for this.

wmp
0
 

Author Comment

by:JacobC
ID: 36563762
Oops I’m sorry I messed up my file names, it works great!

Do you know any good reference sites I can read up on to get a better understanding? I know how to do basic math in Linux but I want to learn more about exporting/importing information out of files into equations.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 36563839
This is the bash reference manual:

http://www.gnu.org/s/bash/manual/bash.html

Selecting from files (to assign variables for later use e.g. in calculations) can be done with sed, awk, perl, and also with combinations of "head", "tail" and "cut". There are many ways, "read" and "set" can also be used in particular situations.

See the manpages of sed, awk, head, tail, cut etc.

This is for GNU sed:
http://www.gnu.org/software/sed/manual/sed.html

and GNU awk:
http://www.gnu.org/s/gawk/manual/gawk.html

Perl is a very powerful programming language. A tutorial:
http://www.perltutorial.org/

Have fun!

wmp
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Over the last ten+ years I have seen Linux configuration tools come and go. In the early days there was the tried-and-true, all-powerful linuxconf that many thought would remain the one and only Linux configuration tool until the end of times. Well,…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

706 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

21 Experts available now in Live!

Get 1:1 Help Now