Solved

Referencing a text line in Linux

Posted on 2011-09-19
5
281 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

This is the error message I got (CODE) Error caused by incompatible libmp3lame 3.98-2 with ffmpeg I've googled this error message and found out sometimes it attaches this note "can be treated with downgrade libmp3lame to version 3.97 or 3.98" …
Fine Tune your automatic Updates for Ubuntu / Debian
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

830 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