Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Referencing a text line in Linux

Posted on 2011-09-19
5
Medium Priority
?
292 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 2000 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

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

Question has a verified solution.

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

I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
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 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…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Suggested Courses
Course of the Month11 days, 12 hours left to enroll

916 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