LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-25-2006, 05:44 AM   #1
ohcarol
Member
 
Registered: Dec 2004
Location: Nepal
Posts: 86

Rep: Reputation: 15
parsing text file in php


I have these line in a text file called file.txt.

=====================================
1 ::lfp ::eth1 ::eth0 ::192.168.1.20 ::1 ::48 ::48 ::28 ::32 ::20
1 ::xzone ::eth1 ::eth0 ::192.168.1.21 ::1 ::48 ::48 ::28 ::32 ::21
#1 ::lec ::eth1 ::eth0 ::192.168.1.22 ::1 ::64 ::64 ::32 ::32 ::22
1 ::test ::eth1 ::eth0 ::192.168.1.23 ::1 ::48 ::48 ::32 ::32 ::23
====================================================


I want to parse these file so that only select text can be dislpayed. if there is comment on the line with "#" then that line should be skipped. How can I do that?

Output should be

Name: ipaddress: down: up: id:
lfp 192.168.1.20 48 28 20
zone 192.168.1.21 48 32 21
test 192.168.1.22 48 32 23


I have written this script but it didn't help

<?

$filename = "file.txt";
$fp = fopen("$filename", "r") or die("couldnot open a file");
while (!feof($fp)){
$line = fgets($fp, 1024);
if(preg_match("/^#/", $line) ){
next;
}
$client = split("eth0|eth1|::", $line);
foreach($client as $line)
print "$line<br>";
}

?>

Any help
 
Old 08-25-2006, 09:18 AM   #2
w3bd3vil
Senior Member
 
Registered: Jun 2006
Location: Hyderabad, India
Distribution: Fedora
Posts: 1,191

Rep: Reputation: 49
try using the cut command, something more like
cut -d:: -f1 file.txt
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
I need help parsing text from a text file rsmccain Linux - General 2 01-05-2006 02:43 PM
Need help parsing text file scilec Programming 5 12-02-2004 01:00 PM
need help parsing text file airman99 Linux - General 2 10-08-2004 09:09 PM
Parsing Text from a html file. Rezon Programming 6 10-18-2003 12:09 AM
Parsing a file for a string of text jamesmwlv Linux - General 2 12-02-2002 07:13 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:33 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration