bash 如何获取分隔字符串中的字段数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18351284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the count of fields in a delimited string?
提问by DoesJohnDo
Given the following data:
鉴于以下数据:
field1;field2;field3;field4;
How to get the number of fields in this string? For example, the command should give back 4for this case.
如何获取此字符串中的字段数?例如,对于这种情况,命令应该返回4。
Actually, I don't have the same number of contents in each line, but I need to return them all anyway. Once I manage to count the number of fields, I will also manage to make a loop to get them all.
实际上,我每行的内容数量并不相同,但无论如何我都需要返回它们。一旦我设法计算字段的数量,我还将设法进行循环以获取所有字段。
回答by devnull
You can say:
你可以说:
$ echo "field1;field2;field3;field4;" | grep -o ";" | wc -l
4
Alternatively,
或者,
$ tr -dc ';' <<< "field1;field2;field3;field4;" | wc -c
4
EDIT: In order to loop over the fields, you can say:
编辑:为了遍历字段,您可以说:
$ IFS=';' read -ra field <<< "field1;field2;field3;field4;"
$ for i in "${field[@]}"; do echo $i; done
field1
field2
field3
field4
回答by Shashilpi
Using cut is not the recommended command to do the job as it can be done in one line using awk.
不推荐使用 cut 命令来完成这项工作,因为它可以使用 awk 在一行中完成。
For e.g.
例如
data='field1;field2;field3;field4;'
echo $data|awk -F';' '{print NF}'
Actually above will return 5 because in given data there is "semicolon" at the end, hence linux awk assumes that last field is empty.
实际上上面将返回 5 因为在给定的数据中最后有“分号”,因此 linux awk 假设最后一个字段为空。
But if this is expected to have it like this then you can use below command to subtract 1.
但是,如果预计会有这样的情况,那么您可以使用以下命令减去 1。
echo $data|awk -F';' '{print NF-1}'
Explanation: -F option in awk is for delimiter, in this case it's semicolon (enclosed in single quote) NF means Number of fields.
说明:awk 中的 -F 选项用于分隔符,在这种情况下它是分号(用单引号括起来) NF 表示字段数。
回答by Aleks-Daniel Jakimenko-A.
Pure bash solution:
纯bash解决方案:
myStr='field1;field2;field3;field4;'
x="${myStr//[^;]}" # remove everything but the delimiter ';'
echo "${#x}" # get the length - outputs 4
回答by David Pena
The option I have provided still holds up without bugs or manipulation.
我提供的选项仍然没有错误或操作。
j=0
i=1
while [ "$j" ]
do
j="$(cat dataFile.txt | cut -d[delimiter] -f$i)"
i="$(($i+1))"
done
echo "$(($i-2))"
I initialize a variable $j
to hold the results, and as long as the results exist, the loop counts each run. cut
always runs till 2 past the field, explaining my $(($i-2))
statement.
我初始化一个变量$j
来保存结果,只要结果存在,循环就会对每次运行进行计数。cut
总是跑到 2 过去,解释我的$(($i-2))
陈述。
Put your data-file's delimiter after cut -d I used a comma to separate my fields, so my actual code has: cut -d, -f$i
把你的数据文件的分隔符放在 cut -d 之后我用逗号来分隔我的字段,所以我的实际代码有:cut -d, -f$i
I hope this makes sense.
我希望这是有道理的。
回答by hàqk
#!/bin/bash
DATA="field1;field2;field3;field4;"
# save IFS
OLDIFS=$IFS
# set new IFS
IFS=";"
# set DATA as position parameters
set -- ${DATA//\*/\*}
# simply get parameter count
echo "Fields: $#"
# restore IFS
IFS=$OLDIFS