473,473 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

extracting a string

4 New Member
i have a input file of the format

0000GH000
0000GH000
0000NM000

How would i extract GHGHNM from this input file using perl scripts
Nov 20 '08 #1
6 2199
nithinpes
410 Recognized Expert Contributor
What have you tried? You can do it using regular expressions.
Nov 20 '08 #2
jahanaz
4 New Member
i am new to perl
hardly a weeks experience

i have tried this way

if ($string =~ /^CPDS/)
{
if ((substr($string, 38, 2) ne " "))
{
@chk = $string;
foreach $rec1 (@chk)
{
$chk1 = substr($rec1, 38, 2);
}
print "$chk1";
}

}
this code prints my desired result on console
but when it's printed on the file , i can see only the last 2 characters
Nov 20 '08 #3
nithinpes
410 Recognized Expert Contributor
The script you have posted has no relevance to the data that you posted initially. According to your initial posting, you were trying to extract two non-digits(alphabets) from each line and appending them. the following code will do that:
Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. open(DATA,"inputfile") or die "failed:$!";
  3. my $str="";
  4. while(<DATA>) {
  5. chomp;
  6. my $ch=$1 if(/(\D\D)/) ;  # extract consecutive non-digits
  7. print "$ch\n";
  8. $str.=$ch;   # append 
  9. }
  10. print "$str";
  11.  
If this does not solve your problem, please be more clear on what exactly you need.
Nov 20 '08 #4
jahanaz
4 New Member
i tried this but this does not give my desired result .
this is how it is supposed to work

first it looks for records starting with CPDS
then with in those CPDS records it checks whether position 38,2 is not null
if it's true
it has to get this 38,2 substring from all the choosen records
so that each substring would be concatenated to become a single string.

when i tried with ur code part it worked this way
LK
LKWN
WNPR
PR
Nov 20 '08 #5
nithinpes
410 Recognized Expert Contributor
i tried this but this does not give my desired result .
this is how it is supposed to work

first it looks for records starting with CPDS
then with in those CPDS records it checks whether position 38,2 is not null
if it's true
it has to get this 38,2 substring from all the choosen records
so that each substring would be concatenated to become a single string.

when i tried with ur code part it worked this way
LK
LKWN
WNPR
PR
Could you provide sample data from your file?
Nov 20 '08 #6
jahanaz
4 New Member
am sorry my friend

it worked
I was using ur code a different way initially . This is just a small part of the whole code . i really had to integrate ur logic with my program's logic.

anyway thanks a lot
Nov 20 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: lecichy | last post by:
Hello Heres the situation: I got a file with lines like: name:second_name:somenumber:otherinfo etc with different values between colons ( just like passwd file) What I want is to extract...
2
by: Sammy | last post by:
Subject line says it all really. I have a 4 character long string, that contains a 32bit integer. Problem is i have no idea how i go about extracting it. Any help will be appreciated.
2
by: Steve | last post by:
Hi, I have a very long string, someting like: DISPLAY=localhost:0.0,FORT_BUFFERED=true, F_ERROPT1=271\,271\,2\,1\,2\,2\,2\,2,G03BASIS=/opt/g03b05/g03/basis,...
2
by: Dickyb | last post by:
Extracting an Icon and Placing It On The Desktop (C# Language) I constructed a suite of programs in C++ several years ago that handle my financial portfolio, and now I have converted them to...
13
by: Randy | last post by:
Is there any way to do this? I've tried tellg() followed by seekg(), inserting the stream buffer to an ostringstream (ala os << is.rdbuf()), read(), and having no luck. The problem is, all of...
7
by: Tempo | last post by:
Hello. I am having a little trouble extracting text from a string. The string that I am dealing with is pasted below, and I want to extract the prices that are contained in the string below. Thanks...
2
by: VictorTan | last post by:
Hello. I'm new to this forum. Hope that I don't make mistakes in here but if I do, please correct me if there is. Thanks. I also wanted to ask you guys regarding about the following following...
6
by: Werner | last post by:
Hi, I try to read (and extract) some "self extracting" zipefiles on a Windows system. The standard module zipefile seems not to be able to handle this. False Is there a wrapper or has...
4
by: dexter48 | last post by:
Hi I'm searching for a string occurance in a text file. I find the string ok and write the results to a log file. But on the line above is also some information I need. How can i get that. The string...
5
by: Mukesh | last post by:
Hi, I am using framework 2.0. I am writing a foreach loop that will extract single dimensional arrays out of double dimensional array. I am trying writing something like this. string ...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.