Sponsored Content
Top Forums Shell Programming and Scripting Automating find and replace REGEX expressions in a script Post 303045492 by bedtime on Friday 27th of March 2020 04:39:27 PM
Old 03-27-2020
Quote:
Originally Posted by balajesuri
You may try perl. When it comes to regex and string parsing, perl handles them beautifully. Of course, doesn't mean you don't have other options.
Thank you. I've done just that. Perl seems to be the perfect tool for this issue.


Sorry for the late reply.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

automating file search and replace text

Hi, I am trying something like this: Let's say I have a file called File1 with contents: x=-0.3 y=2.1 z=9.0 I have another file, File2, with contents: xx= yy= zz= (nothing after "="). What I want to do is get the value of x in File1 and set it to xx in File2, i.e., xx=-0.3. And the... (3 Replies)
Discussion started by: ommatidia
3 Replies

2. Shell Programming and Scripting

A simple find and replace without using any regex (bash)

Hi, I need to do an exact find and replace (I don't want to use regular expressions because the input comes from user). I want to find a line that matches the user's input text and replace it with an empty string. For example, let's say the user enters I love "Unix" and the contents of the... (2 Replies)
Discussion started by: srikanths
2 Replies

3. Shell Programming and Scripting

Search Replace with regular expressions

Hello I have this regular expression: </book>(?:\n)<collection>(.*)</collectioninfo> And I have this peace of text on a FILE <book bookid="3" title="the title 3" remaining = "50" price="100"> <reader readerid="1"><!]></reader> <reader readerid="2"><!]></reader> <reader... (1 Reply)
Discussion started by: dirdamalah
1 Replies

4. UNIX for Dummies Questions & Answers

Regular Expressions -- Find spaces outside

