Sponsored Content
Top Forums Shell Programming and Scripting Perl script: matching multiple lines error Post 302844327 by durden_tyler on Sunday 18th of August 2013 01:55:39 PM
Old 08-18-2013
I'll answer your second question first.

Quote:
Originally Posted by askari
if my original data is changed and added the additional parameter below. I have used your code above before but when I want to split it (e.g. halo1 data into two column by using regexp) is it possible in the while looping directly after the pattern is match?.

Code:
Pattern Gabriel
halo1 data
halo2 data
end
Pattern Andreas
halo1 data
halo2 data
end

Of course it is possible. Almost every kind of text processing is possible in Perl, if you know how to do it.

As an example:

Code:
$ 
$ 
$ cat -n input
     1    Pattern Gabriel
     2    halo1 data
     3    halo2 data
     4    end
     5    Pattern Andreas
     6    halo1 data
     7    halo2 data
     8    end
$ 
$ 
$ perl -lne 'BEGIN {$/=undef}
             while(/^(Pattern (\S+).*?end)/msg) {
               @x = split("\n",$1);
               print "Begin:",$x[0];
               for $i (1..$#x-1) {
                 print "Line = ", $x[$i];
                 ($col1, $col2) = $x[$i] =~ /^(\S+)\s+(\S+)$/;
                 print "col1 = ", $col1;
                 print "col2 = ", $col2;
                 print "=" x 20;
               }
               print "End:",$x[$#x]
             }
            ' input
Begin:Pattern Gabriel
Line = halo1 data
col1 = halo1
col2 = data
====================
Line = halo2 data
col1 = halo2
col2 = data
====================
End:end
Begin:Pattern Andreas
Line = halo1 data
col1 = halo1
col2 = data
====================
Line = halo2 data
col1 = halo2
col2 = data
====================
End:end
$ 
$ 
$

Now for your first concern about the file being huge - that is a subjective term. What may be a huge file for you may not be huge for Perl. A good idea would be to benchmark your processing and go from there.

Finally, if you want to put chunks of text into separate log files but still want to do line-by-line regex processing, then a simpler approach be to just:

- read your file line-by-line,
- start logging to the file when you encounter the start pattern
- use regexes for the following lines; do whatever you want to do with those
- and stop logging when you encounter the end pattern
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Help] PERL Script - grep multiple lines

Hi Gurus, I need some help with the "grep" command or whatever command that you think suitable for me. I'm about to write a perl script to extract a report from the system and submit it to the end users. The input for the script will consist of 3 element. 1) Generation ID 2) Month 3) Year... (6 Replies)
Discussion started by: miskin
6 Replies

2. Shell Programming and Scripting

pattern matching over multiple lines and deleting the first

I've got a longish log file with content such as Uplink traffic: Downlink traffic: I want to parse the log file and remove any line that contains the string "Uplink traffic:" at the beginning of the line, but only if the line following it beginnings with the string "Downlink traffic:" (in... (7 Replies)
Discussion started by: Yorkie99
7 Replies

3. Shell Programming and Scripting

Matching lines across multiple csv files and merging a particular field

I have about 20 CSV's that all look like this: "","","","","","","","","","","","","","","",""What I've been told I need to produce is the exact same thing, but with each file now containing the start_code from every other file where the email matches. It doesn't matter if any of the other... (1 Reply)
Discussion started by: Demosthenes
1 Replies

4. Shell Programming and Scripting

perl basic multiple pattern matching

Hi everyone, and thank you for your help with this. I am VERY new with perl so all of your help is appreciated. I have tried google but as I don't know the proper terms to search for and could be daunting for a newbie scripter... I know this is very easy for most of you! Thanks! I have a... (4 Replies)
Discussion started by: sinusoid
4 Replies

5. Shell Programming and Scripting

Perl XML, find matching condition and grep lines and put the lines somewhere else

Hi, my xml files looks something like this <Instance Name="New York"> <Description></Description> <Instance Name="A"> <Description></Description> <PropertyValue Key="false" Name="Building A" /> </Instance> <Instance Name="B"> ... (4 Replies)
Discussion started by: tententen
4 Replies

6. Shell Programming and Scripting

Perl Regex matching multiple lines

