动态列间计算

【问题】

I have the following sample tab delimited file:

.CvR Col_1 Col_2 Col_3 Col_4 Col_5
S1 1 0 1 0 1
S2 1 1 1 0 1
S3 1 1 1 1 1
S4 1 0 1 1 1
S5 1 0 1 1 1

I am trying to come up with a simple way to print the first column and all columns with just “1” values in them.

My desired output file should look like this:

.CvR Col_1 Col_3 Col_5
S1 1 1 1
S2 1 1 1
S3 1 1 1
S4 1 1 1
S5 1 1 1

My actual input file will be much bigger. I would like to do this in UNIX where possible. Can anybody help? Thanks.

#!/bin/bash
clear
value='\\(\[01\]\\)'
cp file file2
for i in 1 2 3 4 5 6; do
 sed -i "s/ ${value}/ val${i}_\\1/" file2
done
rowcount=$(wc -l <file2)
for i in 1 2 3 4 5 6; do
 if \[ $(grep -c val${i}_1 file2) -eq ${rowcount} \]; then
 sed -i "s/val${i}_./1/" file2
 else
 sed -i "s/Col_${i}//" file2
 sed -i "s/val${i}_.//" file2
 fi
done
cat file2

【回答】

动态列的问题还可以用 SPL 做,代码更简单些:

A
1=file("/user/data.txt").import@t()
2=A1.fname().to(2,).select(A1.field(~).count(~==1)==A1.count())
3=A1.new(${"\'.CvR\',"+A2.concat@c()})

集算器支持 windows\unix 命令行,还可与 JAVA 集成,参考【集算器实现文本处理的应用方案

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值