Solved

Referencing a text line in Linux

Posted on 2011-09-19
5
288 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
[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
  • 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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

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,…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
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.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

623 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