I need a way to extract data from X 4T Solution 21 OCT 2011 37 .00 to account 12345678 User1 user2 X 4T Solution Solution Unlimited 11 Sep 2009 248 .00 to account 87654321 user3 user4 I need it to extract 'X' '37.00' and account number 12345678. I have extracted above stuff... (3 Replies)
Discussion started by: chakrapani
3 Replies

7. Shell Programming and Scripting

Multiple pattern matching using awk and getting count of lines

Hi , I have a file which has multiple rows of data, i want to match the pattern for two columns and if both conditions satisfied i have to add the counter by 1 and finally print the count value. How to proceed... I tried in this way... awk -F, 'BEGIN {cnt = 0} {if $6 == "VLY278" &&... (6 Replies)
Discussion started by: aemunathan
6 Replies

8. Shell Programming and Scripting

Help need with PERL multiple search pattern matching!

My example file is as given below: conn=1 uid=oracle conn=2 uid=db2 conn=3 uid=oracle conn=4 uid=hash conn=5 uid=skher conn=6 uid=oracle conn=7 uid=mpalkar conn=8 uid=anarke conn=9 uid=oracle conn=1 op=-1 msgId=-1 - fd=104 slot=104 LDAPS connection from 10.10.5.6 to 10.18.6.5 conn=2... (3 Replies)
Discussion started by: sags007_99
3 Replies

9. Shell Programming and Scripting

Perl Matching multiple variables

I am trying to match mulitple (3) variables. I found the sub given below on the web which works well when all vars are defined. But there are situations where one or two will not be defined (at least one will always be defined.) Example of the variable content possibilities The sub found... (4 Replies)
Discussion started by: popeye
4 Replies

10. Shell Programming and Scripting

Group Multiple Lines on SINGLE line matching pattern

Hi Guys, I am trying to format my csv file. When I spool the file using sqlplus the single row output is wrapped on three lines. Somehow I managed to format that file and finally i am trying to make the multiple line on single line. The below command is working fine but I need to pass the... (3 Replies)
Discussion started by: RJSKR28
3 Replies
funsky(1)							SAORD Documentation							 funsky(1)

