read a value from a string/line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting read a value from a string/line
# 1  
Old 04-29-2009
read a value from a string/line

I have a file that contains the something like this

a=15
b=21.5
c=544
and so on

the question is how could I find the line that contains the value of c and store its value on a variable in bash shell?, Thanks in advanced
# 2  
Old 04-29-2009
If c is prsent in the file. It will be good to test first if c is present ( use grep with option c to get the count and compare it with 0)
Code:
var = `grep c filename | grep -v grep | cut -d "=" -f2`

cheers,
Devaraj Takhellambam
# 3  
Old 04-29-2009
More possibilties:

Code:
var=$(awk -F= '/c=/{print $2}' file)

var=$(sed -n 's/c=//p' file)

# 4  
Old 04-29-2009
Thanks for the answer, I've tested the first one and it works, thanks a lot, but for the real case I'm working with does not (the first message file composition was an example that I thought was equivalent). The real line and value that I'm looking for has the following composition

Total Transmitted pow. 0.984574121

and cut -d does not accept two spaces as delimiter between the string to remove and the value to store. The point causes also some conflicts with the point after pow. I'm on testing the second
# 5  
Old 04-29-2009
Got it!, I had not understood the delimiters politics properly,

var=` grep "Total Transmission pow." file.dat | cut -d "." -f2,3`

thanks again!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to read file line by line and compare subset of 1st line with 2nd?

Hi all, I have a log file say Test.log that gets updated continuously and it has data in pipe separated format. A sample log file would look like: <date1>|<data1>|<url1>|<result1> <date2>|<data2>|<url2>|<result2> <date3>|<data3>|<url3>|<result3> <date4>|<data4>|<url4>|<result4> What I... (3 Replies)
Discussion started by: pat_pramod
3 Replies

2. Shell Programming and Scripting

Read line by line and replace string.

Hi, I currently have a problem that I need to read a file line by line. After I read it line by line there are some commands in which I have to change a specific string.(In my case, I have to make a script that changes all the passwords into hash value) Here is a sample input... (3 Replies)
Discussion started by: thebennnn
3 Replies

3. Red Hat

How to add a new string at the end of line by searching a string on the same line?

Hi, I have a file which is an extract of jil codes of all autosys jobs in our server. Sample jil code: ************************** permission:gx,wx date_conditions:yes days_of_week:all start_times:"05:00" condition: notrunning(appDev#box#ProductLoad)... (1 Reply)
Discussion started by: raghavendra
1 Replies

4. Shell Programming and Scripting

Need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line...

Hello, I need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line... An example of entries in the file would be: SRVXPAPI001 ERRO JUN24 07:28:34 1775 REASON= 0000, PROCID= #E506 #1065: TPCIPPR, INDEX= 003F ... (8 Replies)
Discussion started by: Ferocci
8 Replies

5. Shell Programming and Scripting

how to read the contents of two files line by line and compare the line by line?

Hi All, I'm trying to figure out which are the trusted-ips and which are not using a script file.. I have a file named 'ip-list.txt' which contains some ip addresses and another file named 'trusted-ip-list.txt' which also contains some ip addresses. I want to read a line from... (4 Replies)
Discussion started by: mjavalkar
4 Replies

6. Shell Programming and Scripting

Read Line and sent as variable for each string

Hi , I want to read below output, lets called it output1.txt and each string for every line will be declare as a variables. This is the input file 196 server_a server_unix_2 FW 196 server_b server_win_1 CD 196 server_c server_win_2 CD 196 server_bd ... (2 Replies)
Discussion started by: sQew
2 Replies

7. Shell Programming and Scripting

[Solved] Read a line from one string till to another.... Unix scripting..

So i have a file which contains paths to JPG images separated by a space. I have to separate them each path to another file. So, I have to search all strings that start from /home/ and ends with .jpg or .png Then write each one to another file... Can you please help me on doing this???:cool: (11 Replies)
Discussion started by: hakermania
11 Replies

8. Shell Programming and Scripting

bash: read file line by line (lines have '\0') - not full line has read???

I am using the while-loop to read a file. The file has lines with null-terminated strings (words, actually.) What I have by that reading - just a first word up to '\0'! I need to have whole string up to 'new line' - (LF, 10#10, 16#A) What I am doing wrong? #make file 'grb' with... (6 Replies)
Discussion started by: alex_5161
6 Replies

9. UNIX for Dummies Questions & Answers

Read a string with leading spaces and find the length of the string

HI In my script, i am reading the input from the user and want to find the length of the string. The input may contain leading spaces. Right now, when leading spaces are there, they are not counted. Kindly help me My script is like below. I am using the ksh. #!/usr/bin/ksh echo... (2 Replies)
Discussion started by: dayamatrix
2 Replies

10. Shell Programming and Scripting

read string, check string length and cut

Hello All, Plz help me with: I have a csv file with data separated by ',' and optionally enclosed by "". I want to check each of these values to see if they exceed the specified string length, and if they do I want to cut just that value to the max length allowed and keep the csv format as it... (9 Replies)
Discussion started by: ozzy80
9 Replies
Login or Register to Ask a Question