根据bash中的条件输出重定向csv

时间:2017-03-02 07:06:13

标签: bash shell

我有一个非常大的csv文件,我想根据某个条件导出部分内容 - 我需要保留所有列,但只导出csv的summary_id>部分92900.如何在BASH中编写此命令?

$ head -n 10 snapshots.csv
id,station_id,status,available_bikes,available_docks,created_at,summary_id
32583548,465,Active,18,7,2015-12-12 12:40:07.325473,92982
32583549,466,Active,28,33,2015-12-12 12:40:07.330174,92982
32583550,467,Active,13,17,2015-12-12 12:40:07.334921,92982
32583551,468,Active,21,22,2015-12-12 12:40:07.339576,92982
32583552,469,Active,44,19,2015-12-12 12:40:07.344086,92982
32583553,470,Active,12,46,2015-12-12 12:40:07.34872,92982
32583554,471,Active,12,11,2015-12-12 12:40:07.353296,92982
32583555,472,Active,12,19,2015-12-12 12:40:07.357868,92982
32583556,473,Active,0,29,2015-12-12 12:40:07.362545,92982

2 个答案:

答案 0 :(得分:0)

当然,awk可能最适合这项任务(见评论Anubhava),但是因为你在BASH中问过怎么做:

#!/bin/bash
cat snapshots.csv |
while read line ; do
    value=${line##*,}
    if [ $value -gt 92900 ] ; then
        echo $line
    fi
done

${line##*,}将删除从$ line到(包括)最后一个逗号的所有内容。

答案 1 :(得分:0)

@anubhava在评论中完全正确。 public ActionResult Edit (int? id) { using (var db = new EmployeeContext()) { var model = new GradeSelectListViewModel(db.Employees.Find(id), db.Grades); //model.Employee = db.Employees.Single(x => x.EmployeeID == id); model.Grades = db.Grades.ToList().Select(x => new SelectListItem { Value = x.ID.ToString(), Text = x.Level }); return View(model); } } 是此处使用的工具。

@model MVCDemo.ViewModels.GradeSelectListViewModel
....
@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()
    ....
    @Html.DropDownListFor(x => Model.Employee.Level,
        new SelectList(Model.Grades, "ID", "Level"),
        "Select Level")
    ....
    <input type="submit" value="Create" class="btn btn-default" />
}

解释 -

awk将分隔符设置为逗号。

awk -F, '$NF > 92900' file.csv > exported.csv -F,中的内置变量,它引用了最后一列。如果您并不总是将最后一列作为您感兴趣的字段,也可以使用$NF

更清楚地编写命令的方法是 -

awk

$7