Indexing Variable Names


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Indexing Variable Names
# 1  
Old 09-25-2014
Indexing Variable Names

Hi All

I think I might have bitten off more than I can chew here and I'm hoping some of you guys with advanced pattern matching skills can help me.

What I want to do is index the occurrence of variable names within a library of scripts that I have. Don't ask why, I'm just sad like that...

I have code of this form:

Code:
    EXPECTED_FILE=$(m_find_file ${CSV}) 
    if !  m_check_file -f "${EXPECTED_FILE}" s
    then
             m_junit_xml_log_failure ${TT} "${FQN%/*}" "${SCRIPT_NAME}" "Muse Runtime" "Expected file not found" "${XML_REPORT}"
             m_fail 1 "Error: Failed to find expected result file (${CSV}) (${SCRIPT_NAME})" 
    fi

As you can see the variable names are all uppercase and can contain the characters [_A-Z0-9].

First of all I want to isolate all of the lines containing a variable and record the line number.

Then within each line I want to pull out each variable name in isolation, without any surrounding braces.

Then I can use this to build up an indexed list of each instance of each variable name and use that in some tools to inform the developer wherever an instance of a variable name is used or defined.

Sort of an "Intellisense" for BASH if you will.

Thanks in advance for any help with this.
# 2  
Old 09-25-2014
What is your desired output:
something like this?
Code:
$ awk -F'[$][{]' '{for(i=2; i<=NF; i++){sub(/\}.*/,x,$i); print NR, $i}}' file
1 CSV
2 EXPECTED_FILE
4 TT
4 FQN%/*
4 SCRIPT_NAME
4 XML_REPORT
5 CSV
5 SCRIPT_NAME

or like this:
Code:
$ awk -F'[$][{]' '{for(i=2; i<=NF; i++){sub(/[^_A-Z0-9].*/,x,$i); print NR, $i}}' file
1 CSV
2 EXPECTED_FILE
4 TT
4 FQN
4 SCRIPT_NAME
4 XML_REPORT
5 CSV
5 SCRIPT_NAME

---
Or with GNU grep ( if compiled with Perl regex option):
Code:
$ grep -nPo '(?<=\$\{).*?(?=\})' file
1:CSV
2:EXPECTED_FILE
4:TT
4:FQN%/*
4:SCRIPT_NAME
4:XML_REPORT
5:CSV
5:SCRIPT_NAME

or

Code:
$ grep -nPo '(?<=\$\{).*?(?=[^_A-Z0-9])' file
1:CSV
2:EXPECTED_FILE
4:TT
4:FQN
4:SCRIPT_NAME
4:XML_REPORT
5:CSV
5:SCRIPT_NAME


Last edited by Scrutinizer; 09-25-2014 at 09:55 AM..
# 3  
Old 09-25-2014
Is it sort of something like this you are after? Or have I got the wrong end of the stick here:

Code:
awk 'NR==1{print "Line numbers containing variables reported below:"}
    {
    a=0
    for (i=1;i<=NF;i++) {
        if($i ~ /\${.+}/) {
            a=gsub(/[^_A-Z0-9]/,"",$i)
            v[$i]
            }
    }
if (a) {
     print NR
     }
}
END{
    print "\n\nUnique variables reported below:"
    for (x in v) {
        print x
        }
}' inputfile

# 4  
Old 09-25-2014
Many Thanks

Thank you both for jumping on this so quickly Smilie

This is perfect for what I need:

Code:
awk -F'[$][{]' '{for(i=2; i<=NF; i++){sub(/[^_A-Z0-9].*/,x,$i); print NR, $i}}'

Cheers
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

TCSH scripts that use the same variable names

If I run two different TCSH scripts simultaneously that use identical variable names will this cause any problems? (1 Reply)
Discussion started by: thibodc
1 Replies

2. Shell Programming and Scripting

[SHELL: /bin/sh] For loop using variable variable names

Simple enough problem I think, I just can't seem to get it right. The below doesn't work as intended, it's just a function defined in a much larger script: CheckValues() { for field in \ Group_ID \ Group_Title \ Rule_ID \ Rule_Severity \ ... (2 Replies)
Discussion started by: Vryali
2 Replies

3. UNIX for Dummies Questions & Answers

Incrementing Variable Names

Hi, I am using BASH. I have encountered a situation where the following is necessary (but I am not sure how to do it): #Define multiple arrays, whose names only differ by a number: ARRAY_1=(1 2 3) ARRAY_2=(4 5 6) ARRAY_3=(7 8 9) #Define ARRAY_AMOUNT, the number of arrays. In this case... (1 Reply)
Discussion started by: msb65
1 Replies

4. Shell Programming and Scripting

File Names in a Variable in a loop

Hi All , I am having confusion in a shell script. Please guide me. I need to get multiple files (number of files vary time to time, file names are separated by '|') using FTP get from the remote server. Actually, i call the FTP function in a loop. At the last step, i need to move all the get... (3 Replies)
Discussion started by: spkandy
3 Replies

5. Shell Programming and Scripting

Variable names within array call

I am trying to write a piece of code that will call a value from an array. There are multiple arrays that I need to call data from. Only one array needs to be used based on the step within the program. The arrays have the names "cue_0", "cue_1", and so on. I can't figure out how to call a value... (2 Replies)
Discussion started by: vockleya
2 Replies

6. UNIX for Dummies Questions & Answers

Variable Names With Numbers

Hi Everyone, I was just curious if you are allowed to begin a variable name with a number. For example, I would really like to have the variable 8DAY_AVERAGE. But my shell script only seems to allow EIGHTDAY_AVERAGE. Is there a way I can get the former to work? I am using bash. Thanks a lot! ... (1 Reply)
Discussion started by: msb65
1 Replies

7. Shell Programming and Scripting

awk computed variable names

Is there a way to do make-style computed variable names in awk? e.g. in make foo = bar bar = wocket I can get "wocket" with $($(foo)) Alternatively can you list all defined variables in awk? thanks (4 Replies)
Discussion started by: craig06y
4 Replies

8. UNIX for Dummies Questions & Answers

Variable names

Hi, I have a variable v_iteration which can equal any 3 digit number eg 001 or 926 I would like to dynamically make a new variable name up using this 3 digit number eg v_another_variable_001=fred v_another_variable_926=joe The following are examples of what I have tried ... (2 Replies)
Discussion started by: Bab00shka
2 Replies

9. UNIX for Dummies Questions & Answers

Variable assignment for file names.

I am trying to process error files in selected directories. I can count the files that are there and export the contents to a file for either emailing or printing. The next step is to move the files to a processed directory with the name changed to .fixed as the last extension. for file in... (2 Replies)
Discussion started by: jagannatha
2 Replies

10. Shell Programming and Scripting

Variable names

Hi I have several variables called var1, var2, var3, var4 and so on. I would like to examine the contents of the variables using a loop and a variable called num which equals a figure eg num=3 I wanted to do something like echo $var$num to display the contents of var3 (4 Replies)
Discussion started by: Bab00shka
4 Replies
Login or Register to Ask a Question