Extracting values from files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extracting values from files
# 1  
Old 08-13-2004
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  
Old 08-14-2004
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  
Old 08-14-2004
Thats great Smilie

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  
Old 08-14-2004
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  
Old 08-15-2004
Hey thnx ... gonna experiment a bit now...

sure glad i found this forum..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extracting and comparing values

I was trying to extract value of g1 and p1 only inside the tags where t1 is "Reading C (bytes)" and comparing them to make sure p1 is always less than g1. Here is the Json file I'm using - File:- { "g1" : 1482568, "n1" : "v_4", "p1" : 0, "s1" : "RC", "t1" : "LM", } { "g1" :... (3 Replies)
Discussion started by: Mannu2525
3 Replies

2. Shell Programming and Scripting

Taking key values from one file and extracting values from another file

Hi, I have two files with values in both. File1: cat 2 3 dog 4 5 elephant 6 7 camel 2 3 File2: ----+--gkf;ajf= ---+---- +----- cat -------=----+ 3 | 4 ----- dog ------++-- 5 | 9 ----++-- elephant | 5 | 7 ---++ camel ------ ++++_---- || 8 | 9 I want the final file as: cat 4... (1 Reply)
Discussion started by: npatwardhan
1 Replies

3. Shell Programming and Scripting

Extracting values based on line-column numbers from multiple text files

Dear All, I have to solve the following problems with multiple tab-separated text file but I don't know how. Any help would be greatly appreciated. I have access to Linux mint (but not as a professional). I have multiple tab-delimited files with the following structure: file1: 1 44 2 ... (5 Replies)
Discussion started by: Bastami
5 Replies

4. Shell Programming and Scripting

Extracting field values from .csv

How can I select the bold fields from the following? "CLLI","SWREL","RPTDATE","RPTIME","TZ","RPTTYPE","RPTPD","IVALDATE","IVALSTART","IVALEND","NUMENTIDS" "tklc9010801","EAGLE5 45.0.0-64.70.1","2013-08-07","02:01:50","MST ","COMPONENT MEASUREMENTS ON... (4 Replies)
Discussion started by: leghorn
4 Replies

5. UNIX for Dummies Questions & Answers

extracting values from file using perl

I would like to read value after $ symbol from a file. For eg a file will be having the following lines. 5,$0,-32768p,32767p,$400014f8,1,, 5,$0,0,0,$400008ce,1,, 5,$0,0,0,$400008d0,1,, i would like to read 400014f8 then 400014f8 and similar all the value after $ symbol.Can any one help... (1 Reply)
Discussion started by: jagadeeshrr
1 Replies

6. Shell Programming and Scripting

Extracting fetching values

I have a file like this ############################################ # ParentFolder Flag SubFolders Colateral 1 Source1/Checksum CVA 1 Source1/Checksum Test 1 VaR/Checksum Test2 1 SVaR/Checksum FX 1 ... (6 Replies)
Discussion started by: manas_ranjan
6 Replies

7. Shell Programming and Scripting

extracting values from configuration file

Dear All, i am new to shell scripting, I am working on embedded system based on linux.I am supposed to the read the configuration file and edit another file. presently I would like to read from the configuration file.It would be having values file one below. There is chance of entering... (6 Replies)
Discussion started by: Ratheendran
6 Replies

8. UNIX for Dummies Questions & Answers

Extracting values from an XML file

Hello People, I have an xml file from which I need to extract the values of the parameters using UNIX shell commands. Ex : Input is like : <Name>Roger</Name> or <Address>MI</Address> I need the output as just : Roger or MI with the tags removed. Please help. (1 Reply)
Discussion started by: sushant172
1 Replies

9. Shell Programming and Scripting

Extracting the values separated by comma

Hi, I have a variable which has a list of string separated by comma. for ex , Variable=/usr/bin,/usr/smrshbin,/tmp How can i get the values between the commas separately using shell scripts.Please help me. Thanks, Padmini. (6 Replies)
Discussion started by: padmisri
6 Replies

10. Shell Programming and Scripting

Extracting dynamic values

Hi, I am stuck with extracting values by combining 2 dynamically extracted values. The code goes like this #!/usr/bin/ksh ID1="abcd" i=1 #this is a dynamic value and keeps on changing b="ID" #this is static now i want the value of ID1 variable. like echo $b$i But echo... (1 Reply)
Discussion started by: chaitanyapn
1 Replies
Login or Register to Ask a Question