AWK file multiline command not working - linux

I have an awk file get_PV_sql_from_file.awk:
#! /bin/awk -f
/^CREATE.*VIEW.*PV_/
{
inside_create = 1; print "view found"
}
{
if(inside_create == 1)
{
print $0
}
}
/.*;$/
{
if(inside_create == 1){
inside_create = 0;
}
}
And when i run it:
awk -f get_PV_sql_from_file.awk test_PV_sql.txt
I have got "view found" phrase in each line:
view found
CREATE FORCE VIEW "POSC"."P3V_SEC_TREES_V" ("BSASC_ID", "BSASC_S", "LV0_K", "LV0_V", "LV1_K", "LV1_V", "LV2_K", "LV2_V", "LV3_K", "LV3_V", "LV4_K", "LV4_V", "LV5_K", "LV5_V") AS
view found
SELECT DISTINCT
view found
B1.BSASC_ID,
view found
B2.BSASC_S,
view found
R_SEC_TREES.LV0_K,
view found
R_SEC_TREES.LV0_V,
...
etc
But if i write each command in awk file on a single line:
#! /bin/awk -f
/^CREATE.*VIEW.*PV_/ { inside_create = 1; print "view found" }
{ if(inside_create == 1) { print $0 } }
/.*;$/ { if(inside_create == 1) { inside_create = 0; } }
It is works right:
view found
CREATE FORCE VIEW "POSC"."PV_SEC_MODULES_V" ("BSASC_ID", "MODULE", "ACT") AS
SELECT DISTINCT
B1.BSASC_ID BSASC_ID,
BSASC_PRIVILEGE.GDLN_OR_PRIV_ID MODULE,
BSASC_PRIVILEGE.R_BSASC_PRIV_KIND ACT
...
etc
Why is this happening? And where i made mistake?

You need to put the pattern and the opening brace that begins the action on the same line. Otherwise the pattern is treated as not having any action, so the default action, print the line, is performed. The action on the next line is then treated as having no pattern which means it matches every line. So write your AWK code like this:
/^CREATE.*VIEW.*PV_/ {
inside_create = 1; print "view found"
}
{
if(inside_create == 1)
{
print $0
}
}
/.*;$/ {
if(inside_create == 1) {
inside_create = 0;
}
}

Related

perl remove string block from file and save to file

