[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 256
  • Last Modified:

shell program, how to open a file and match for a line

I have a file (status.pl) that has a key word in it.  I want to math for "$initiateProcess" and return that line in my shell program.  Also, how to figure out (or match) for the value after "=" to determine if it is 0 or 1.

In status.pl:
$initiateProcess = 0;  # if 1, then initiate process

0
rgbcof
Asked:
rgbcof
  • 2
  • 2
1 Solution
 
johnsoneSenior Oracle DBACommented:
If you want the line number that contains $initiateProcess, grep will give that to you:

grep -hn "\$initiateProcess" status.pl | cut -f1 -d:

Then if you want the value after the =

grep "\$initiateProcess" status.pl | cut -f2 -d'='| cut -f1 -d\;

This assumes that the value you are looking for is preceded with = and has ; following it.
0
 
rgbcofAuthor Commented:
is there a way to use regular expression to extract the number after "=" and before ";"
0
 
rgbcofAuthor Commented:
It works, but I was hoping for a regular expression way.
0
 
johnsoneSenior Oracle DBACommented:
This should work as well:

grep "\$initiateProcess" status.pl | sed 's/^.*=\(.*\);.*/\1/'
0
 
TintinCommented:
Here's another way


 status=$(sed -n 's/$initiateProcess = \([0-9]\).*/\1/p' status.pl)

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now