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

Posted on 2011-10-03
Last Modified: 2012-05-12
I have a file ( 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.

$initiateProcess = 0;  # if 1, then initiate process

Question by:rgbcof
    LVL 34

    Accepted Solution

    If you want the line number that contains $initiateProcess, grep will give that to you:

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

    Then if you want the value after the =

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

    This assumes that the value you are looking for is preceded with = and has ; following it.

    Author Comment

    is there a way to use regular expression to extract the number after "=" and before ";"

    Author Closing Comment

    It works, but I was hoping for a regular expression way.
    LVL 34

    Expert Comment

    This should work as well:

    grep "\$initiateProcess" | sed 's/^.*=\(.*\);.*/\1/'
    LVL 48

    Expert Comment

    Here's another way

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

    Open in new window


    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
    Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
    Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
    Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now