Sponsored Content
Full Discussion: Regex learning.
Top Forums Shell Programming and Scripting Regex learning. Post 303044737 by RavinderSingh13 on Tuesday 3rd of March 2020 12:09:53 AM
Old 03-03-2020
Hello All,

Learnt an example of Lazy match in Regex in Perl, so thought to share here.
Let's say following is Input_file.

Code:
cat Input_file
abcdtest123^ DUMMYtestabcd12234 DUMMY bla blabla12231311313blabla bla.....,,,,,bla
test132131 ^ DUMMY blabla1213 121313_ 131y7351eg1eub wdfwfknfidh28e7ty;;;

Now we would like to have data between first occurrence of ^ to DUMMY , then we could use Lazy match like as follows:

Code:
perl -pe 's|(\^.*?DUMMY\s+)(.*)| new_text_here.... \2|'  Input_file

Output will be as follows for mentioned sample:
Code:
abcdtest123 new_text_here.... bla blabla12231311313blabla bla.....,,,,,bla
test132131  new_text_here.... blabla1213 121313_ 131y7351eg1eub wdfwfknfidh28e7ty;;;

Why is Lazy match Good here? Because .* is a GREEDY match and matches anything till last occurrence of any mentioned character etc but using Lazy match .*?DUMMY\s+ it matches very first occurrence of string DUMMY followed with space starting from ^

Tested and written this in PERL, thought/views/improvements are most welcome here.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
 

8 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

learning on my own

can i do this? i am learning this on my own..and from the book..simple unix i am not sure if the syntax would work if statement then statement do or for or while statement done else statement fi.... I dont know how else to explain that...I hope I... (2 Replies)
Discussion started by: jonas27
2 Replies

2. Shell Programming and Scripting

Converting perl regex to sed regex

I am having trouble parsing rpm filenames in a shell script.. I found a snippet of perl code that will perform the task but I really don't have time to rewrite the entire script in perl. I cannot for the life of me convert this code into something sed-friendly: if ($rpm =~ /(*)-(*)-(*)\.(.*)/)... (1 Reply)
Discussion started by: suntzu
1 Replies

3. Shell Programming and Scripting

Learning Perl

Folks! Anyone please explain the behavior of this program step by step. Thanks. #! /usr/bin/perl $testfile = "./testfile2"; for ( $i = 1, $i <= 5, $i++) { open ($FILE, ">", $testfile); print ($FILE "Output 1 \n"); close ($FILE); } print "The value of (4 * 2) / 2 is "; print (4 * 2)... (1 Reply)
Discussion started by: huko99
1 Replies

4. UNIX for Dummies Questions & Answers

Need advice on learning RegEx

Hello Unix graduates, I have gone through 50 posts here in this Unix.com, out of which 35-40 are answered with regex.. Regex is always been a problem for me. :wall: I have gone through few URLs still it doesn't help me to reach what you guys are.. :confused: How to get skilled on... (1 Reply)
Discussion started by: sathyaonnuix
1 Replies

5. UNIX for Dummies Questions & Answers

read regex from ID file, print regex and line below from source file

I have a file of protein sequences with headers (my source file). Based on a list of IDs (which are included in some of the headers), I'd like to print out only the specified sequences, with only the ID as header. In other words, I'd like to search source.txt for the terms in IDs.txt, and print... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

6. Programming

Learning C with CBT?

Hello All, I am a beginner in C, although I believe my learning curve is rather steep. I would like to know if any of you would have some recommendations with some learning materials to become a good C developer. I am particularly thinking about some CBT courses or some good Quizz that would... (2 Replies)
Discussion started by: freddie50
2 Replies

7. Shell Programming and Scripting

Perl, RegEx - Help me to understand the regex!

