Perl help - Searching for a pattern and return the position


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl help - Searching for a pattern and return the position
# 1  
Old 04-08-2009
Perl help - Searching for a pattern and return the position

Hi,
I need to search a file, in each line I need to check for occurance of '1' from a particular position through the next 32 bytes. If 1 is found, i need to return the position. Here is an example of the file and the output i need. Please help. I'm new to perl and unix.

File:
Code:
12341234567890123400100011001000100010000000001000233493849318
12341234567890123410000000000000100000001110000000238437483291
12341234567890123401000000010000000010000000001001028282783828
12341234567890123400000010000000010000001000000001122389393939

In the above file, starting from 19th byte i need to check if '1' is present in the next 32 bytes. I need to position as the output, each separated by ';'.
Here is the output I am expecting.

Code:
003;007;008;011;015;019;029;
001;015;023;024;025;
002;010;019;029;032;
007;016;023;032;

The first line starts with 003 since 1 is found in the 3rd position after the 19th byte of that line.
# 2  
Old 04-08-2009
next time, some effort on your part to solve your programming requirements would be nice to see, even if they do not work.

Code:
while(<DATA>){
   my $t = substr($_,18,32);
   while($t =~ /1/g) {
      printf "%03d;", pos($t);
   }
   print "\n";
}
__DATA__
12341234567890123400100011001000100010000000001000233493849318
12341234567890123410000000000000100000001110000000238437483291
12341234567890123401000000010000000010000000001001028282783828
12341234567890123400000010000000010000001000000001122389393939

make sure you have the "g" on the end of the regexp or you will put the script into an infinite loop.

Last edited by KevinADC; 04-08-2009 at 06:11 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep a pattern in a key position a file

Hi guys, So I have a file containing data of a marathon. Here's an example what it looks like including the given key: # key: sex, time, athlete, athlete's nationality, date, city, country M, 2:30:57.6, Harry Payne, GBR, 1929-07-05, Stamford Bridge, England M, 2:5:42, Khalid Khannouchi,... (3 Replies)
Discussion started by: DNM_UKN
3 Replies

2. UNIX for Dummies Questions & Answers

String pattern matching and position

I am not an expert with linux, but following various posts on this forum, I have been trying to write a script to match pattern of charters occurring together in a file. My file has approximately 200 million characters (upper and lower case), with about 50 characters per line. I have merged all... (5 Replies)
Discussion started by: biowizz
5 Replies

3. Shell Programming and Scripting

Searching and printing the only pattern using awk,sed or perl

Hi All, i have an output of command vmstat as below : $ vmstat System configuration: lcpu=4 mem=5376MB ent=1.00 kthr memory page faults cpu ----- ----------- ------------------------ ------------ ----------------------- r b avm fre re pi... (10 Replies)
Discussion started by: omkar.jadhav
10 Replies

4. Shell Programming and Scripting

awk pattern search with inconsistent position

Hi, Anybody knows how to get the value after the regexp and test it on pattern? The if the pattern matches, it will print the entire line on a separate file. Here's my raw file: ^_Name^_string^_Apple ^_Color^_string^_Red ^_Code^_string^_121 ^_Name^_string^_Banana ^_Code^_string^_123 ... (7 Replies)
Discussion started by: Jin_
7 Replies

5. Shell Programming and Scripting

Pattern match exclusive return pattern/variable

I have an application(Minecraft Server) that generates a logfile live. Using Crontab and screen I send a 'list' command every minute. Sample Log view: 2013-06-07 19:14:37 <Willrocksyea1> hello* 2013-06-07 19:14:41 <Gromden29> hey 2013-06-07 19:14:42 Gromden29 lost connection:... (1 Reply)
Discussion started by: gatekeeper258
1 Replies

6. Shell Programming and Scripting

Searching for a pattern and extracting records related to that pattern

Hi there, Looking forward to your advice for the below: I have a file which contains 2 paragraphs related to a particular pattern. I have to search for those paragraphs from a log file and then print a particular line from those paragraphs. Sample: I have one file with the fixed... (3 Replies)
Discussion started by: danish0909
3 Replies

7. Shell Programming and Scripting

Searching the file of below pattern through perl script from different location

Hi All, I am creating a small per script but I am getting the error "Unable to open file because No such file or directory".My code is below.I am trying to go into the below location so using chdir and then seraching the log files of below pattern. #!/usr/bin/perl -w chdir... (3 Replies)
Discussion started by: anuragpgtgerman
3 Replies

8. Shell Programming and Scripting

Insert carriage return on the 10th char position of each line

Hi experts, Need your help on how to insert carriage return after the 10th char position of each line in a file and then add two blank spaces after the carriage return. Example: >cat test.txt testingline dummystring samplesample teststringline Expected output should be.. ... (2 Replies)
Discussion started by: brichigo
2 Replies

9. Shell Programming and Scripting

Searching a pattern in file and deleting th ewhole line containing the pattern

Hi All, Please can someone assist in the script I have made that searches a pattern in a file and delete the whole line containing the pattern. #!bin/sh # The pattern that user want to add to the files echo "Enter the pattern of the redirect" read value # check if the user has... (1 Reply)
Discussion started by: Shazin
1 Replies

10. Shell Programming and Scripting

Pattern searching pattern in c files

I have a problem in searching a specific pattern in c files. My requirement: I have to find all the division operator in all cfiles. The problem is, the multi line comments and single line comments will also have forward slash in it. Even after avoiding these comments also, if both... (6 Replies)
Discussion started by: murthybptl
6 Replies
Login or Register to Ask a Question