0

I want to retrieve more than one line of a text file, and processing lines.

Example :

$user = system('getent passwd') ."\n";

result exmple :

tcpdump:x:72:72::/:/sbin/nologin
guest:x:500:500:Guest User:/home/guest:/bin/bash
dovecot:x:97:97:Dovecot IMAP server:/usr/libexec/dovecot:/sbin/nologin
dovenull:x:495:488:Dovecots unauthorized user:/usr/libexec/dovecot:/sbin/nologin
sa-milt:x:494:487:SpamAssassin Milter:/var/lib/spamass-milter:/bin/bash
demo1:*:2417:100:albert DemoUn:/home/gir_net/demo1:/bin/bash
demo2:*:2418:100:alfred DemoDeux:/home/gir_net/demo2:/bin/bash
demo3:*:2419:100:alphonse DemoTrois:/home/gir_net/demo3:/bin/bash
demo4:*:2420:100:alcide DemoQuatre:/home/gir_net/demo4:/bin/bash

I want to retrieve the full line who have "/home/" in the result of command. I try many solution, but it's doesn't work.

After I retrieve I want to extract somme information of the lines who contains "/home/".

tshepang
  • 12,111
  • 21
  • 91
  • 136
mpgn
  • 7,121
  • 9
  • 67
  • 100

2 Answers2

1

Here is a possible solution using regular expression:

$user = shell_exec('getent passwd');
preg_match_all('/\/home\/[^:]+/', $user, $matches);

foreach ($matches[0] as $dir) {
    print $dir;
}

/* will print:
/home/guest
/home/gir_net/demo1
/home/gir_net/demo2
/home/gir_net/demo3
/home/gir_net/demo4
*/

Update:

If you want to display the full line, it will be easier to use string functions instead of regular expression.

$user = shell_exec('getent passwd');
$user = explode("\n", trim($user));
foreach ($user as $line) {
    if (strpos($line, '/home/') !== FALSE) {
        echo $line."\n";
    }
}
flowfree
  • 16,356
  • 12
  • 52
  • 76
  • For me, i see only : /home/gir_net/demo4, only the last. A var_dump($result) => array(1) { [0]=> array(1) { [0]=> string(32) "/home/giron_net/demo6" } } – mpgn Apr 23 '13 at 12:53
  • I forgot that `system()` only return the last line. Try to use `$user = shell_exec('getent passwd');` to get the full output string. – flowfree Apr 23 '13 at 12:55
  • Wow it's works, but match don't return the full line who contains "/home/" but she begining at /home/. I don't have : demo1:*:2417:100:albert DemoUn: – mpgn Apr 23 '13 at 13:03
  • Are you trying to get the full line or just the home directory? – flowfree Apr 23 '13 at 13:08
  • full line who have "/home/". We can preg_match with ":*:" too – mpgn Apr 23 '13 at 13:10
  • Wow, perfect, i understand, you extracting all the line in a tab, you through the lines an you search "/home/" – mpgn Apr 23 '13 at 13:22
0

Turn $user into an array and grep it. About extracting, if you want some help you should give details. Anyway come back after trying what you can obtain with explode and grep.

Community
  • 1
  • 1
ShinTakezou
  • 9,432
  • 1
  • 29
  • 39
  • I already try, but the result is an array with only the last line (demo4), demo1,2,3 or unrecognized... – mpgn Apr 23 '13 at 12:58
  • see bsdnoobz comment; in general I avoid the use of system so I am not accustomed to its details; once you have all the lines, explode and grep should work – ShinTakezou Apr 23 '13 at 13:02
  • 1
    Yeah, we need to use "shell_exec" not system. – mpgn Apr 23 '13 at 13:06