473,699 Members | 2,329 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Get some lines from a file

4 New Member
Hey all. I am pretty new to this. But I was hopeing i can get some help from here. Have searched the but haven't found the right thing for me.

I would need a program that will read a file, with a lot of lines, then get some lines starting from <!------ TÄNA KANAL 2-S ----------> then these lines will be printed out and when comes this line: <!------//TÄNA KANAL 2-S ----------> the program will end. It should also save the lines to another file.
I think its not possible with that <!------//TÄNA KANAL 2-S ----------> line because it has those two slashes, or am i wrong?

Expand|Select|Wrap|Line Numbers
  1. The file beginning...
  2. ...
  3. ...
  4. <!------ TÄNA KANAL 2-S ---------->
  5. ...
  6. Some lines
  7. ...
  8. <!------//TÄNA KANAL 2-S ---------->
  9. ...
  10. ...
  11. The end of the file
  12.  
Sep 16 '07 #1
9 2005
numberwhun
3,509 Recognized Expert Moderator Specialist
Hey all. I am pretty new to this. But I was hopeing i can get some help from here. Have searched the but haven't found the right thing for me.

I would need a program that will read a file, with a lot of lines, then get some lines starting from <!------ TÄNA KANAL 2-S ----------> then these lines will be printed out and when comes this line: <!------//TÄNA KANAL 2-S ----------> the program will end. It should also save the lines to another file.
I think its not possible with that <!------//TÄNA KANAL 2-S ----------> line because it has those two slashes, or am i wrong?

Expand|Select|Wrap|Line Numbers
  1. The file beginning...
  2. ...
  3. ...
  4. <!------ TÄNA KANAL 2-S ---------->
  5. ...
  6. Some lines
  7. ...
  8. <!------//TÄNA KANAL 2-S ---------->
  9. ...
  10. ...
  11. The end of the file
  12.  
First, Welcome to TSDN and to Perl!

Second, Perl is the best language you could choose for doing anything with text since it is the one thing it was truly designed to handle.

Unfortunately, we do not provide a script writing service here so you are going to have to do some coding yourself. But, we will certainly help you when you get stuck. All you need to do is post to this forum the code that you are working on(enclosed in code tags of course), and we will assist you over the hump in your way.

Why not post what you have so far and we can help you. If you need some "learning Perl" resources, also let us know and we can give you a couple of good links.

Regards,

Jeff
Sep 16 '07 #2
numberwhun
3,509 Recognized Expert Moderator Specialist
Hey all. I am pretty new to this. But I was hopeing i can get some help from here. Have searched the but haven't found the right thing for me.

I would need a program that will read a file, with a lot of lines, then get some lines starting from <!------ TÄNA KANAL 2-S ----------> then these lines will be printed out and when comes this line: <!------//TÄNA KANAL 2-S ----------> the program will end. It should also save the lines to another file.
I think its not possible with that <!------//TÄNA KANAL 2-S ----------> line because it has those two slashes, or am i wrong?

Expand|Select|Wrap|Line Numbers
  1. The file beginning...
  2. ...
  3. ...
  4. <!------ TÄNA KANAL 2-S ---------->
  5. ...
  6. Some lines
  7. ...
  8. <!------//TÄNA KANAL 2-S ---------->
  9. ...
  10. ...
  11. The end of the file
  12.  
Here are some links that you will find very handy in your quest to learn Perl.

1. Beginning Perl - An online book that is pretty good.

2. perldoc.perl.or g - This is where you will find a lot of references for the Perl language online.

3. perlfaq - These are the Perl FAQ's. You will want to read these as they cover some standard ways of doing things and answer a lot of beginner questions as well.

4. CPAN - CPAN is where all of the Perl modules are stored. You should search here first for a module to do what you want to do. You will often find links to modules given out here to help with a situation.

I hope these help.

Regards,

Jeff
Sep 16 '07 #3
KevinADC
4,059 Recognized Expert Specialist
I think its not possible with that <!------//TÄNA KANAL 2-S ----------> line because it has those two slashes, or am i wrong?
The two slashes will have no affect on searching through the file and finding what you want.
Sep 16 '07 #4
jaxx
4 New Member
Well Thank you all, who responded. Well i have a code but i have come to a conclusion that this will only take a line that starts and ends with those lines, but i would like to get the lines that are between those lines.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. open (KANAL2, "/home/jaxx/MyScripts/IRC_Kava/KANAL2/index.r4w") or die "The file is missing or unreadable";
  4. for $line (<KANAL2>) {
  5.      if ($line =~ /^<!------ TÄNA KANAL 2-S ---------->.+<!------//TÄNA KANAL 2-S ---------->$/) {
  6.         open (KANAL2_KAVA, "+>/home/jaxx/MyScripts/IRC_Kava/KANAL2/kava.text") or die "Can't create the file!";
  7.          print $line;
  8.         close KANAL2_KAVA
  9.     }
  10. }
  11. close KANAL2;
Also what i know is that that ^ shows the start of the line and $ the end of a line. But those regex (i think it was called like that) is pretty much like chinese to me. I hope there aren't any mistakes in that program?
And Excuse my enligsh, im from Estonia and my english isin't very good.


