UNIX - 文件列数 [英] unix - count of columns in file

查看:132
本文介绍了UNIX - 文件列数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于像这样的数据文件(即stores.dat文件)

  SID | storeNo |纬度|经度
2 | 1 | -28.03720000 | 153.42921670
9 | 2 | -33.85090000 | 151.03274200

什么会outpout列名的数量的命令?

,即在上面的例子中这将是4(的配管的字符+ 1中的第一行号)

我的想法是这样的:

 的awk'{FS =| }; {打印NF}'stores.dat

但它返回所有行而不只是第一和第一线返回1而不是4。


解决方案

 的awk -F| {打印NF;退出}'stores.dat

第一行之后就退出了。

Given a file with data like this (ie stores.dat file)

sid|storeNo|latitude|longitude
2|1|-28.03720000|153.42921670
9|2|-33.85090000|151.03274200

What would be a command to outpout the number of column names?

ie in the example above it would be 4. (number of pipe characters + 1 in the first line)

I was thinking something like:

awk '{ FS = "|" } ; { print NF}' stores.dat

but it returns all lines instead of just the first and for the first line it returns 1 instead of 4.

解决方案

awk -F'|' '{print NF; exit}' stores.dat 

Just quit right after the first line.

这篇关于UNIX - 文件列数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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