Hello, I need help with using grep and regular expressions.... I have a long list of about 1000 lines of Chinese flashcards. Here's a small excerpt: 意文 yìwén (given name) 貴姓 guìxìng (honorable surname) 貴 guì (honorable) 姓 xìng (one's surname is; to be surnamed; surname) 呢 ne (interrogative... (2 Replies)
Discussion started by: arduino411
2 Replies

5. Linux

A find and replace script

Hello everyone, I am trying to do a simple script (shell or perl) for a bioinformatic problem, where I want to use a list from file2 and append this information to file1 (output). Example: File1 >Apple1 GAGACGATTTATATATAGAAGAGAG >Banana2 CAGAGAGAGAGACCCCCCCCCCCC File2 >Apple1 ... (2 Replies)
Discussion started by: herasj01
2 Replies

6. Shell Programming and Scripting

FIND w/ multiple expressions

For reference i am still a newb at scripting, but i have what i think is a complex issue. I need to run a FIND on / to identify all files and print them to >> $TEMPFILE I need to avoid MVFS I need to avoid /var/tmp/nastmp/ I was trying find / \( -type d -name nastmp -prune \) -a \( -fstype... (4 Replies)
Discussion started by: nitrobass24
4 Replies

7. Shell Programming and Scripting

Find 2 expressions then print in a single line

Guys, need help. I have a file that contains something like this: abc def ghi jkl I want to print the first and last line of the file and the output should be in a single line. so, output should be like this: abc jkl (3 Replies)
Discussion started by: solidhelix08
3 Replies

8. UNIX for Advanced & Expert Users

Using find and regular expressions

Hi Could you please advise how can one extract from the output of find . -name "*.c" -print only filenames in the current direcotry and not in its subdirectories? I tried using (on Linux x86_64) find . -name "*.c" -prune but it is not giving correct output. Whereas I am getting... (9 Replies)
Discussion started by: tinku981
9 Replies

9. Shell Programming and Scripting

Find and Replace in Shell script

Friends, I have more than 1000 lines in text file which needs to be converted as UPPERCASE by adding _ com.sun.url=www.sun.com com.ssl.port=808 com.ui.path=/apps/ssi Expected output com.sun.url=_COM.SUN.URL_ com.ssl.port=_COM.SSL.PORT_ com.ui.path=_COM.UI.PATH_ Thanks in... (4 Replies)
Discussion started by: baluchen
4 Replies

10. Shell Programming and Scripting

sed find multiple expressions plus next next line

Hello I am trying to write sed code where it will look through the text for lines with specific expression "Started by user" and when found, will also add the following line, and also lines with another expression "Finished:" sed -n '/Started by user/, +1p, /Finished:/'... (4 Replies)
Discussion started by: dlesny
4 Replies
DateTime::Format::Builder::Tutorial(3pm)		User Contributed Perl Documentation		  DateTime::Format::Builder::Tutorial(3pm)

NAME
DateTime::Format::Builder::Tutorial - Quick class on using Builder CREATING A CLASS
As most people who are writing modules know, you start a package with a package declaration and some indication of module version: package DateTime::Format::ICal; our $VERSION = '0.04'; After that, you call Builder with some options. There are only a few (detailed later). Right now, we're only interested in parsers. use DateTime::Format::Builder ( parsers => { ... } ); The parsers option takes a reference to a hash of method names and specifications: parsers => { parse_datetime => ... , parse_datetime_with_timezone => ... , ... } Builder will create methods in your class, each method being a parser that follows the given specifications. It is strongly recommended that one method is called parse_datetime, be it a Builder created method or one of your own. In addition to creating any of the parser methods it also creates a "new()" method that can instantiate (or clone) objects of this class. This behaviour can be modified with the constructor option, but we don't need to know that yet. Each value corresponding to a method name in the parsers list is either a single specification, or a list of specifications. We'll start with the simple case. parse_briefdate => { params => [ qw( year month day ) ], regex => qr/^(dddd)(dd)(dd)$/, }, This will result in a method named parse_briefdate which will take strings in the form 20040716 and return DateTime objects representing that date. A user of the class might write: use DateTime::Format::ICal; my $date = "19790716"; my $dt = DateTime::Format::ICal->parse_briefdate( $date ); print "My birth month is ", $dt->month_name, " "; The "regex" is applied to the input string, and if it matches, then $1, $2, ... are mapped to the params given and handed to "DateTime->new()". Essentially: my $rv = DateTime->new( year => $1, month => $2, day => $3 ); There are more complicated things one can do within a single specification, but we'll cover those later. Often, you'll want a method to be able to take one string, and run it against multiple parser specifications. It would be very irritating if the user had to work out what format the datetime string was in and then which method was most appropriate. So, Builder lets you specify multiple specifications: parse_datetime => [ { params => [ qw( year month day hour minute second ) ], regex => qr/^(dddd)(dd)(dd)T(dd)(dd)(dd)$/, }, { params => [ qw( year month day hour minute ) ], regex => qr/^(dddd)(dd)(dd)T(dd)(dd)$/, }, { params => [ qw( year month day hour ) ], regex => qr/^(dddd)(dd)(dd)T(dd)$/, }, { params => [ qw( year month day ) ], regex => qr/^(dddd)(dd)(dd)$/, }, ], It's an arrayref of specifications. A parser will be created that will try each of these specifications sequentially, in the order you specified. There's a flaw with this though. In this example, we're building a parser for ICal datetimes. One can place a timezone id at the start of an ICal datetime. You might extract such an id with the following code: if ( $date =~ s/^TZID=([^:]+):// ) { $time_zone = $1; } # Z at end means UTC elsif ( $date =~ s/Z$// ) { $time_zone = 'UTC'; } else { $time_zone = 'floating'; } $date would end up without the id, and $time_zone would contain something appropriate to give to DateTime's set_time_zone method, or time_zone argument. But how to get this scrap of code into your parser? You might be tempted to call the parser something else and build a small wrapper. There's no need though because an option is provided for preprocesing dates: parse_datetime => [ [ preprocess => &_parse_tz ], # Only changed line! { params => [ qw( year month day hour minute second ) ], regex => qr/^(dddd)(dd)(dd)T(dd)(dd)(dd)$/, }, { params => [ qw( year month day hour minute ) ], regex => qr/^(dddd)(dd)(dd)T(dd)(dd)$/, }, { params => [ qw( year month day hour ) ], regex => qr/^(dddd)(dd)(dd)T(dd)$/, }, { params => [ qw( year month day ) ], regex => qr/^(dddd)(dd)(dd)$/, }, ], It will necessitate _parse_tz to be written, and that routine looks like this: sub _parse_tz { my %args = @_; my ($date, $p) = @args{qw( input parsed )}; if ( $date =~ s/^TZID=([^:]+):// ) { $p->{time_zone} = $1; } # Z at end means UTC elsif ( $date =~ s/Z$// ) { $p->{time_zone} = 'UTC'; } else { $p->{time_zone} = 'floating'; } return $date; } On input it is given a hash containing two items: the input date and a hashref that will be used in the parsing. The return value from the routine is what the parser specifications will run against, and anything in the parsed hash ($p in the example) will be put in the call to "DateTime->new(...)". So, we now have a happily working ICal parser. It parses the assorted formats, and can also handle timezones. Is there anything else it needs to do? No. But we can make it work more efficiently. At present, the specifications are tested sequentially. However, each one applies to strings of particular lengths. Thus we could be efficient and have the parser only test the given strings against a parser that handles that string length. Again, Builder makes it easy: parse_datetime => [ [ preprocess => &_parse_tz ], { length => 15, # We handle strings of exactly 15 chars params => [ qw( year month day hour minute second ) ], regex => qr/^(dddd)(dd)(dd)T(dd)(dd)(dd)$/, }, { length => 13, # exactly 13 chars... params => [ qw( year month day hour minute ) ], regex => qr/^(dddd)(dd)(dd)T(dd)(dd)$/, }, { length => 11, # 11.. params => [ qw( year month day hour ) ], regex => qr/^(dddd)(dd)(dd)T(dd)$/, }, { length => 8, # yes. params => [ qw( year month day ) ], regex => qr/^(dddd)(dd)(dd)$/, }, ], Now the created parser will create a parser that only runs specifications against appropriate strings. So our complete code looks like: package DateTime::Format::ICal; use strict; our $VERSION = '0.04'; use DateTime::Format::Builder ( parsers => { parse_datetime => [ [ preprocess => &_parse_tz ], { length => 15, params => [ qw( year month day hour minute second ) ], regex => qr/^(dddd)(dd)(dd)T(dd)(dd)(dd)$/, }, { length => 13, params => [ qw( year month day hour minute ) ], regex => qr/^(dddd)(dd)(dd)T(dd)(dd)$/, }, { length => 11, params => [ qw( year month day hour ) ], regex => qr/^(dddd)(dd)(dd)T(dd)$/, }, { length => 8, params => [ qw( year month day ) ], regex => qr/^(dddd)(dd)(dd)$/, }, ], }, ); sub _parse_tz { my %args = @_; my ($date, $p) = @args{qw( input parsed )}; if ( $date =~ s/^TZID=([^:]+):// ) { $p->{time_zone} = $1; } # Z at end means UTC elsif ( $date =~ s/Z$// ) { $p->{time_zone} = 'UTC'; } else { $p->{time_zone} = 'floating'; } return $date; } 1; And that's an ICal parser. The actual DateTime::Format::ICal module also includes formatting methods and parsing for durations, but Builder doesn't support those yet. A drop in replacement (at the time of writing the replacement) can be found in the examples directory of the Builder distribution, along with similar variants of other common modules. SUPPORT
Any errors you see in this document, please log them with CPAN RT system via the web or email: http://perl.dellah.org/rt/dtbuilder bug-datetime-format-builder@rt.cpan.org This makes it much easier for me to track things and thus means your problem is less likely to be neglected. LICENSE AND COPYRIGHT
Copyright X Iain Truskett, 2003. All rights reserved. You can redistribute this document and/or modify it under the same terms as Perl itself. The full text of the licenses can be found in the Artistic and COPYING files included with this document. AUTHOR
Iain Truskett <spoon@cpan.org> SEE ALSO
"datetime@perl.org" mailing list. http://datetime.perl.org/ perl, DateTime, DateTime::Format::Builder perl v5.10.1 2010-03-14 DateTime::Format::Builder::Tutorial(3pm)
All times are GMT -4. The time now is 05:09 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy