Sponsored Content
Full Discussion: BEGIN END questions
Top Forums Shell Programming and Scripting BEGIN END questions Post 302213987 by radoulov on Friday 11th of July 2008 03:47:17 PM
Old 07-11-2008
Quote:
Originally Posted by llsmr777
Why would you need to use this in a script?
Why can't you just use print to print out what you want printed in the begining and print for what you want at the end.

So this:

Code:
nawk 'BEGIN {print "this is the first line"}
{print $1 $2 $3}
{print $5 $6}
END {print "this is the last line"}'

why can't i just or why would I not use:

Code:
print "this is the first line"
nawk 
'{print $1 $2 $3}
{print $5 $6}'
print "this is the last line"

Because you may need something like this or much more:

Code:
awk 'BEGIN {
  printf "Number of arguments: %d\n",
    ARGC
    }
END {
  printf "Number of records read: %d\n",
    NR
}' input

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

begin end detection

Hi all, i am new to scripting. i need to write a code to detect begin and end of word that either begins or ends with t,th,d,dh,s,sh i have a set of words in a file containg one word per line. let the filename be aaa.txt. i have an another file bbb.txt which has two lines, just specifying the... (7 Replies)
Discussion started by: blkanth
7 Replies

2. Shell Programming and Scripting

awk BEGIN END and string matching problem

Hi, Contents of BBS-list file: foo foo foo awk ' BEGIN { print "Analysis of \"foo\"" } /foo/ { ++n } END { print "\"foo\" appears", n, "times." }' BBS-list Output: Analysis of "foo" "foo" appears 3 times. awk ' (3 Replies)
Discussion started by: cola
3 Replies

3. Shell Programming and Scripting

Use of Begin IF ,END IF END not working in the sql script

Hi I have written a script .The script runs properly if i write sql queries .But if i use PLSQL commands of BEGIN if end if , end ,then on running the script the comamds are getting printed on the prompt . Ex :temp.sql After connecting to the databse at the sql prompt i type... (1 Reply)
Discussion started by: isha_1
1 Replies

4. UNIX for Dummies Questions & Answers

Dump to another file for a begin and end Pattern

Hi All , I am newbie to linux shell scripting , below are the contents of my log file , i want the lines between a begin pattern and a end pattern for an instance , my begin Pattern is "Transaction Begins for Usr" and end pattern is "Transaction Ends for Usr" into another file Sample file... (1 Reply)
Discussion started by: SARAVANANE
1 Replies

5. Programming

search a file between two begin and end strings in c

Can any one help me out with following problem... I want to search in a file which has two strings repeat each time(like start and end) i want to search between these two string in C programming. please help me with the solution. thanks in advance. (8 Replies)
Discussion started by: uday.sena.m
8 Replies

6. Shell Programming and Scripting

Get the sum of values in between begin and end in the file

Hi All, test file Begin Script Run at Thu Mar 14 09:24:16 PDT 2013 tst_accounts: ws zip: WS_out_20130313.tar.gz dat: test_20130313.dat count: 63574 loaded: xx pre-merge: xx post-merge: xx timestamp: Thu Mar 14 09:30:42 PDT 2013 tst_accounts: ws zip: WS_out_20130313.tar.gz dat: s_20130313.dat... (6 Replies)
Discussion started by: bmk
6 Replies

7. Shell Programming and Scripting

Begin/End blocks in awk: confused

I am trying to understand how to use the END block in awk without much success. I have this script that I found: gawk '{count++; keyword = $1} if (count == 3) keyword = "order this" else print keyword " " k } }' << orderfile Is that the way that the END block should be used? I am... (6 Replies)
Discussion started by: newbie2010
6 Replies

8. Shell Programming and Scripting

BEGIN and END format in awk

I'm new to awk, trying to understand the basics. I'm trying to reset the counter everytime the program gets a new file to check. I figured in the BEGIN part it would work, but it doesn't. #!/bin/awk -f BEGIN {counter=0} { sum=0 for ( i=1; i<=NF;... (1 Reply)
Discussion started by: guitarist684
1 Replies

9. Shell Programming and Scripting

Search ad replace using begin and end of the file

Hello Friends , Please help to create script for compare and replace if not matches of set of lines . * Primary* Servername Server1 Location R201 Rack 4 *End Primary* *Secondary* Server Name Server1 IPAddress 10.24.30.10 Application Apache *End of Secondary* Above... (4 Replies)
Discussion started by: rnary
4 Replies

10. Shell Programming and Scripting

In ksh script what is this BEGIN and END in this function?