I am not a big expert in regex and have just little understanding of that language. Could you help me to understand the regular Perl expression: ^(?!if\b|else\b|while\b|)(?:+?\s+){1,6}(+\s*)\(*\) *?(?:^*;?+){0,10}\{ ------ This is regex to select functions from a C/C++ source and defined in... (2 Replies)
Discussion started by: alex_5161
2 Replies

8. Shell Programming and Scripting

Sendmail K command regex: adding exclusion/negative lookahead to regex -a@MATCH

I'm trying to get some exclusions into our sendmail regular expression for the K command. The following configuration & regex works: LOCAL_CONFIG # Kcheckaddress regex -a@MATCH +<@+?\.++?\.(us|info|to|br|bid|cn|ru) LOCAL_RULESETS SLocal_check_mail # check address against various regex... (0 Replies)
Discussion started by: RobbieTheK
0 Replies
Config::Model::DumpAsData(3pm)				User Contributed Perl Documentation			    Config::Model::DumpAsData(3pm)

NAME
Config::Model::DumpAsData - Dump configuration content as a perl data structure VERSION
version 2.021 SYNOPSIS
use Config::Model ; use Log::Log4perl qw(:easy) ; use Data::Dumper ; Log::Log4perl->easy_init($WARN); # define configuration tree object my $model = Config::Model->new ; $model ->create_config_class ( name => "MyClass", element => [ [qw/foo bar/] => { type => 'leaf', value_type => 'string' }, baz => { type => 'hash', index_type => 'string' , cargo => { type => 'leaf', value_type => 'string', }, }, ], ) ; my $inst = $model->instance(root_class_name => 'MyClass' ); my $root = $inst->config_root ; # put some data in config tree the hard way $root->fetch_element('foo')->store('yada') ; $root->fetch_element('bar')->store('bla bla') ; $root->fetch_element('baz')->fetch_with_id('en')->store('hello') ; # put more data the easy way my $step = 'baz:fr=bonjour baz:hr="dobar dan"'; $root->load( step => $step ) ; print Dumper($root->dump_as_data); # $VAR1 = { # 'bar' => 'bla bla', # 'baz' => { # 'en' => 'hello', # 'fr' => 'bonjour', # 'hr' => 'dobar dan' # }, # 'foo' => 'yada' # }; DESCRIPTION
This module is used directly by Config::Model::Node to dump the content of a configuration tree in perl data structure. The perl data structure is a hash of hash. Only CheckList content will be stored in an array ref. Note that undefined values are skipped for list element. I.e. if a list element contains "('a',undef,'b')", the data structure will contain 'a','b'. CONSTRUCTOR
new ( ) No parameter. The constructor should be used only by Config::Model::Node. Methods dump_as_data(...) Return a perl data structure Parameters are: node Reference to a Config::Model::Node object. Mandatory full_dump Also dump default values in the data structure. Useful if the dumped configuration data will be used by the application. (default is yes) skip_auto_write Skip node that have a "perl write" capability in their model. See Config::Model::AutoRead. auto_vivify Scan and create data for nodes elements even if no actual data was stored in them. This may be useful to trap missing mandatory values. ordered_hash_as_list By default, ordered hash (i.e. the order of the keys are important) are dumped as Perl list. This is the faster way to dump such hashed while keeping the key order. But it's the less readable way. When this parameter is 1 (default), the ordered hash is dumped as a list: [ A => 'foo', B => 'bar', C => 'baz' ] When this parameter is set as 0, the ordered hash is dumped with a special key that specifies the order of keys. E.g.: { __order => [ 'A', 'B', 'C' ] , B => 'bar', A => 'foo', C => 'baz' } Methods dump_annotations_as_pod(...) Return a string formatted in pod (See perlpod) with the annotations. Parameters are: node Reference to a Config::Model::Node object. Mandatory experience master, advanced or beginner check_list Yes, no or skip AUTHOR
Dominique Dumont, (ddumont at cpan dot org) SEE ALSO
Config::Model,Config::Model::Node,Config::Model::ObjTreeScanner perl v5.14.2 2012-11-09 Config::Model::DumpAsData(3pm)
All times are GMT -4. The time now is 06:42 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy