Ayyappa Boligala
Ayyappa Boligala

Reputation: 1096

Unable to increment last 2 digit of variable declared in file using script

I have the file given below:

elix554bx.xayybol.42> vi setup.REVISION
# Revision information
setenv RSTATE R24C01
setenv CREVISION X3
exit

My requirement is to read RSTATE from file and then increment last 2 digits of RSTATE in setup.REVISION file and overwrite into same file. Can you please suggest how to do this?

Upvotes: 0

Views: 145

Answers (3)

qwwqwwq
qwwqwwq

Reputation: 7309

Here's an awk one-liner type solution:

awk '{
    if ( $0 ~ 'RSTATE' ) {
    match($0, "[0-9]+$" );
    sub( "[0-9]+$",
        sprintf( "%0"RLENGTH"d", substr($0, RSTART, RSTART+RLENGTH)+1 ),
        $0 );
    print; next;
    } else { print };
}' setup.REVISION > tmp$$
mv tmp$$ setup.REVISION

Returns:

setenv RSTATE R24C02
setenv CREVISION X3
exit

This will handle transitions from two to three to more digits appropriately.

Upvotes: 1

user2585076
user2585076

Reputation:

I wrote for you a class.

class Reader
{
    public string ReadRs(string fileWithPath)
    {
        string keyword = "RSTATE";
        string rs = "";
        if(File.Exists(fileWithPath))
        {
            StreamReader reader = File.OpenText(fileWithPath);
            try
            {
                string line = "";
                bool finded = false;
                while (reader != null && !finded)
                {
                    line = reader.ReadLine();
                    if (line.Contains(keyword))
                    {
                        finded = true;
                    }
                }
                int index = line.IndexOf(keyword);
                rs = line.Substring(index + keyword.Length +1, line.Length - 1 - (index + keyword.Length));
            }
            catch (IOException)
            {
                //Error
            }
            finally
            {
                reader.Close();
            }

        }

        return rs;
    }
    public int GetLastTwoDigits(string rsState)
    {
        int digits = -1;
        try
        {
            int length = rsState.Length;
            //Get the last two digits of the rsstate                
            digits = Int32.Parse(rsState.Substring(length - 2, 2));
        }
        catch (FormatException)
        {
            //Format Error
            digits = -1;
        }

        return digits;
    }
}

You can use this as exists

Reader reader = new Reader();
string rsstate = reader.ReadRs("C://test.txt");
int digits = reader.GetLastTwoDigits(rsstate);

Upvotes: 0

Jonathan Leffler
Jonathan Leffler

Reputation: 753765

If you're using vim, then you can use the sequence:

/RSTATE/
$<C-a>:x

The first line is followed by a return and searches for RSTATE. The second line jumps to the end of the line and uses Control-a (shown as <C-a> above, and in the vim documentation) to increment the number. Repeat as often as you want to increment the number. The :x is also followed by a return and saves the file.

The only tricky bit is that the leading 0 on the number makes vim think the number is in octal, not decimal. You can override that by using :set nrformats= followed by return to turn off octal and hex; the default value is nrformats=octal,hex.

You can learn an awful lot about vim from the book Practical Vim: Edit Text at the Speed of Thought by Drew Neil. This information comes from Tip 10 in chapter 2.

Upvotes: 1

Related Questions