getting data out from a text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting getting data out from a text file
# 1  
Old 09-11-2002
getting data out from a text file

Hi
Is it possible for me to extract particular lines from the text lines below. For example I want to extract the first three lines and the 4 lines below <owner>. I ant to put them in one line like thus:
12.33.164.12 UNITED HEALTHCARE CORP. 6300 OLSEN MEMORIAL HIGHWAY GOLDEN VALLEY, MN 55427 US

I have a file full of data like this and would like to create rows with jsut the IP address and the owner.

Can you please help!!

<ip address/hostname>
12.33.164.12
uhgatt12.uhc.com

<net block>
12.33.164.0 - 12.33.164.15

<owner>
UNITED HEALTHCARE CORP.
6300 OLSEN MEMORIAL HIGHWAY
GOLDEN VALLEY, MN 55427
US

<administrative contact>
Elverhoy, Dave dehoy@uhc.com
(612)797-2113

<additional data>
UNITED-164-0
Updated: 28-Jun-2000
Source: rs1.arin.net
# 2  
Old 09-11-2002
sounds like your trying to build your own version of a whois record for some sort of program that logs ip addys of hosts you have "tested" for something.

i think i wont touch this one. but i will point you in teh right direction.

why dont you check out a book by oreilly its called "Mastering Regulard Expressions 2nd edition"
# 3  
Old 09-11-2002
Either that, or for spam...
# 4  
Old 09-12-2002
just about right...

you are right in that I am trying to use the output of an whois report. Thanks for pointing me in the right direction with this. Let me assure you... this is not for spamming. I am jsut getting the output in a tabular manner.

I will check out the O'Reily book... thank you!
# 5  
Old 09-13-2002
well, if you are using Perl, i think you can easily retrieve those information by using "split" and also "=~" commands to search for the right kind of data that you wish to retrieve.
i have once done this to retrieve the information displayed on the screen by the iptables,
first i saved the screen in a file and then i split them up into pieces for easy lookup. last but not least i use the "=~" commands to look for the right words.
# 6  
Old 09-13-2002
Quote:
Originally posted by mercz
well, if you are using Perl, i think you can easily retrieve those information by using "split" and also "=~" commands to search for the right kind of data that you wish to retrieve.
i have once done this to retrieve the information displayed on the screen by the iptables,
first i saved the screen in a file and then i split them up into pieces for easy lookup. last but not least i use the "=~" commands to look for the right words.
=~ is not a command it is an operator. to be more specific its a binding operator to tie a scalar value to a pattern matching expression which will be on the right side of the operator.
# 7  
Old 09-13-2002
Am I good or just bored?Smilie

don't answer that.



Code:
#!/usr/local/bin/perl
#by photon
#

$file = 'data.txt' ;
open(INFO, "$file" ) ;
@lines = <INFO> ;
close(INFO) ;


$count = 0;

foreach $line (@lines){

        if ($line =~ m/\<ip address\/hostname\>/){
                $count = 1;
        }elsif($line =~ m/\<owner\>/){
                $count = 2;
        }elsif($count == 1){
                push(@data, $line);
                print "$line \n";
                $count = 0;
        }elsif($count == 2){
                 if ($line !~ m/\<administrative contact\>/ ){
                         push(@data, $line);
                         print "$line \n";
                 }else{
                         $count = 0;
                  }
         }else{ # Reset count
                 $count = 0;
         }
}

open ( REPORT_FILE, ">report.txt" ) || die " report.txt $! \n " ;

foreach $data (@data){

format REPORT_FILE =
@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$data
.
write REPORT_FILE ;
}
print "\nSee report.txt\nALL DONE\n\n" ;

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read csv file, convert the data and make one text file in UNIX shell scripting

I have input data looks like this which is a part of a csv file 7,1265,76548,"0102:04" 8,1266,76545,"0112:04" I need to make the output data should look like this and the output data will be part of text file: 7|1265000 |7654899 |A| 8|12660000 |76545999 |B| The logic behind the... (6 Replies)
Discussion started by: RJG
6 Replies

2. Shell Programming and Scripting

Compare 2 text file with 1 column in each file and write mismatch data to 3rd file

Hi, I need to compare 2 text files with around 60000 rows and 1 column. I need to compare these and write the mismatch data to 3rd file. File1 - file2 = file3 wc -l file1.txt 58112 wc -l file2.txt 55260 head -5 file1.txt 101214200123 101214700300 101250030067 101214100500... (10 Replies)
Discussion started by: Divya Nochiyil
10 Replies

3. Shell Programming and Scripting

Help with grepping data from a text file

Hello, I have a text file which contains a list of strings which I want to grep from another file where these strings occur and print out only these lines. I had earlier used the grep command where File1 was the file containing the strings to be grepped (Source File) and File2 the Target File... (4 Replies)
Discussion started by: gimley
4 Replies

4. Shell Programming and Scripting

Filter a .kml file (xml) with data set from text file

I have a .kml file. So I want filter the .kml to get only the tags that have this numeric codes that they are in a text file 11951 11952 74014 11964 11965 11969 11970 11971 11972 60149 74018 74023 86378 11976 11980 11983 11984 11987 (5 Replies)
Discussion started by: pcoj33
5 Replies

5. Shell Programming and Scripting

Find and replace data in text file with data in same file

OK I will do my best to explain what I need help with. I am trying to format an ldif file so I can import it into Oracle oid. I need the file to look like this example. Keep in mind there are 3000 of these in the file. changetype: modify replace: userpassword dn:... (0 Replies)
Discussion started by: timothyha22
0 Replies

6. Shell Programming and Scripting

Data fetched from text file and save in a csv file

Hi i have wriiten a script which fetches the data from text file, and saves in the output in a text file itself, but i want that the output should save in different columns. I have the output like: For Channel:response_time__24.txt 1547 data points 0.339 0.299 0.448 0.581 7.380 ... (1 Reply)
Discussion started by: rohitkalia
1 Replies

7. Shell Programming and Scripting

Get data from two text file in one loop

Hi all I have two file that contain different data.I want to get each file data line by line in one loop.i try with cat but cat works ok against one file File a 23 34 45 File b abc xyz cgh now i want like this in a loop.Below is just example.i am doing some thing smiler.can any... (3 Replies)
Discussion started by: aliahsan81
3 Replies

8. Windows & DOS: Issues & Discussions

Filter data from text file

Hi All We have got a text file, which has data dumped from 60 tables. From these 60 tables of data we need data from 4 tables only. I tried assigning line numbers to filter out data, but it is not working as intended. below is the sample file ----Table1----- 3,dfs,43,df 4,sd,5,edd... (18 Replies)
Discussion started by: b_sri
18 Replies

9. Shell Programming and Scripting

Extracting data from text file based on configuration set in config file

Hi , a:) i have configuration file with pattren <Range start no>,<Range end no>,<type of records to be extracted from the data file>,<name of the file to store output> eg: myfile.confg 9899000000,9899999999,DATA,b.dat 9899000000,9899999999,SMS,a.dat b:) Stucture of my data file is... (3 Replies)
Discussion started by: suparnbector
3 Replies

10. Shell Programming and Scripting

How to extract data from a text file

Hello All, Is there an easy way to extract data from a text file? The text file is actually a dump of a 2 page report with 6 columns and 122 lines. Example is Report Tile Type Product 1 Product 2 Product 3 Product 4... (1 Reply)
Discussion started by: negixx
1 Replies
Login or Register to Ask a Question