用线输出的csv只包含一列 [英] output csv with lines that contains only one column
问题描述
与输入csv文件
sid,storeNo,latitude,longitude
2,1,-28.03720000,153.42921670
9
我希望只输出与一列的线,在这个例子中它是3行。
这怎么能在bash shell脚本来完成?
I wish to output only the lines with one column, in this example it's line 3. how can this be done in bash shell script?
推荐答案
用awk
下面的awk将usfull
The following awk would be usfull
$ awk -F, 'NF==1' inputFile
9
它的作用?
-
-F
设置字段分隔符为,
NF == 1
匹配以 NF
线,1场没有任何动作因此,提供默认操作,打印整个记录拍摄。它类似于 NF == 1 {$打印0}
NF==1
matches lines with NF
, number of fields as 1. No action is provided hence default action, printing the entire record is taken. it is similar to NF==1{print $0}
INPUTFILE
输入csv文件的awk脚本
inputFile
input csv file to the awk script
使用grep
同样的功能也可以用做的grep
The same function can also be done using grep
$ grep -v ',' inputFile
9
-
-v
不匹配模式的选项输出线-v
option prints lines that do not match the pattern,
与-v
沿里grep匹配不包含行,
along with-v
greps matches lines that do not contain,
field separator使用SED
$ sed -n '/^[^,]*$/p' inputFile 9
它做什么?
-
-n
燮presses模式空间的正常打印
-n
suppresses normal printing of pattern space
'/ ^ [^,] * $ /
选择匹配模式的线条,线条没有任何,
'/^[^,]*$/
selects lines that match the pattern, lines without any,
-
^
主播正则表达式在字符串的开头
^
anchors the regex at the start of the string
[^] *
匹配以外的任何其他,
[^,]*
matches anything other than,
$
在字符串结尾的字符串锚$
anchors string at the end of stringP
行动P
使得sed将打印当前的格局空间,也就是模式空间匹配输入p
actionp
makes sed to print the current pattern space, that is pattern space matching the input这篇关于用线输出的csv只包含一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
-