Sponsored Content
Top Forums Shell Programming and Scripting Sysdate inside awk print statement Post 302537365 by Peasant on Friday 8th of July 2011 01:53:29 AM
Old 07-08-2011
See if you can use this inside your code :
Code:
awk -v sysdate=$(date "+%Y%d%m") ' { print sysdate } '

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

variable inside awk '{print $c}'

i'm trying to do this (in bash darwin); echo "give me some words: " read a c=2 # this is get by other ways echo $a | awk '{print $c}' # i want to print the column given # by de $c variable if there is someone understand what i pretend... (3 Replies)
Discussion started by: Tártaro
3 Replies

2. Shell Programming and Scripting

print argv inside awk

mode=$1 psg telnetd | awk current=`date +%M`'{ printf ("mode is %s",mode) printf ("mode is %s",ARGV) }' at command prompt when i run the script along with the argument i get only-- 'mode is ' argument is not printed.(If the argument is... (3 Replies)
Discussion started by: Anteus
3 Replies

3. Shell Programming and Scripting

Print to 2 files in awk if statement

Hi all, I have some code like this awk -F, '{ if ($1==3) print $2 > "output_file" print "1" > "new_file" }' "input_file" When I check output_file this has the correct values in it. However the new_file has 1 in it for every line in the input_file. If the input file has 20 lins then... (2 Replies)
Discussion started by: Donkey25
2 Replies

4. Shell Programming and Scripting

awk inside another awk statement

hi all, i have two files 1) a.txt one two three 2) abc "one" = 10 pqr "three" = 20 345 "two" = 0 this is what i want in third file (3 Replies)
Discussion started by: shishirkotkar
3 Replies

5. Shell Programming and Scripting

Can't print multiple lines inside awk :(

Hi Friends, I have small issue with following code snippet. I am trying call one function inside awk in which the function inturn will echo few lines. However when i ran script its throwing an error saying "nawk: syntax error at source line 1". #!/bin/sh eval input=$@ while read... (3 Replies)
Discussion started by: Shahul
3 Replies

6. Shell Programming and Scripting

use awk print statement for two files

Hello, is there a way to use the awk print statement on two files at once? I would like to take two columns from one file, and one column from another file and print them as consecutive columns to a third file. Seems simple...as in: file 1 1 a 2 b 3 c 4 d 5 e file 2 1 t 2 u... (3 Replies)
Discussion started by: HugoHuhn
3 Replies

7. Shell Programming and Scripting

Explanation of print statement - awk

Hi, i'm just after a simple explanation of how the following awk oneliner works. gawk -F"," '{for(i=m;i<=n;i++)printf "%s" OFS,$i; print x}' m=1 n=70 OFS=, input.csv > output.csv In particular i'm trying to understand how the two print statements work? How is the "x" variable being assigned... (3 Replies)
Discussion started by: theflamingmoe
3 Replies

8. Shell Programming and Scripting

For loop inside awk to read and print contents of files

Hello, I have a set of files Xfile0001 - Xfile0021, and the content of this files (one at a time) needs to be printed between some line (lines start with word "Generated") that I am extracting from another file called file7.txt and all the output goes into output.txt. First I tried creating a for... (5 Replies)
Discussion started by: jaldo0805
5 Replies

9. Shell Programming and Scripting

Command in inside awk statement

Hello can you please help me with below script which is meant to delete clients from multiple netbackup policies I want to run a command insdie awk statement apparelnlty this script is not working for me for i in $( cat clients_list) do bppllist -byclient $i | awk... (6 Replies)
Discussion started by: Sara_84
6 Replies

10. Shell Programming and Scripting

awk statement piped inside sed

Hello folks, I have multiple occurrences of the pattern: ).: where is any digit, in various text context but the pattern is unique as this regex. And I need to turn this decimal fraction into an integer (corresponding percent value: the range of 0-100). What I'm doing is: cat... (1 Reply)
Discussion started by: roussine
1 Replies
DBIAgent(3pm)						User Contributed Perl Documentation					     DBIAgent(3pm)

