Sponsored Content
Top Forums Shell Programming and Scripting Perl extract number from file & write to file Post 302578826 by durden_tyler on Friday 2nd of December 2011 11:45:24 AM
Old 12-02-2011
Quote:
Originally Posted by austinj
...I don't understand this line:

if (/^CVR\(10\)\s*=\s*(.*?),.*?SAUCE$/)

I understand that it is looking for CVR(10) and SAUCE,
...
Here's a breakup of the entire regular expression -

Code:
/^CVR\(10\)\s*=\s*(.*?),.*?SAUCE$/ =>
 
See if the current line matches the regular expression that:
 
^        : has at the beginning
CVF      : the characters "C", "V", "R", followed by
\(       : an open-parenthesis character (the escape character is necessary because we want Perl to consider its literal value)
10       : followed by the characters "1", "0", followed by
\)       : a close-parenthesis character (escaping is necessary because we want Perl to consider its literal value)
\s*      : followed by 0 or more occurrences of whitespace characters
=        : followed by the "=" character
\s*      : followed by 0 or more occurrences of whitespace characters
(.*?),   : followed by the shortest continuous sequence of characters that ends with
        the comma character (","); group and capture this shortest continuous
        sequence of characters in a variable called "$1". This "grouping and
        capturing" is achieved by the parentheses; note that they are not
        escaped here, because we want Perl to consider their special (and not literal) values here
.*?S     : followed by shortest continuous sequence of characters that is followed by the character "S"
AUCE     : followed by the characters "A", "U", "C", "E"
$        : thereby reaching the end of the line

If the line does not match the regular expression, the "if" condition is false and we go to the next line.
If the line does match the regular expression, then we copy the value in "$1" to "$extracted_value" and stop processing the file (that is achieved by the "last" statement).
Since the CVR, SAUCE exists in the file only once, we don't spend time unnecessarily going through the remainder of the file once we find the extracted value. Imagine what would happen if the file had 100 million lines and the CVR, SAUCE was the 1st or 2nd line.

Quote:
...
is there some documentation to explain all the '' \?*,./ '' business? looks like gibberish to me.

Anyway works great that's what I needed, the "EOF" is quite useful...
The "\?*,./ business" is ".*?" really, and it means "non-greedy/lazy quantifier". It's a part of the powerful tool called "regular expressions" and you'll inevitably encounter it while studying regular expressions. The Web and the world of books abound in information about regular expressions, and few that come to mind are as follows.

Code:
(1)  The most well-known book on regexes is this one:
Mastering Regular Expressions, 3rd Ed by Jeffrey Friedl - 
Chapter 3: Overview of Regular Expressions Features and Flavors, Section "Lazy Quantifiers"
Chapter 4: The Mechanics of Expression Processing - entire chapter provides detailed information of backtracking, greediness and laziness
 
(2)  Jan Goyvaerts, the author of "Regular Expressions Cookbook", has a website on regular expressions, and he explains greediness/laziness over there:
http://www.regular-expressions.info/repeat.html
 
(3)  The online Perl documentation has a really good tutorial on regexes. The section "Matching Repetitions" explains this concept:
http://perldoc.perl.org/perlretut.html#Matching-repetitions

tyler_durden

Last edited by durden_tyler; 12-02-2011 at 02:55 PM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

perl -write values in a file to @array in perl

Hi can anyone suggest me how to write a file containing values,... say 19 20 21 22 .. 40 to an array @array = (19, 20, ... 40) -- Thanks (27 Replies)
Discussion started by: meghana
27 Replies

2. Shell Programming and Scripting

Extract string from a file & write to a new file (Perl)

Hi, This is the first time playing around with perl and need some help. Assuming if i have a line of text that looks like this: Date/Time=Nov 18 17:12:11;Device Name=192.168.1.1;Device IP=192.168.1.1;Device Class=IDS;Source IP=155.212.212.111;Source Name=UNKNOWN;Source Port=1679... (3 Replies)
Discussion started by: LuckyGuy
3 Replies

3. Shell Programming and Scripting

Extract data from an XML file & write into a CSV file

Hi All, I am having an XML tag like: <detail sim_ser_no_1="898407109001000090" imsi_1="452070001000090"> <security>ADM1=????</security> <security>PIN1=????</security> <security>PIN2=????</security> ... (2 Replies)
Discussion started by: ss_ss
2 Replies

4. Shell Programming and Scripting

How to write a script to extract strings from a file.

Hello fourm members, I want to write a script to extarct paticular strings from the all type of files(.sh files,logfiles,txtfiles) and redirect into a log file. example: I have to find the line below in the script and extract the uname and Pwds. sqsh -scia2007 -DD0011uw01 -uciadev... (5 Replies)
Discussion started by: rajkumar_g
5 Replies

5. Shell Programming and Scripting

Extract a number from a line in a file and sed in another copied file

Dear all, I am trying to extract a number from a line in one file (task 1), duplicate another file (task 2) and replace all instances of the strings 300, in duplicated with the extracted number (task 3). Here is what I have tried so far: for ((k=1;k<4;k++)); do temp=`sed -n "${k}p"... (2 Replies)
Discussion started by: mnaqvi
2 Replies

6. Shell Programming and Scripting

write specific line number in file

dear all, i need your advice i have sample script like this: testing.sh for i in {1..10} do echo testing $i done but i forgot create "#!/bin/bash" in above "for" so i want output will like this testing.sh #!/bin/bash for i in {1..10} do echo testing $i done (2 Replies)
Discussion started by: zvtral
2 Replies

7. Shell Programming and Scripting

how to write bash script that will automatically extract zip file

i'm trying to write a bash script that that will automatically extract zip files after the download. i writed this script #!/bin/bash wget -c https://github.com/RonGokhle/kernel-downloader/zipball/master CURRENDIR=/home/kernel-downloader cd $CURRENDIR rm $CURRENDIR/zipfiles 2>/dev/null ... (2 Replies)
Discussion started by: ron gokhle
2 Replies

8. Shell Programming and Scripting

Extract data from XML file and write in CSV file

Hi friend i have input as following XML file <?xml version="1.0"?> <Document xmlns="urn:iso:std:iso:20022:tech:xsd:camt.054.001.02"> <BkToCstmrDbtCdtNtfctn> <GrpHdr><MsgId>LBP-RDJ-TE000000-130042430010001001</MsgId><CreDtTm>2013-01-04T03:21:30</CreDtTm></GrpHdr>... (3 Replies)
Discussion started by: mohan sharma
3 Replies

9. Shell Programming and Scripting

Extract text from file then write variable

I have a text file that has many lines, and for each line I need to extract different sections of text, then write several variables from the data. I can split out the various sections by selecting their position on each line as the column sizes will never vary. A sample of my text file is below... (2 Replies)
Discussion started by: kcpoole
2 Replies

10. Shell Programming and Scripting

Need to search a particular String form a file a write to another file using perl script

I have file which contains a huge amount of data. I need to search the pattern Message id. When that pattern is matched I need to get abcdeff0-1g6g-91g3-1z2z-2mm605m90000 to another file. Kindly provide your input. File is like below Jan 11 04:05:10 linux100 |NOTICE... (2 Replies)
Discussion started by: Raysf
2 Replies
All times are GMT -4. The time now is 09:19 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy