仅当特定列不为空时才打印CSV行 [英] Print CSV rows only if particular column is not empty

查看:40
本文介绍了仅当特定列不为空时才打印CSV行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试访问一个大的csv文件,如果特定的列为空,我正在尝试将该行放入其他其他csv中以进行进一步处理.我已经编写了一段代码来处理相同的代码,但是问题是一列包含的数据量很大,因此我的代码将这一列作为单独的一行.

I am trying to access the csv file which is big one and if the particular column is empty i am trying to put the row into other other csv for further process. I have written the piece of code to process the same but the problem is that one column is having huge data with spacing hence my code is taking that column as a separate line.

IFS=$'\n'
for line in `cat file.csv`; do
    echo "Processing for $line"
    res=`echo $line | cut -d ',' -f2`
    if [ '$res' != '0' ];then
        echo $line >> results.xlsx
    fi
done

推荐答案

我将使用awk.首先是一些数据:

I'd use awk for it. First some data:

$ cat assumed_data
1,2
3,
4,5

awk:

$ awk -F, '$2==""' assumed_data

输出:

3,

和一些解释:

  • awk 使用awk完成工作
  • -F,将输入字段定界符( FS )设置为逗号
  • '$ 2 =="'条件:如果第二个字段为空,则执行操作,默认操作是输出当前记录
  • 要处理的
  • assumed_data 文件
  • awk using awk for the job
  • -F, set input field delimiter (FS) to a comma
  • '$2==""' condition: if the second field is empty execute action and the default action is to output current record
  • assumed_data file to process

这篇关于仅当特定列不为空时才打印CSV行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