Need to extract value from a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to extract value from a variable
# 1  
Old 06-30-2017
Need to extract value from a variable

Hello All,

I have a variable named as output which holds value as below -
Code:
output=[record
  bdfo_len      73
  bdfo_key      "     200000016"
  bdfo_param_no "000"
  parm01        NULL
  parm02        NULL
  parm03        NULL
  parm04        NULL
  parm05        NULL
  parm06        NULL
  parm07        NULL
  parm08        NULL
  parm09        NULL
  parm13        NULL
  parm16        NULL
  parm017       NULL
  parm18        NULL
  parm93        NULL
  parm94        NULL
  parm96        NULL
  parm97        NULL
  parm83        NULL
  header        [record
                  bdfo_run_date     41991
                  bdfo_file_id      "GYFL3027"
                  bdfo_centre_id    "G"
                  bdfo_sbi_file_ind "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY"]
  trailer       NULL
  data          NULL]

I need to extract bdfo_run_date value which is '41991' out of this, any pointers will be greatly appreciated.
# 2  
Old 06-30-2017
Quick and dirty sed solution:
Code:
echo "${output}" | sed -n '/bdfo_run_date/ s///p'

Quoting ${output} preserves the formatting of the variable. sed is run in "no output" mode until it finds the string you are looking for. It then deletes that string and prints the result. The shell then takes care of the white space.

As I said, it is quick and dirty

Andrew
This User Gave Thanks to apmcd47 For This Post:
# 3  
Old 06-30-2017
Thank you Andrew

Hello Andrew,
Thank you very much for sed solution, it worked, however if I wish to store the value of bdfo_run_date instead of displaying it, what can exactly be done please?

Thanks in advance !
# 4  
Old 06-30-2017
awk splits on whitespace into fields
Code:
echo "$output" | awk '$1=="bdfo_run_date" {print $2}'

To store this in a variable you can use the ` ` or $( ) (replacing the command by its output)
Code:
output=`echo "$output" | awk '$1=="bdfo_run_date" {print $2}'`

This User Gave Thanks to MadeInGermany For This Post:
# 5  
Old 07-03-2017
It worked

SmilieSmilie I worked.. thank you very much !
Quote:
Originally Posted by MadeInGermany
awk splits on whitespace into fields
Code:
echo "$output" | awk '$1=="bdfo_run_date" {print $2}'

To store this in a variable you can use the ` ` or $( ) (replacing the command by its output)
Code:
output=`echo "$output" | awk '$1=="bdfo_run_date" {print $2}'`

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to extract value from column using variable

I am having trouble extracting the value in the columns declared in a variable. I have tried several different variation of awk but both give me the column number and not the actual value of that column. Any suggestions? Neither of the "extract" variables below are performing as desired ... (5 Replies)
Discussion started by: ncwxpanther
5 Replies

2. UNIX for Dummies Questions & Answers

Extract variable name in a string

Hi, I'm doing a script to list all scripts called by a "master" script. But I have an issue as there is some variables in the name of the called scripts. Example: % cat master_script.sh ENVIR=PROD VERSION=1.2 /users/maturix/$ENVIR/program_$VERSION.shI would like my script displays a kind... (7 Replies)
Discussion started by: maturix
7 Replies

3. Shell Programming and Scripting

Extract values from Perl variable

Hi Guys, I am stuck in a problem. I have a variable in Perl script which has value for example X=a-b-c; Now, I want to extract a b c separately into different 3 variables. I know this can be done in shell using awk but Perl behaves a bit different. Can anybody help me on this please?... (3 Replies)
Discussion started by: prashant2507198
3 Replies

4. Shell Programming and Scripting

Extract and place in variable

If i wanted to extract the number of collisions from the eth0 section of the ifconfig file and have the output placed in a variable how would i do this? I can get the output displayed using: /sbin/ifconfig eth0 | grep "collisions" What command would i now use to place the output in a... (1 Reply)
Discussion started by: warlock129
1 Replies

5. Shell Programming and Scripting

bash, extract value from the file to the variable

Hi I've got phrases (string) in phrases.txt file and I've got script.sh. How can I draw out the first line from phrases.txt, save it in variable PHRASE and perform the content of script.sh, and then draw out the second line from phrases.txt, save it in the same variable PHRASE and perform the... (4 Replies)
Discussion started by: patrykxes
4 Replies

6. UNIX for Dummies Questions & Answers

Using GREP to extract variable following a string

Hello, I'm running calculations and I need to extract a specific number from a output file. So far I've only been able to GREP entire lines containing the string: '1 F=' . I would like to go a step further and extract just the number following '1 F='. The entire line looks like: 1 F=... (10 Replies)
Discussion started by: modey3
10 Replies

7. Shell Programming and Scripting

Extract Variable and Append it to the file

I have a file named xyz_extract_file_20091201.dat and I need to strip out 20091201 from the file name and append this value as a first field in all the records in the file. Can you please help? Thanks. Eg. Input File: xyz_extract_file_20091201.dat ------------ Campaign1,A2347,Grp_id1... (1 Reply)
Discussion started by: shekharaj
1 Replies

8. UNIX for Dummies Questions & Answers

extract characters from a variable

Hi All, I have scenario as below, I want to cut the characters and store it variable2.. and want to display the variable2.. as shown below... eg: the size may differs , i am show just an example..... numbers are not fixed in variable1..i want to extract characters and... (1 Reply)
Discussion started by: G.K.K
1 Replies

9. Shell Programming and Scripting

How to extract variable number from a String

hi,I am new to shell script,I have String,like this: Number of rows exported: 5321 the numbe at end could changing,how can I extract this number and assign it to a variable,then use it later in script. thanks. (19 Replies)
Discussion started by: vitesse
19 Replies

10. Shell Programming and Scripting

How to extract field from variable more efective

Hi, I know that simple way to do this is var=`echo $line | awk '{print $5}'` But maybe there is shorter way to do this ? Also maybe there is some way to assign all fileds from line separated by lets say ":" to some variables instead of var1=`echo $line | awk '{print $1}'` var2=`echo $line... (11 Replies)
Discussion started by: pp56825
11 Replies
Login or Register to Ask a Question