码迷,mamicode.com
首页 > Web开发 > 详细

php/awk 处理csv

时间:2018-07-15 22:18:21      阅读:289      评论:0      收藏:0      [点我收藏+]

标签:ftp   read   sof   input   row   tps   []   https   guide   

取第5列,去掉开头结尾的引号,匹配以http://, https://, ftp://开头的行

 

* awk

 awk -F"," ‘str=gsub(/(^\"*)|(\"*$)/,"",$5) {if($5~/(^http(s)?:\/\/)|(^ftp:\/\/)/) print $5}‘   ../data/t_video.csv > ../data/urls.csv

  

* php

<?php
// awk -F"," ‘str=gsub(/(^\"*)|(\"*$)/,"",$5) {if($5~/(^http(s)?:\/\/)|(^ftp:\/\/)/) print $5}‘ // ../data/t_video.csv > ../data/urls.csv

$in = new SplFileObject(‘../data/t_video.csv‘, ‘rb‘);

if (!$in) {
    throw new Exception(‘open file for read failed!‘);
}

$out = new SplFileObject(‘../data/urls.csv‘, ‘w‘);
if (!$out) {
    throw new Exception(‘open file for write failed!‘);
}

$a = []; $videoURL = ‘‘;
foreach ($in as $lineNum => $line) {
    $a = explode(‘,‘, $line);
    if (!isset($a[4])) {
        continue;
    }
    $videoURL = $a[4];
    if (7<strlen($videoURL)) {
        $videoURL = trim($videoURL, "\"");
        if (preg_match(‘/^[http:\/\/|ftp:\/\/|https:\/\/]/‘, $videoURL)) {
            $out->fwrite($videoURL.PHP_EOL);
        }
    }
}

  

input:

../data/t_video.csv

"9","其他","ULTIMATE PHOTO GUIDE",NULL,"http://118.190.209.209/media/mp4/1.mp4","http://118.190.209.209/media/png/1.png","ULTIMATE PHOTO GUIDE"
"10","其他","THE VERGE",NULL,"http://118.190.209.209/media/mp4/2.mp4","http://118.190.209.209/media/png/2.the_verge.png","THE VERGE"
"11","其他","Microsoft Power BI",NULL,"http://118.190.209.209/media/mp4/3.mp4","http://118.190.209.209/media/png/3.png","Microsoft Power BI"

  

output:

../data/url.csv

http://118.190.209.209/media/mp4/1.mp4
http://118.190.209.209/media/mp4/2.mp4
http://118.190.209.209/media/mp4/3.mp4

  

 

php/awk 处理csv

标签:ftp   read   sof   input   row   tps   []   https   guide   

原文地址:https://www.cnblogs.com/mingzhanghui/p/9314906.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!