EDIT: Oh and numberwhun, i have read that perl is the best language to be used as a text manipulating. Err i think sentence did not come out right, and i hope you understood it.
Also thanks for the different links, am reading the Beginning perl right now. A little late (23:02), have to go to school tomorrow but, yeah i can manage :D
Sep 16 '07 #5
KevinADC
4,059 Recognized Expert Specialist
One possible way to do what you are trying to do:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. my @lines;
  5. open (KANAL2, "/home/jaxx/MyScripts/IRC_Kava/KANAL2/index.r4w") or die "The file is missing or unreadable: $!";
  6. OUTTER: while (<KANAL2>) {
  7.    if (/^\Q<!------ TÄNA KANAL 2-S ---------->\E/) {
  8.       INNER: while (<KANAL2>) {
  9.          if (/^\Q<!------\/\/TÄNA KANAL 2-S ---------->\E$/) {
  10.             open (KANAL2_KAVA, ">/home/jaxx/MyScripts/IRC_Kava/KANAL2/kava.text") or die "Can't create the file: $!";
  11.             print  KANAL2_KAVA @lines;
  12.             close KANAL2_KAVA;
  13.             next OUTTER;
  14.          }
  15.          push @lines,$_;
  16.       }
  17.    }
  18. }
  19. close KANAL2;
I find this approach more straight forward than what you are trying to do but you can use one regexp to grab the lines between the patterns if you wanted to. If there is only one set of lines between the patterns you can change:

next OUTTER;

to:

last OUTTER;

and the loop will end at that point.
Sep 16 '07 #6
jaxx
4 New Member
Hmm, I found out that there are some whitespaces in front of those patterns, and then i should put an \s+ in front or after the \Q regexs?

EDIT: actualy there was a tab and then some whitespaces, i put the tab and whitespaces in the code, and it worked, and very good, than you for your help anyone. If i get into trouble again, with my code then i am sure i will come here ^^. But actualy yeah, if there are some tabs and whitespaces how would i have to use those regular expressions?
Sep 17 '07 #7
KevinADC
4,059 Recognized Expert Specialist
\s+ will find tabs so you can just use \s+:

Expand|Select|Wrap|Line Numbers
  1. if (/^\s+\Q<!------ TÄNA KANAL 2-S ---------->\E/) {
Sep 17 '07 #8
jaxx
4 New Member
Oh before the \Q ? :D dam, i jsut thought that affter that \Q, i guess that why it didn't work for the first time i tried it. Ok tnx, another thing i learned.
Sep 18 '07 #9
KevinADC
4,059 Recognized Expert Specialist
Oh before the \Q ? :D dam, i jsut thought that affter that \Q, i guess that why it didn't work for the first time i tried it. Ok tnx, another thing i learned.

Look up what \Q means and you will understand.
Sep 19 '07 #10

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

Similar topics

2
2067
by: Jesse Noller | last post by:
I am a relative newbie to python and I am having issues trying to iterate over the lines of a file. I have a text file - foo.bar inside of this file are lines of text: x-3411342 y-1324123 w-2314121 Each with a trailing \n to designate the end of the line.
22
61376
by: Ling Lee | last post by:
Hi all. I'm trying to write a program that: 1) Ask me what file I want to count number of lines in, and then counts the lines and writes the answear out. 2) I made the first part like this: in_file = raw_input("What is the name of the file you want to open: ") in_file = open("test.txt","r")
1
6922
by: j | last post by:
Hi, I've been trying to do line/character counts on documents that are being uploaded. As well as the "counting" I also have to remove certain sections from the file. So, firstly I was working with uploaded MS WORD .doc files. Using code like that below: strLine = sr.ReadLine While Not IsNothing(strLine) 'Not eof If Trim(strLine) <> "" Then 'Not blank
2
3015
by: Murali | last post by:
Hello Everyone, I am breaking my head with this problem. Please help me out. Let me first explain my problem : Here it is: I am working in realtime environment where i will be creating logfiles internally while my system is running.
2
1678
by: m00nm0nkey | last post by:
Ok well i thought i'd try a different approach, so what I'm now trying is appending 50,000 lines from the collection to a stringbuilder, and then writing that entire stringbuilder to a file. However, look at this log: 21/04/2006 14:09:06: Building String Start 21/04/2006 14:09:14: appended 10,000 lines to the stringbuilder 21/04/2006 14:09:39: appended 10,000 lines to the stringbuilder 21/04/2006 14:10:20: appended 10,000 lines to the...
6
24152
by: ivan.perak | last post by:
Hello, im a beginner in VB.NET... The thing i would like to do is as it follows.... I have a text file (list of names, every name to the next line) which is about 350000 lines long. I would like to split it and create a new file at every lets say 20000 lines... so, the directory output would have to be something like this:
7
2100
by: peraklo | last post by:
Hello, there is another problem i am facing. i have a text file which is about 15000 lines big. i have to cut the last 27 lines from that file and create a new text file that contans those 27 lines. and after that save both of those files... since that is a big block of text (15000 lines) i thint that it is a big job to look for a keyword... so my question is this exactly: how do i do this:
7
3268
by: Gustaf | last post by:
Hi all, Just for fun, I'm working on a script to count the number of lines in source files. Some lines are auto-generated (by the IDE) and shouldn't be counted. The auto-generated part of files start with "Begin VB.Form" and end with "End" (first thing on the line). The "End" keyword may appear inside the auto-generated part, but not at the beginning of the line. I imagine having a flag variable to tell whether you're inside the...
3
3590
by: Robert | last post by:
I would like to count lines in a file using the fileinput module and I am getting an unusual output. ------------------------------------------------------------------------------ #!/usr/bin/python import fileinput # cycle through files for line in fileinput.input(): if (fileinput.isfirstline()): if (fileinput.lineno 1):
2
4096
by: rka77 | last post by:
Hi, I am trying to make a Python2.6 script on a Win32 that will read all the text files stored in a directory and print only the lines containing actual data. A sample file - Set : 1 Date: 10212009 12 34 56 25 67 90 End Set ******** Set: 2 Date: 10222009
0
8692
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
8620
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9181
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...
0
9040
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8889
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
5877
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
4378
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
4633
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3060
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

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.