Variable syntax and lines of code


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Variable syntax and lines of code
# 1  
Old 07-10-2008
Question Variable syntax and lines of code

I am trying to use use the AWK and EGREP commands together in an AWK script. My overall objective is to count lines of code but exclude comments and blank lines and such.

I am able to use AWK with the FIND command like so:

Code:
| awk '{print \"wc -l \"$1}' > lineCount.sh

Now when I modify the line to use EGREP, I get a 'Variable Syntax' error

Code:
"| awk '{print \"egrep -cv '^$|^[*]|^[ *]|^[//]|^#|[\\n]'  \"$1}' > lineCount.sh

Regular expressions are new to me and they seem to be a bit painful. Any help is appreciated.
# 2  
Old 07-10-2008
You already have a thread about this. What comments? C like /* */ or hashes like for shell scripts #?

Post some examples maybe.
# 3  
Old 07-10-2008
Quote:
Originally Posted by zaxxon
You already have a thread about this. What comments? C like /* */ or hashes like for shell scripts #?

Post some examples maybe.
I have a script that finds different file types and does a word count on them using AWK. I am trying to use EGREP instead to count the lines in the files. Right now, my task is to count lines for JavaScript files. here is my test file

Code:
************
* SLOC file test
************

var hipHop = false;
        # I want to see if I can get egrep working




// This is only a test.

temp = temp * 6;



/* Some function */

# 4  
Old 07-11-2008
Code:
root@isau02:/data/tmp/testfeld> cat infile
************
* SLOC file test
************

var hipHop = false;
        # I want to see if I can get egrep working




        // This is only a test.

        temp = temp * 6;



        /* Some function */
root@isau02:/data/tmp/testfeld> egrep -v "^$|^[[:space:]]*\*|^[[:space:]]*\#|^[[:space:]]]*\/" infile
var hipHop = false;
        temp = temp * 6;

You will have problems with comments like
Code:
/* here starts my
    comment and
    goes over
    several lines
    until this line     */

where you might want to use multiline patterns or addresses like /start/,/end/.
# 5  
Old 07-11-2008
Awesome. Now I tried adding that to the script and here is the result:
awk: syntax error near line 172
awk: illegal statement near line 172


And this is the line that fails me all the time.
Code:
outputString = outputString "| awk '{print egrep -cv "^$|^[[:space:]]*\*|^[[:space:]]*\#|^[[:space:]]]*\/ \"$1}' > lineCount.sh

Should there be a big difference with syntax in a script versus the command line?
# 6  
Old 07-11-2008
No offense, but I think you should take dig a bit deeper into learning to write shell scripts by some guide/howto/book, which will help a lot to write working code. There are plenty of good infos and guides about shell scripting on the web, but if you want some links, I can dig out what I have bookmarked.

Quote:
outputString = outputString "| awk '{print egrep -cv "^$|^[[:space:]]*\*|^[[:space:]]*\#|^[[:space:]]]*\/ \"$1}' > lineCount.sh
I am not sure what you are going to do, but if outputString has already some value like a String and you want to echo it, you need to write it like
Code:
outputString = `echo ${outputString} | awk .................... `

You should put your code to be executed in backticks or simple brackets so that it will be executed and assigned to the variablename in the front. Not sure, but maybe you want to assing the value to a different variable name, than that you are already using like (just a suggestion - not sure if you have your reasons to use it that way)
Code:
newString = `echo ${outputString} | awk .................... `

# 7  
Old 07-11-2008
If you are a professional, you really didn't need to post all that. Thanks for you help.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Variable syntax error in $?

hi all , i just tried to take the status of previous command inside the script using echo $?. It throws me a variable syntax error , but when i use echo $? as an individual command it works perfectly . can anyone Please tell me why am getting a variable syntax error when i use echo $?... (7 Replies)
Discussion started by: Rahul619
7 Replies

2. Shell Programming and Scripting

awk syntax(partial) in variable

if a variable has part of awk syntax stored in it. for eg: x=if($1>100) can we substitute this variable in an awk statement. based on above requirement can we execute something like: awk '{x print $1}' infile (5 Replies)
Discussion started by: 47shailesh
5 Replies

3. Shell Programming and Scripting

[Perl] Split lines into array - variable line items - variable no of lines.

Hi, I have the following lines that I would like to see in an array for easy comparisons and printing: Example 1: field1,field2,field3,field4,field5 value1,value2,value3,value4,value5Example 2: field1,field3,field4,field2,field5,field6,field7... (7 Replies)
Discussion started by: ejdv
7 Replies

4. Shell Programming and Scripting

Need help line 35: syntax error: unexpected end of file only 34 lines of code

I am not sure what I am doing wrong here, I did some research and only confused myself further. Any help would be greatly appreciated. I need to make this work for work tomorrow. There are only 34 lines of code in this script, yet its complaining about line 35 Here is the code: ... (7 Replies)
Discussion started by: BkontheShell718
7 Replies

5. Shell Programming and Scripting

Sed syntax to print lines that do not end with )}

I must admit I am relativly new to sed, and I am trying to make a simple sed query to search log files and return multiple variables from multiple logs. So far I have had success, but one problem remains: Alot of lines in the log files wrap onto multiple lines and are not being returned as... (2 Replies)
Discussion started by: demanche
2 Replies

6. Shell Programming and Scripting

Syntax error on variable assignment

Hello all, I have "inherited" a Korn shell script I'm supposed to maintain, and running a "sh -n" on it, I got this syntax error: script.sh: syntax error at line 63: `OB_DEVICE=$' unexpected The line in cause is the first occurence of the usage of perl one-liners. The whole line: ... (2 Replies)
Discussion started by: AdrianM
2 Replies

7. Shell Programming and Scripting

How do I write multiple variable syntax?

I am trying to write a script that will do a sql statement. But in the sql I will have a list (1,2,3,4). How would I go about asking for the input from the user running the script, and then transferring that back into the list as say ($var1,$var2,$var3)? It would be somewhat like this: ... (1 Reply)
Discussion started by: Captain
1 Replies

8. Solaris

C-shell: variable syntax question

There is a possibility to set a variable, having an another variable in it's name: prompt% setenv PRT one prompt% setenv VAR_${PRT} value prompt% So, this way the VAR_one = "value" and could be viewed: prompt% echo VAR_one value prompt% Q: How to view a variable having another... (0 Replies)
Discussion started by: alex_5161
0 Replies

9. UNIX for Dummies Questions & Answers

Setting a variable (need syntax help)

I need some syntax help (working in a bash shell) I have a variable which is a filename with an extension, and I need to create another variable with the same name but a different extension To explain, the input file should be called something like "filename.L1" and the output file should be... (1 Reply)
Discussion started by: Slanter
1 Replies

10. Shell Programming and Scripting

Syntax to export any variable

How to export variables on a UNIX prompt. Please provide me syntax. Thanks in advance. Malay (5 Replies)
Discussion started by: malaymaru
5 Replies
Login or Register to Ask a Question