NAME
POE::Component::DBIAgent - POE Component for running asynchronous DBI calls. SYNOPSIS
sub _start { my ($self, $kernel, $heap) = @_[OBJECT, KERNEL, HEAP]; $heap->{helper} = POE::Component::DBIAgent->new( DSN => [$dsn, $username, $password ], Queries => $self->make_queries, Count => 3, Debug => 1, ); # Queries takes a hashref of the form: # { query_name => 'select blah from table where x = ?', # other_query => 'select blah_blah from big_view', # etc. # } $heap->{helper}->query(query_name => { cookie => 'starting_query' }, session => 'get_row_from_dbiagent'); } sub get_row_from_dbiagent { my ($kernel, $self, $heap, $row, $cookie) = @_[KERNEL, OBJECT, HEAP, ARG0, ARG1]; if ($row ne 'EOF') { # {{{ PROCESS A ROW #row is a listref of columns # }}} PROCESS A ROW } else { # {{{ NO MORE ROWS #cleanup code here # }}} NO MORE ROWS } } DESCRIPTION
DBIAgent is your answer to non-blocking DBI in POE. It fires off a configurable number child processes (defaults to 3) and feeds database queries to it via two-way pipe (or sockets ... however POE::Component::Wheel::Run is able to manage it). The primary method is "query". Usage After initializing a DBIAgent and storing it in a session's heap, one executes a "query" (or "query_slow") with the query name, destination session (name or id) and destination state (as well as any query parameters, optionally) as arguments. As each row of data comes back from the query, the destination state (in the destination session) is invoked with that row of data in its $_[ARG0] slot. When there are no more rows to return, the data in $_[ARG0] is the string 'EOF'. Not EVERY query should run through the DBIAgent. If you need to run a short lookup from within a state, sometimes it can be a hassle to have to define a whole separate state to receive its value, and resume processing from there.. The determining factor, of course, is how long your query will take to execute. If you are trying to retrieve one row from a properly indexed table, use "$dbh->selectrow_array()". If there's a join involved, or multiple rows, or a view, you probably want to use DBIAgent. If it's a longish query and startup costs (time) don't matter to you, go ahead and do it inline.. but remember the whole of your program suspends waiting for the result. If startup costs DO matter, use DBIAgent. Return Values The destination state in the destination session (specified in the call to "query()") will receive the return values from the query in its $_[ARG0] parameter. DBIAgent invokes DBI's "fetch" method internally, so the value will be a reference to an array. If your query returns multiple rows, then your state will be invoked multiple times, once per row. ADDITIONALLY, your state will be called one time with $_[ARG0] containing the string 'EOF'. 'EOF' is returned even if the query doesn't return any other rows. This is also what to expect for DML (INSERT, UPDATE, DELETE) queries. A way to utilise this might be as follows: sub some_state { #... if ($enough_values_to_begin_updating) { $heap->{dbiagent}->query(update_values_query => this_session => update_next_value => shift @{$heap->{values_to_be_updated}} ); } } sub update_next_value { my ($self, $heap) = @_[OBJECT, HEAP]; # we got 'EOF' in ARG0 here but we don't care... we know that an # update has been executed. for (1..3) { # Do three at a time! my $value; last unless defined ($value = shift @{$heap->{values_to_be_updated}}); $heap->{dbiagent}->query(update_values => this_session => update_next_value => $value ); } } new() Creating an instance creates a POE::Session to manage communication with the Helper processes. Queue management is transparent and automatic. The constructor is named "new()" (surprised, eh? Yeah, me too). The parameters are as follows: DSN An arrayref of parameters to pass to DBI->connect (usually a dsn, username, and password). Queries A hashref of the form Query_Name => "$SQL". For example: { sysdate => "select sysdate from dual", employee_record => "select * from emp where id = ?", increase_inventory => "update inventory set count = count + ? where item_id = ?", } As the example indicates, DBI placeholders are supported, as are DML statements. Count The number of helper processes to spawn. Defaults to 3. The optimal value for this parameter will depend on several factors, such as: how many different queries your program will be running, how much RAM you have, how often you run queries, and most importantly, how many queries you intend to run simultaneously. ErrorState An listref containing a session and event name to receive error messages from the DBI. The message arrives in ARG0. query($query_name, [ \%args, ] $session, $state, [ @parameters ]) The "query()" method takes at least three parameters, plus any bind values for the specific query you are executing. $query_name This parameter must be one of the keys to the Queries hashref you passed to the constructor. It is used to indicate which query you wish to execute. \%args This is an OPTIONAL hashref of arguments to pass to the query. Currently supported arguments: hash Return rows hash references instead of array references. cookie A cookie to pass to this query. This is passed back unchanged to the destination state in $_[ARG1]. Can be any scalar (including references, and even POE postbacks, so be careful!). You can use this as an identifier if you have one destination state handling multiple different queries or sessions. delay Insert a 1ms delay between each row of output. I know what you're thinking: "WHY would you want to slow down query responses?!?!?" It has to do with CONCURRENCY. When a response (finally) comes in from the agent after running the query, it floods the input channel with response data. This has the effect of monopolizing POE's attention, so that any other handles (network sockets, pipes, file descriptors) keep getting pushed further back on the queue, and to all other processes EXCEPT the agent, your POE program looks hung for the amount of time it takes to process all of the incoming query data. So, we insert 1ms of time via Time::HiRes's "usleep" function. In human terms, this is essentially negligible. But it is just enough time to allow competing handles (sockets, files) to trigger "select()", and get handled by the POE::Kernel, in situations where concurrency has priority over transfer rate. Naturally, the Time::HiRes module is required for this functionality. If Time::HiRes is not installed, the delay is ignored. group Sends the return event back when "group" rows are retrieved from the database, to avoid event spam when selecting lots of rows. NB: using group means that $row will be an arrayref of rows, not just a single row. $session, $state These parameters indicate the POE state that is to receive the data returned from the database. The state indicated will receive the data in its $_[ARG0] parameter. PLEASE make sure this is a valid state, otherwise you will spend a LOT of time banging your head against the wall wondering where your query data is. @parameters These are any parameters your query requires. WARNING: You must supply exactly as many parameters as your query has placeholders! This means that if your query has NO placeholders, then you should pass NO extra parameters to "query". Suggestions to improve this syntax are welcome. finish() The "finish()" method tells DBIAgent that the program is finished sending queries. DBIAgent will shut its helpers down gracefully after they complete any pending queries. If there are no pending queries, the DBIAgent will shut down immediately. NOTES
o Error handling is practically non-existent. o The calling syntax is still pretty weak... but improving. We may eventually add an optional attributes hash so that each query can be called with its own individual characteristics. o I might eventually want to support returning hashrefs, if there is any demand. o Every query is prepared at Helper startup. This could potentially be pretty expensive. Perhaps a cached or deferred loading might be better? This is considering that not every helper is going to run every query, especially if you have a lot of miscellaneous queries. Suggestions welcome! Diffs more welcome! :-) AUTHOR
This module has been fine-tuned and packaged by Rob Bloodgood <robb@empire2.com>. However, most of the queuing code originated with Fletch <fletch@phydeaux.org>, either directly or via his ideas. Thank you for making this module a reality, Fletch! However, I own all of the bugs. This module is free software; you may redistribute it and/or modify it under the same terms as Perl itself. perl v5.10.1 2010-04-10 DBIAgent(3pm)
All times are GMT -4. The time now is 02:28 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy