LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   parsing text file in php (https://www.linuxquestions.org/questions/programming-9/parsing-text-file-in-php-477215/)

ohcarol 08-25-2006 05:44 AM

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

w3bd3vil 08-25-2006 09:18 AM

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


All times are GMT -5. The time now is 07:46 PM.