The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
extracting data from files.. anchal_khare Shell Programming and Scripting 1 04-03-2008 06:56 AM
extracting files using date in ksh pavan_test UNIX for Dummies Questions & Answers 1 04-04-2006 03:29 PM
extracting uncommon part between two files sabyasm Shell Programming and Scripting 2 11-06-2005 01:25 PM
Perl - extracting data from .csv files kregh99 Shell Programming and Scripting 3 10-09-2003 10:18 AM
extracting .rar files sskb UNIX for Dummies Questions & Answers 5 05-15-2002 09:20 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-13-2004
Master Error Master Error is offline
Registered User
  
 

Join Date: Aug 2004
Posts: 3
Extracting values from files

Im trying to create a utility that can do unit conversions using a seperate formula file(one which i can add conversions to at a later date). however i'm stuck when it comes to pulling the formulas out of the formula file for use in the script.
heres a rundown of what the script does.

The formula file consists of 3 columns :
a code such as m_ft (metres to feet), the constant (used for the conversion), and a description

In the script the user types in the code (m_ft for example), the script then searches for the code in the formula file (using grep).. i opted to send the output from grep into a temp file.
problem is now i want to extract the 2nd column that contains the constant for use in the script. Is there a way to cut the constant and put it into a variable in the script, and then apply an arithmetic operation to it ( y = x * constant) ?

I've looked up piping the cut command but no where does it say if i can pipe to a variable and how the variable will behave.

Tnx
  #2 (permalink)  
Old 08-14-2004
MacMonster MacMonster is offline
Registered User
  
 

Join Date: Oct 2001
Location: Null
Posts: 16
Code:
#!/bin/bash

echo -n "Enter formula code: "
read code

if [ ! `cat formula.txt | cut -d ' ' -f 1 | grep "$code"` ]; then
    echo "No such formula."
    exit
fi

echo -n "Enter value: "
read value

awk '$1 == "'"$code"'" { print '"$value"' * $2 }' formula.txt
Formula.txt is the formula database file. This is the content of my formula.txt:

Code:
hkd_usd  0.128 #HKD -> USD
kg_pound 2.2   #KG  -> Pound
min_sec  60    #Minute -> Second
mile_km  1.6   #Mile -> Kilometer
nm_km    1.852 #Nautical mile -> Kilometer
  #3 (permalink)  
Old 08-14-2004
Master Error Master Error is offline
Registered User
  
 

Join Date: Aug 2004
Posts: 3
Thats great

but if u don't mind could u break that down for me so i can understand what the codes are doing ...

just these two steps

Quote:
if [ ! `cat formula.txt | cut -d ' ' -f 1 | grep "$code"` ];

Quote:
awk '$1 == "'"$code"'" { print '"$value"' * $2 }' formula.txt
sorry still a beginner, so id much rather learn then copy..
  #4 (permalink)  
Old 08-14-2004
google's Avatar
google google is offline Forum Advisor  
Moderator
  
 

Join Date: Jul 2002
Location: Atlanta
Posts: 740
quote:
--------------------------------------------------------------------------------
if [ ! `cat formula.txt | cut -d ' ' -f 1 | grep "$code"` ];
--------------------------------------------------------------------------------

The code above uses the 'cat' command to pass the contents of the formula.txt file to the 'cut' command. The cut command is used to the cut out the first field ( this is the -f 1 portion of the command) within the file. Each field is delimited by 'cut' by a space (this is the -d ' ' portion of the command). Finally, the output of the cut command (that is, the first field) is passed to 'grep' which then attempts to match one of the entries in the file. Code is set to a value passed in by the user. This whole command sequence is surrounded by a file test which is testing whether or not 'grep' found a match. If it doesnt, the program exits and a message is printed to the screen. "No such formula"



quote:
--------------------------------------------------------------------------------
awk '$1 == "'"$code"'" { print '"$value"' * $2 }' formula.txt
--------------------------------------------------------------------------------
This peice of code searches for the entry in the formula.txt file that matches what the user typed in (I assume this is a value such as USD, or Pound, or Second etc). Awk will then match the entry and use the value (another peice of data entered by the user) multiplied by the second field ($2) of the record matched. The output of this step is printed to the screen.


A few thoughts, it would be better to use a "Select case" to generate a menu rather than have the user type in something that may not be in the file. Also, when using Awk, it would be more portable to use the built in -v switch to pass in shell variables to the program.
  #5 (permalink)  
Old 08-15-2004
Master Error Master Error is offline
Registered User
  
 

Join Date: Aug 2004
Posts: 3
Hey thnx ... gonna experiment a bit now...

sure glad i found this forum..
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:12 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0