NAME
funsky - convert between image and sky coordinates SYNOPSIS
funsky iname[ext] # RA,Dec (deg) or image pix from stdin funsky iname[ext] [lname] # RA, Dec (deg) or image pix from list funsky iname[ext] [col1] [col2] # named cols:units from stdin funsky iname[ext] [lname] [col1] [col2] # named cols:units from list OPTIONS
-d # always use integer tlmin conversion (as ds9 does) -r # convert x,y to RA,Dec (default: convert RA,Dec to x,y) -o # include offset from the nominal target position (in arcsec) -v # display input values also (default: display output only) -T # output display in rdb format (w/header,tab delimiters) DESCRIPTION
Funsky converts input sky coordinates (RA, Dec) to image coordinates (or vice versa) using the WCS information contained in the specified FITS file. Several calling sequences are supported in order to make it easy to specify coordinate positions in different ways. The first required argument is always the input FITS file (or extension) containing the WCS information in an extension header. Note that the data from this file is not used. By default, the program converts input RA and Dec values to X and Y using this WCS information. If the WCS is associated with a FITS image, then the X,Y values are image values. If the WCS is associated with a binary table, then the X, Y val- ues are physical values. To convert X,Y to RA and Dec, use the -r (reverse) switch. If no other command arguments are supplied, then the input positions are read from the standard input. Each line is assumed to contain a single coordinate position consisting of an RA in hours (or X in pixels) followed by a Dec in degrees (or Y in pixels). The usual delim- iters are supported (spaces, commas, tabs). For example: # read from stdin, default column names and units [sh] funsky snr.ev 22.982695 58.606523 # input RA (hrs), Dec(deg) 510.00 510.00 22.982127 58.607634 # input 512.00 510.50 22.981700 58.614301 # input 513.50 513.50 ^D # end of input If a second argument is supplied, this argument is assumed to be a file containing RA (X) and Dec (Y) positions. The file can either be an ASCII table or a FITS binary table. The order of columns is unimportant, if the table has a column header. In this case, the names of the columns must be one of "RA", "DEC", or "X", "Y" for sky to image and image to sky conversions, respectively. If the table has no header, then once again, RA (X) is assumed to first, followed by DEC (Y). For example: # read from file, default column names and units [sh] cat hd.in RA DEC --------- --------- 22.982695 58.606523 22.982127 58.607634 22.981700 58.614301 [sh] funsky snr.ev hd.in 510.00 510.00 512.00 510.50 513.50 513.50 If three arguments are supplied, then the input positions again are read from the standard input. Each line is assumed to contain a single coordinate position consisting of an RA (or X in pixels) followed by a Dec (or Y in pixels), with the usual delimiters supported. However, the second and third arguments now specify the column names and/or sky units using a colon-delimited syntax: [colname]:[h|d|r] If the colname is omitted, the names default to "RA", "DEC", "X", "Y", "COL1", or "COL2" as above. If the units are omitted, the default is hours for RA and degrees for Dec. When the -r switch is used (convert from image to sky) the units are applied to the output instead of the input. The following examples will serve to illustrate the options: # read from stdin, specifying column names (def. units: hours, degrees) [sh] cat hd.in MYRA MYDEC --------- --------- 22.982695 58.606523 22.982127 58.607634 22.981700 58.614301 [sh] funsky snr.ev MYRA MYDEC < hd.in 510.00 510.00 512.00 510.50 513.50 513.50 # read from stdin, specifying column names and units [sh] cat dd.in MYRA MYDEC --------- --------- 344.740432 58.606523 344.731900 58.607634 344.725500 58.614301 [sh] funsky snr.ev MYRA:d MYDEC:d < dd.in 510.00 510.00 512.00 510.50 513.50 513.50 # read stdin, convert image to sky, specifying output sky units [sh] cat im.in 510.00 510.00 512.00 510.50 513.50 513.50 [sh] cat im.in | funsky -r snr.ev :d :d 344.740432 58.606523 344.731900 58.607634 344.725500 58.614301 Finally, four command arguments specify both and input file and column names and/or units: [sh] cat dd.in MYRA MYDEC --------- --------- 344.740432 58.606523 344.731900 58.607634 344.725500 58.614301 [sh] funsky snr.ev dd.in MYRA:d MYDEC:d 510.00 510.00 512.00 510.50 513.50 513.50 # read file, convert image to sky, specifying output sky units [sh] cat im.in 510.00 510.00 512.00 510.50 513.50 513.50 [sh] funsky -r snr.ev im.in :d :d 344.740432 58.606523 344.731900 58.607634 344.725500 58.614301 By default, the output of funsky consists only of the converted coordinate position(s), one per output line. This makes parsing in shell scripts easy. Use the -v (verbose) switch to specify that the input coordinates should be pre-pended to each line. For example: [sh] cat dd.in MYRA MYDEC --------- --------- 344.740432 58.606523 344.731900 58.607634 344.725500 58.614301 [sh] funsky snr.ev dd.in MYRA:d MYDEC:d 510.00 510.00 512.00 510.50 513.50 513.50 [sh] funsky -v snr.ev dd.in MYRA:d MYDEC:d 344.740432 58.606523 510.00 510.00 344.731900 58.607634 512.00 510.50 344.725500 58.614301 513.50 513.50 In addition, a full starbase table can be output using the -T (table) switch. This switch can be used with or without the -v switch. If the -T and -v are both specified, then a descriptive header parameters are output before the table (mainly to remind you of the sky units): # output table in non-verbose mode [sh] funsky -T snr.ev dd.in MYRA:d MYDEC:d X Y ------------ ------------ 510.00 510.00 512.00 510.50 513.50 513.50 # output table in verbose mode [sh] funsky -T -v snr.ev dd.in MYRA:d MYDEC:d # IFILE = /Users/eric/data/snr.ev # ICOL1 = MYRA # ICOL2 = MYDEC # IUNITS1 = d # IUNITS2 = d # OCOL1 = X # OCOL2 = Y MYRA MYDEC X Y ------------ ------------ ------------ ------------ 344.740432 58.606523 510.00 510.00 344.731900 58.607634 512.00 510.50 344.725500 58.614301 513.50 513.50 Finally, the -d (ds9) switch mimicks ds9's use of integer TLMIN and TLMAX values for all coordinate transformations. FITS conventions seem to call for use of floating point TLMIN and TLMAX when the data are floats. This convention is followed by funsky but results in a small discrepancy with ds9's converted values for floating point data. We will remedy this conflict in the future, maybe. SEE ALSO
See funtools(7) for a list of Funtools help pages version 1.4.2 January 2, 2008 funsky(1)
All times are GMT -4. The time now is 03:30 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy