Computing.Net > Forums > Unix > Shell Script: Get value from a file

Shell Script: Get value from a file

Reply to Message Icon

Original Message
Name: Khaliq
Date: November 12, 2003 at 08:23:31 Pacific
Subject: Shell Script: Get value from a file
OS: Unix
CPU/Ram: P4/256
Comment:

Hello All,
I am looking for a way to get a string from
a file and assign it to a variable in Shell
Script.
Example I have a file
FILE containing
A
B
B
If I run grep -c 'B' FILE
It gives me the 2 can I assign this 2 as a
string or integer to a variable. Or in the
other cas Is it possible to extract B and
assign it to a variable.
Thanks
Khaliq


Report Offensive Message For Removal

Response Number 1
Name: Dlonra
Date: November 12, 2003 at 09:33:26 Pacific
Subject: Shell Script: Get value from a file
Reply: (edit)

VAR=$(grep -c 'B' FILE)

similarly for grep 'B' FILE. Of course, this will give 2 values for VAR.

you should google
ksh scripting tutorial


Report Offensive Follow Up For Removal

Response Number 2
Name: unixhead
Date: December 2, 2003 at 16:22:14 Pacific
Subject: Shell Script: Get value from a file
Reply: (edit)


For extracting B:

1st occurence of 'B':
grep 'B' FILE|sed -n '1p'

2nd occurence of 'B':
grep 'B' FILE|sed -n '2p'

Fields within:
grep 'B' FILE|sed -n '1p'|awk '{print $2}'

or whatever occurence and field number.


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Shell Script: Get value from a file

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software