Can Someone please explain why BEGIN and END statement is used inside function? How does that help in scripting? function fileformatting { CleanupMask="xXxX" sed 's/^.//' < ${AllFile} > ${AllFile}.tmp echo $(wc -l ${AllFile}.tmp) `awk -v CleanupMask=${CleanupMask} ' BEGIN... (2 Replies)
Discussion started by: later_troy
2 Replies
apache_mod_perl-108~358::mod_perl-2.0.7::docs::api::ModPUser:Contributed Papache_mod_perl-108~358::mod_perl-2.0.7::docs::api::ModPerl::Registry(3)

NAME
ModPerl::Registry - Run unaltered CGI scripts persistently under mod_perl Synopsis # httpd.conf PerlModule ModPerl::Registry Alias /perl/ /home/httpd/perl/ <Location /perl> SetHandler perl-script PerlResponseHandler ModPerl::Registry #PerlOptions +ParseHeaders #PerlOptions -GlobalRequest Options +ExecCGI </Location> Description URIs in the form of "http://example.com/perl/test.pl" will be compiled as the body of a Perl subroutine and executed. Each child process will compile the subroutine once and store it in memory. It will recompile it whenever the file (e.g. test.pl in our example) is updated on disk. Think of it as an object oriented server with each script implementing a class loaded at runtime. The file looks much like a "normal" script, but it is compiled into a subroutine. For example: my $r = Apache2::RequestUtil->request; $r->content_type("text/html"); $r->send_http_header; $r->print("mod_perl rules!"); XXX: STOPPED here. Below is the old Apache::Registry document which I haven't worked through yet. META: document that for now we don't chdir() into the script's dir, because it affects the whole process under threads. "ModPerl::RegistryPrefork" should be used by those who run only under prefork MPM. This module emulates the CGI environment, allowing programmers to write scripts that run under CGI or mod_perl without change. Existing CGI scripts may require some changes, simply because a CGI script has a very short lifetime of one HTTP request, allowing you to get away with "quick and dirty" scripting. Using mod_perl and ModPerl::Registry requires you to be more careful, but it also gives new meaning to the word "quick"! Be sure to read all mod_perl related documentation for more details, including instructions for setting up an environment that looks exactly like CGI: print "Content-type: text/html "; print "Hi There!"; Note that each httpd process or "child" must compile each script once, so the first request to one server may seem slow, but each request there after will be faster. If your scripts are large and/or make use of many Perl modules, this difference should be noticeable to the human eye. DirectoryIndex If you are trying setup a DirectoryIndex under a Location covered by ModPerl::Registry* you might run into some trouble. META: if this gets added to core, replace with real documenation. See http://marc.theaimsgroup.com/?l=apache-modperl&m=112805393100758&w=2 Special Blocks "BEGIN" Blocks "BEGIN" blocks defined in scripts running under the "ModPerl::Registry" handler behave similarly to the normal mod_perl handlers plus: o Only once, if pulled in by the parent process via "Apache2::RegistryLoader". o An additional time, once per child process or Perl interpreter, each time the script file changes on disk. "BEGIN" blocks defined in modules loaded from "ModPerl::Registry" scripts behave identically to the normal mod_perl handlers, regardless of whether they define a package or not. "CHECK" and "INIT" Blocks Same as normal mod_perl handlers. "END" Blocks "END" blocks encountered during compilation of a script, are called after the script has completed its run, including subsequent invocations when the script is cached in memory. This is assuming that the script itself doesn't define a package on its own. If the script defines its own package, the "END" blocks in the scope of that package will be executed at the end of the interpretor's life. "END" blocks residing in modules loaded by registry script will be executed only once, when the interpreter exits. Security "ModPerl::Registry::handler" performs the same sanity checks as mod_cgi does, before running the script. Environment The Apache function `exit' overrides the Perl core built-in function. Commandline Switches In First Line Normally when a Perl script is run from the command line or under CGI, arguments on the `#!' line are passed to the perl interpreter for processing. "ModPerl::Registry" currently only honors the -w switch and will enable the "warnings" pragma in such case. Another common switch used with CGI scripts is -T to turn on taint checking. This can only be enabled when the server starts with the configuration directive: PerlSwitches -T However, if taint checking is not enabled, but the -T switch is seen, "ModPerl::Registry" will write a warning to the error_log file. Debugging You may set the debug level with the $ModPerl::Registry::Debug bitmask 1 => log recompile in errorlog 2 => ModPerl::Debug::dump in case of $@ 4 => trace pedantically Caveats ModPerl::Registry makes things look just the CGI environment, however, you must understand that this *is not CGI*. Each httpd child will compile your script into memory and keep it there, whereas CGI will run it once, cleaning out the entire process space. Many times you have heard "always use "-w", always use "-w" and 'use strict'". This is more important here than anywhere else! Some other important caveats to keep in mind are discussed on the Perl Reference page. Authors Andreas J. Koenig, Doug MacEachern and Stas Bekman. See Also "ModPerl::RegistryCooker", "ModPerl::RegistryBB" and "ModPerl::PerlRun". perl v5.16.2 2011-0apache_mod_perl-108~358::mod_perl-2.0.7::docs::api::ModPerl::Registry(3)
All times are GMT -4. The time now is 02:27 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy