Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AWK Script

Status
Not open for further replies.

abovebrd

IS-IT--Management
May 9, 2000
690
US
AWK seems to be pretty powerful. I have started writing my own shell scripts. I am having trouble with an awk script I am tring to create.<br><br>I have a directory that is populated with a random amount of files, usally 500 to 1000 files. I need to replace a regular expresion match with the contents of another file.<br><br>How would I do that ?<br><br>Any Idea ?<br><br>The regular expression is not=<br><br>I then need to insert the contents of another file in it place.<br><br>??<br><br> <p>-Danny<br><a href=mailto: > </a><br><a href= > </a><br>
 
Danny,<br><br>One option is to use sed (the stream editor) in a for loop to search for that string and replace it with another.<br><br>Alternatively, you could use a for loop to run this awk script for each file:<br><br>--------------------------------- 8&lt; ---------------------------------<br><FONT FACE=monospace><br>#!/usr/bin/awk -f<br>/not=/ {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while ((getline x &lt; &quot;otherfile&quot;) &gt; 0) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print x<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;next<br>}<br>{ <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print $0 <br>}<br></font><br>--------------------------------- 8&lt; ---------------------------------<br><br>where otherfile is the name of the file containing the replacement text.&nbsp;&nbsp;I'll leave the shell scripting to run it as an excercise for you!<br><br>When it matches the 'not=' string the corresponding piece of code is executed.&nbsp;&nbsp;The 'next' statement makes it skip on to the next line to avoid executing the last paragraph of code when lines do contain the 'not=' string.&nbsp;&nbsp;The last paragraph would normally be executed for every line of input.<br><br>BTW, this script will replace the entire line containing the not= string, even if it contains other text.<br><br>Annihilannic.
 
Annihilannic,<br><br>Thanks for your assistance. I appreciate the help. <p>-Danny<br><a href=mailto: > </a><br><a href= > </a><br>
 
I have files that include code within them.

I'd like to create an awk script that would count number of lines of code only within a file, that is anything that doesn't have a comment.

/* comment */ is a comment

/*comment*/ main() is one line of code despite there is a comment before the function main function

no empty lines are to be accounted for

???

-mo
 
Monash,

Try this:

[tt]--------------------------- 8< ----------------------------
#!/usr/bin/awk -f
BEGIN {
lines=0
}
# Match empty lines (even if they contains spaces or tabs)
/^[ ]*$/ {
print lines &quot; : &quot; $0
next
}
# Match lines containing a comment but no code
/^[ ]*\/\*.*\*\/[ ]*$/ {
print lines &quot; : &quot; $0
next
}
# Match all other lines (which should contain code)
{
lines++
print lines &quot; : &quot; $0
next
}
END {
print &quot;&quot;
print lines &quot; lines of code&quot;
}
--------------------------- 8< ----------------------------[/tt]

If you like you could take out the 'print lines' statements to have less verbose output.

Annihilannic. [sig][/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top