Referencing a text line in Linux

Posted on 2011-09-19
Last Modified: 2012-08-14

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.

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
Question by:JacobC
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
LVL 68

Accepted Solution

woolmilkporc earned 500 total points
ID: 36563622

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


Author Comment

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

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

Expert Comment

ID: 36563748

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


Author Comment

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.
LVL 68

Expert Comment

ID: 36563839
This is the bash reference manual:

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:

and GNU awk:

Perl is a very powerful programming language. A tutorial:

Have fun!


Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
expand ext4 on centos 6 5 71
rsyslog raw message 4 61
php mail() send emails but the message is lost 4 33
subtract to time in .bash file 2 10
Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
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.
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