473,587 Members | 2,509 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help in extracting particular lines in a file

1 New Member
Hello Folks

I am working on extracting lines

of data in a text file based

on the string it contains.

This is the text file called info.txt:

701,apple,red,r ound,cider
214,car,pink,sp orty,cute,
229,knife,silve r,sharp,cut


The program i am working on asks for input from a user .
Thus if the user enters 214 the program
then matches the number to a particular line ( in the text file ) starting with that number (in this case the second line)
and prints out everything on only that particular line.


And this is the program
i have so far

print "type your number";
$num=<STDIN>;
chomp($num);


open (DAT, 'info.txt') || die "file not opened";
@line=<DAT>;

foreach (@line)
{
chomp;

if ($_=~ m/^$num/)
{
print "$_\n";
}
}
close (DAT);



I am learning perl as my first programming language and
any help will be appreciated

Thanks in advance.
Jul 19 '07 #1
3 1754
KevinADC
4,059 Recognized Expert Specialist
Do you need to turn your homework in tomorrow?

See the other forum where you have ths posted.
Jul 19 '07 #2
numberwhun
3,509 Recognized Expert Moderator Specialist
Hello Folks

I am working on extracting lines

of data in a text file based

on the string it contains.

This is the text file called info.txt:

701,apple,red,r ound,cider
214,car,pink,sp orty,cute,
229,knife,silve r,sharp,cut


The program i am working on asks for input from a user .
Thus if the user enters 214 the program
then matches the number to a particular line ( in the text file ) starting with that number (in this case the second line)
and prints out everything on only that particular line.


And this is the program
i have so far

print "type your number";
$num=<STDIN>;
chomp($num);


open (DAT, 'info.txt') || die "file not opened";
@line=<DAT>;

foreach (@line)
{
chomp;

if ($_=~ m/^$num/)
{
print "$_\n";
}
}
close (DAT);



I am learning perl as my first programming language and
any help will be appreciated

Thanks in advance.
I am not sure which other forum you posted this to (thanks for the heads up Kevin), but hopefully they guided you correctly.

What I see is that you are trying to use a variable in a Regular Expression and that is not possible. Variable Interpolation does not happen in a regex match. You have to produce a regex that would match the data you are trying to identify. The regex isn't that bad and shouldn't be to hard to figure out. Why not try and write an appropriate regex and if you are still having issues, repost here for more assistance. Be sure not to keep cross posting between different forums as a number of us do lurk/post to other forums as well ( as you found out).

Regards,

Jeff
Jul 20 '07 #3
KevinADC
4,059 Recognized Expert Specialist
he/she is getting help on the other forum.
Jul 20 '07 #4

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

Similar topics

4
6945
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 some part of it like all names or numbers from each line, simply text fom between e.g. second and third colon. And turn it
18
1660
by: v4vijayakumar | last post by:
is there any standard way to extract output from system(3) function? TIA.
0
2414
by: georges the man | last post by:
The purpose: • Sorting and Searching • Numerical Analysis Design Specification You are to write a program called “StockAnalyser”. Your program will read a text file that contains historical price of a stock. The program will allow users to query the stock price of a particular date and output its statistics (mean price, median price and standard deviation) for any specified period. Your program must be menu driven and works as follows.
0
2212
by: Jack Wu | last post by:
Hi I've spent a good majority of my day trying to figure out how to have PIL 1.1.5 working on my OSX 10.3.9_PPC machine. I'm still stuck and I have not gotten anywhere. Could somebody please help me... I've scoured all the documentation, google, and mailing lists to no avail. I believe the problem may lay in a jpeglib problem with OSX 10.3.9, or a python paths problem.
0
5566
by: raypjr | last post by:
Hi everyone. I'm new here and hope I can get a little advice on how to list my array into a ListBox. I have my structure and array of structures. I need help with a For Loop that will list the particular elements in the List Box. Any suggestions on how to best write this? ' I create a Structure with members ' place, title, author, publisher, price, description, lastweek, weeksonlist Structure bestseller Public place...
2
1998
by: raypjr | last post by:
Hi everyone. I'm new here and hope I can get a little advice on how to list my array into a ListBox. I have my structure and array of structures. I need help with a For Loop that will list the particular elements in the List Box. Any suggestions on how to best write this? ' I create a Structure with members ' place, title, author, publisher, price, description, lastweek, weeksonlist Structure bestseller Public place As Integer...
9
1976
by: Paulers | last post by:
Hello, I have a log file that contains many multi-line messages. What is the best approach to take for extracting data out of each message and populating object properties to be stored in an ArrayList? I have tried looping through the logfile using regex, if statements and flags to find the start and end of each message but I do not see a good time in this process to create a new instance of my Message object. While messing around with...
2
1984
by: avik1612 | last post by:
hi, I have created a script in perl to extract n number of zip files to a particular destination after creating the folder of the zip file. It is extracting . But when the zip file has subdirectories or folders inside it it ignores that and eztracts the whole contents without making folders what shout it do I am using activeperl in Windows XP i have pasted the code
4
8652
by: Ant | last post by:
Hi all, My kids have a bunch of games that have to be run from CD (on Windows XP). Now they're not very careful with them, and so I have a plan. I've downloaded a utility (Daemon Tools) which allows me to mount and unmount virtual CD drives onto an ISO image. This works well, but I want to wrap this in a Python GUI which will present them with a list of games in a particular directory. Essentially the steps are this:
0
7923
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8216
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
7974
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8221
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5395
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3845
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2364
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1192
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.