I have a file that looks like this:
string 1 {
abc { session 1 }
fairPrice {
ID LU0432618274456
Source 4
service xyz
}
}
string 2 {
abc { session 23 }
fairPrice {
ID LU036524565456171
Source 4
service tzu
}
}
My program should read in the file with a search-parameter given (for example "string 1") and search the complete block until "}" and remove that part from the file. Can someone assist on that...I have some code so far but how can I do the removal and saving to the same file again?
my $fh = IO::File->new( "$fname", "r" ) or die ( "ERROR: Strategy file \"$fname\" not found." );
while($line=<$fh>)
{
if ($line =~ /^\s*string 1\s*\w+\s*\{\s*$/) {
$inside_json_msg = 1;
$msg_json .= $line;
}
else {
if ($inside_json_msg)
{
if ($line =~ m/^\}\s*$/) {
$msg_json.= $line if defined($line);
$inside_json_msg = 0;
} else {
$msg_json .= $line;
}
}
}
}
You code mentions JSON, but your data isn't JSON. If it is JSON and you've just transcribed it badly, then please use a JSON library.
But if your data isn't JSON, then something like this will do the trick.
#!/usr/bin/perl
use strict;
use warnings;
my $match = shift or die "I need a string to match\n";
while (<DATA>) {
# If this is the start of a block we want to remove...
if (/^\s*$match\s+{/) {
# Set $braces to 1 (or 0 if the block closes on this line)
my $braces = /}/ ? 0 : 1;
# While $braces is non-zero
while ($braces) {
# Read the next line of the file
$_ = <DATA>;
# Increment or decrement $braces as appropriate
$braces-- if /}/;
$braces++ if /{/;
}
} else {
# Otherwise, just print the line
print;
}
}
__DATA__
string 1 {
abc { session 1 }
fairPrice {
ID LU0432618274456
Source 4
service xyz
}
}
string 2 {
abc { session 23 }
fairPrice {
ID LU036524565456171
Source 4
service tzu
}
}
Currently, this just prints the output to the console. And I use the DATA filehandle for easier testing. Switching to use real filehandles is left as an exercise for the reader :-)
Update: I decided that I didn't like all the incrementing and decrementing of $braces using regex matches. So here's another (improved?) version that uses y/.../.../ to count the occurrences of opening and closing braces in the line. It's possible that this version might be slightly less readable (the syntax highlighter certainly thinks so).
#!/usr/bin/perl
use strict;
use warnings;
my $match = shift or die "I need a string to match\n";
while (<DATA>) {
if (/^\s*$match\s+{/) {
my $braces = y/{// - y/}//;
while ($braces) {
$_ = <DATA>;
$braces -= y/}//;
$braces += y/{//;
}
} else {
print;
}
}
__DATA__
string 1 {
abc { session 1 }
fairPrice {
ID LU0432618274456
Source 4
service xyz
}
}
string 2 {
abc { session 23 }
fairPrice {
ID LU036524565456171
Source 4
service tzu
}
}
Update 2: Ok, I originally said that dealing with real filehandles would be left as an exercise for the reader. But here's a version that does that.
#!/usr/bin/perl
use strict;
use warnings;
my $match = shift or die "I need a string to match\n";
open my $fh, '+<', 'data' or die $!;
# Read all the data from the file
my #data = <$fh>;
# Empty the file
seek $fh, 0, 0;
truncate $fh, 0;
my $x = 0;
while ($x <= $#data) {
$_ = $data[$x++];
if (/^\s*$match\s+{/) {
my $braces = y/{// - y/}//;
while ($braces) {
$_ = $data[$x++];
$braces -= y/}//;
$braces += y/{//;
}
} else {
print $fh $_;
}
}
Currently, I've hard-coded the filename to be data. I hope it's obvious how to fix that.
Can use Text::Balanced to break the text into blocks delimited by {}, in a way that also keeps the text preceding and following the blocks.
In that list drop the element with the specific skip-pattern (string 1 here) and its following block and retain everything else. Then overwrite the source file with that.
use warnings;
use strict;
use Path::Tiny;
use Text::Balanced qw(extract_bracketed extract_multiple);
my $file = shift // die "Usage: $0 file\n"; #/
my $text = path($file)->slurp;
# returns: 'string 1', BLOCK, 'string 2', BLOCK (may have spaces/newlines)
my #elems = extract_multiple(
$text, [ sub { extract_bracketed($text, '{}') } ]
);
my $skip_phrase = 'string 1';
my (#text_keep, $skip);
for (#elems) {
if (/$skip_phrase/) {
$skip = 1;
next;
}
elsif ($skip) {
$skip = 0;
next
}
push #text_keep, $_;
}
print for #text_keep;
# Overwrite source; uncomment when tested
#open my $fh_out, '>', $file or die "Can't open $file: $!";
#print $fh_out $_ for #text_keep;
Tested with files with more text and blocks, both before and after the one to drop.
Another tool that can be used to extract delimited chunks is in Regexp::Common, see this post.
I would use proper json as format and jq as processor for that format. Rewriting a hack in perl does not make much sense.
Here is an example using Regexp::Grammars:
use feature qw(say);
use strict;
use warnings;
use Data::Printer;
use Regexp::Grammars;
{
my ($block_name, $block_num) = #ARGV;
my $parser = qr!
<nocontext:>
<blocks>
<rule: blocks> <[block]>+
<rule: block> <block_name> <block_num> <braced_item>
<token: block_name> \w+
<token: block_num> \d+
<rule: braced_item> \{ (?: <escape> | <braced_item> | [^{}] )* \}
<token: escape> \\ .
!xms;
my $data = read_file('cfg.txt');
if ($data =~ $parser) {
print_blocks( $/{blocks}{block}, $block_name, $block_num );
}
else {
warn "No match";
}
}
sub print_blocks {
my ( $blocks, $block_name, $block_num ) = #_;
for my $block (#$blocks) {
next if ($block->{block_name} eq $block_name)
&& ($block->{block_num} == $block_num);
say $block->{block_name}, " ", $block->{block_num},
" ", $block->{braced_item}{braced_item};
}
}
sub read_file {
my ( $fn ) = #_;
open ( my $fh, '<', $fn ) or die "Could not open file '$fn': $!";
my $str = do { local $/; <$fh> };
close $fh;
return $str;
}

What is wrong in this grep command?

Okay... so I'm trying to make a dictionary using Crunch and Grep I've also tried using Perl instead.
crunch 8 12 1234567890 -d 2# | grep -v '\([0-9]\) .*\1.*\1.*\1.*' | grep 41106041
So, basically i want to filter all passwords which numbers appear 3 times
grep 41106041
Is just to test if the code works.. and it doesn't.
I've also tried some "C style" perl code as I'm still a newbie to perl:
#! /bin/perl
#lines=<STDIN>;
$c=0;
foreach $number(#lines)
{
$acum=undef;
$pos=0;
while($pos <= &countdig($number))
{
if ($acum=$digit)
{
$c=$c+1;
}
else
{
$c=0;
}
$acum=$digit;
}
if ($c=3)
{
print "$number"." ";
}
$c=0;
}
sub countdig
{
my($j)=0;
chomp(my(#n)=#_);
print "first dig $n[$j] \n";
while($_[0][$j]>=0 && $_[0][j]<=9)
{
$j+=1;
}
print "j total : $j \n";
$j;
}
Countdig is supposed to count the number of digits but thing is.. I can't access a scalar variable as a list.. so well if you guys could explain me how to make it work it would be very appreciated.
Your grep is failing because there is a stray space in your pattern, and you are searching for 4 identical digits (the one captured, plus three more by backreference).
$ printf '12345678\n41106041\n87654321\n' | grep -v '\(.\).*\1.*\1'
12345678
87654321
The problem is not being a newbie to Perl, cause it's easy to turn a C solution
// For each line
char *num = ...;
int digits[10];
for (int i=0; i<10; ++i) {
digits[i] = 0;
}
const char *p = num;
int triple = 0;
for (; *p && *p != '\n'; ++p) {
if (++digits[*p - '0'] == 3) {
triple = 1;
break;
}
}
if (triple) {
...
}
into a Perl solution
while (my $num = <>) {
chomp($num);
my #digits;
my $triple;
for my $digit (split //, $num) {
if (++$digits[$digit] == 3) {
$triple = 1;
last;
}
}
say $num if $triple;
}
Of course, a Perl programmer might very well use the same approach you used for grep.
while (<>) {
if (!/(.).*\1.*\1/) {
print;
}
}
echo 41106041 | grep -v '\([0-9]\).*\1.*\1.*

Pagination link not work properly?

Pagination not work properly?
I saved this code in "Forum-Com.php" file and when i open this file, pagination works properly and correctly but when I include this file in another page, its open first page correctly but when I click second or another page link it open same comments which are on first page. Please help me. ( I am using Scriptsmill comments script v1.06 and function make_pages_string from admin.php )
$COM_CONF['full_path'] = dirname(__FILE__);
function make_pages_string ($all_count, $records_per_page, $cur_page, $base_url) {
if ($all_count > $records_per_page) {
if ($cur_page > 0) { $cur_page=$cur_page-1; }
$first_record = ($cur_page) * $records_per_page;
$limit_string = "LIMIT $first_record, $records_per_page";
$pages=$all_count/$records_per_page;
if ($pages > (int) $pages) { $pages=(int)$pages+1; }
}
if ($pages>1) {
$pages_string.="Page: ";
if ($cur_page>10 && $pages>20) { $first_page=$cur_page-9; }
else { $first_page=1; }
if ($pages>20 && ($cur_page+10)<$pages) { $last_page=$first_page+19; }
else { $last_page=$pages; }
if ($cur_page+1>1) {
$prev=$cur_page;
$pages_string.="<a href='$base_url&page=$prev'>&lt</a> ";
}
for ($i=$first_page; $i<=$last_page; $i++){
if ($i != $cur_page+1) {
$pages_string.="<a href='$base_url&page=$i'>$i</a> ";
}
else {
$pages_string.="<b>$i</b> ";
}
}
if ($cur_page+1<$pages) {
$next=$cur_page+2;
$pages_string.="<a href='$base_url&page=$next'>&gt</a> ";
}
}
return array ($pages_string, $limit_string);
}
function smcom_view()
{
global $comments_db_link, $COM_CONF, $COM_LANG;
$result = mysql_query("select COUNT(id) from {$COM_CONF['dbmaintable']}", $comments_db_link);
list ($all_count) = mysql_fetch_row($result);
list ($pages_string, $limit_string) = make_pages_string ($all_count, 10, $_REQUEST['page'], "{$COM_CONF['base_url']}?action=view");
$result = mysql_query("select time, text, author, email, dont_show_email from {$COM_CONF['dbmaintable']} order by time {$COM_CONF['sort_order']} $limit_string", $comments_db_link);
$comments_count=0;
$id=$time=$text=$author=$email=$dont_show_email=$ip=array();
while (list($id[$comments_count], $time[$comments_count], $text[$comments_count], $author[$comments_count], $email[$comments_count], $dont_show_email[$comments_count], $ip[$comments_count])=mysql_fetch_array($result)) {
$comments_count++;
}
require("{$COM_CONF['full_path']}/templates/Forum-default.php");
}
The code given above has no problem at all. The problem is with the server configuration which turned off $_REQUEST global variable from direct access.

Zend extension, get arguments of echo?

We have made a Zend extension which we want to write the addresses of the zval's echo is supposed to write out, but we cannot figure how to receive them because we have noticed that there is difference between echo "test"; and $a = "test"; echo $a;
.... Some stuff that overrides the echo opcode ....
FILE *tmpfile;
int echo_handler(ZEND_OPCODE_HANDLER_ARGS)
{
zend_op *opline = execute_data->opline;
tmpfile = fopen("/tmp/echo.test","a+");
fprintf(tmpfile,"Echo was called\n");
fclose(tmpfile);
return ZEND_USER_OPCODE_DISPATCH;
}
How do we get the arguments no matter if it is a variable or not?
The handler for echo is
static int ZEND_FASTCALL ZEND_ECHO_SPEC_CONST_HANDLER(ZEND_OPCODE_HANDLER_ARGS)
{
zend_op *opline = EX(opline);
zval z_copy;
zval *z = &opline->op1.u.constant;
if (IS_CONST != IS_CONST &&
Z_TYPE_P(z) == IS_OBJECT && Z_OBJ_HT_P(z)->get_method != NULL &&
zend_std_cast_object_tostring(z, &z_copy, IS_STRING TSRMLS_CC) == SUCCESS) {
zend_print_variable(&z_copy);
zval_dtor(&z_copy);
} else {
zend_print_variable(z);
}
ZEND_VM_NEXT_OPCODE();
}
from Zend/zend_vm_execute.h, and as you can see all it basically does is to call zend_print_variable().
Hook that function and you should be on the right track.
Bonus: it works for print statements too.

Parsing switch statements with Happy

So, I'm trying to parse code containg switch statements like this
function (a : Boolean) equals (b : Boolean) : Boolean {
switch (a) {
case true:
switch (b) {
case true:
return (true);
case false:
return (false);
}
case false:
switch (b) {
case true:
return (false);
case false:
return (true);
}
}
};
with
switch
: "switch" expression "{" cases "}" {
Switch $2 $4
}
;
cases
: case cases {
($1 : $2)
}
| case {
[$1]
}
;
case
: "case" pattern ":" caseStatements {
Case $2 $4
}
;
caseStatements
: caseStatement ";" caseStatements {
($1 : $3)
}
| caseStatement {
[$1]
}
;
caseStatement
: assignment {
AssignmentCaseStatement $1
}
| return {
ReturnCaseStatement $1
}
| switch {
SwitchCaseStatement $1
}
;
but I keep getting:
certa: user error (../examples/Certa/BooleanLogic.certa:16: Parse error at token 'case')
when I run the generated parser. The strange thing here is that it fails on the second instance of the "case" keyword, but not the first. Why in the world would that be?
Shouldn't your non-recursive leg of caseStatements include a semi-colon?
i.e.
caseStatements
: caseStatement ";" caseStatements {
($1 : $3)
}
| caseStatement ";" {
[$1]
}
;

Resources