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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Conditioning in awk script 2

Status
Not open for further replies.

MCubitt

Programmer
Mar 14, 2002
1,081
GB
I have a script which is used by awk, helped in creation by people from here.

I understand this:
Code:
preceed="";if($0~"lk"olddb"$" || $0~olddb".ora$")preceed="#"

says if the input line ($0) contains (~) "lk" followed by the variable olddb's value at the end ($) OR the input string contains the variable olddb's value followed by ".ora", at the end ($) then set proceed to "#"

How can I use an extended line or series of lines on a similar test:
Code:
if [ $0~".ctl$" ] 
then
 printf "%s -p %s %s\n", "echo rm", $0  >>rscript
 printf "%s -p %s %s\n", "echo rm", $0  " >> "restlogfile >>rscript
 printf "%s -p %s %s\n", "rm", $0  " >> "restlogfile " 2>> "restlogfile >>rscript
endif

to say if the input line ends in ".ctl" then print three lines.

However, [] do not seem to work and even trying three lines of IF... with () and printf... fails.

Please assist!

Thanks


Applications Support
UK
 
Use brackets {} and separate statements with semicolon ; --
Code:
if ( $0~".ctl$" ) {
 printf ...;
 printf ...;
}

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
FYI: forum271

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Thanks LBK, that's great!

Vlad, I should have known shouldn't I? Only UNIX commands could possibly have forums for themselves!!!

thanks



Applications Support
UK
 
LBK, Actually, I get an error:
awk: 0602-565 There are not enough parameters in printf statement %s -p %s %s
.

Code:
if ($0~".ctl$") {
 printf "%s -p %s %s\n", "echo rm", $0  >>rscript;
 printf "%s -p %s %s\n", "echo rm", $0  " >> "restlogfile >>rscript;
 printf "%s -p %s %s\n", "rm", $0  " >> "restlogfile " 2>> "restlogfile >>rscript;
}
printf "%s -p %s %s\n", preceed"echo cp", backup, $0  >>rscript
printf "%s -p %s %s\n", preceed"echo cp", backup, $0 " >> "restlogfile >>rscript
printf "%s -p %s %s\n", preceed"cp", backup, $0 " >> "restlogfile " 2>> "restlogfile >>rscript
}

I cannot see why... It is the 1st printf that fails.





Applications Support
UK
 
man awk
You have 3 %s in your printf format, so you need to provide 3 parameters instead of only 2 ("echo rm", $0)

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
PHV, thanks, it makes sense now.



Applications Support
UK
 
yep, welcome to the world of real OS's with rich toolchest.

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
I always liked my OS400..


Applications Support
UK
 
Use brackets {} and separate statements with semicolon ;
Wrong. Awk is different from C and Perl in that respect. Statements on separate lines do not need trailing semicolons. Semicolons are only needed to separate multiple statements on one line.
[tt]
if ($0~".ctl$") {
[/tt]
Whenever possible, reqular expressions should be enclosed in slashes instead of quotes. If the regular expression is enclosed in slashes and you are matching against $0, $0 can be omitted. The character . is a metacharacter, so to match only a literal ., it should be escaped.
Code:
if ( /\.ctl$/ ) {
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top