bash 无法分隔分号分隔的行 awk
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18634532/
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
Unable to separate semi-colon separated line awk
提问by Sriram
I am trying to do the following:
我正在尝试执行以下操作:
- Read a file line by line.
- Each line has the following structure:
field1;field2;field3
- Use
awk
to separate each of these fields and then process each of these fields further
- 逐行读取文件。
- 每行具有以下结构:
field1;field2;field3
- 使用
awk
分隔每个字段,然后进一步处理这些字段的
The snippet of code I have is:
我拥有的代码片段是:
while read l
do
n=`echo ${l} | awk --field-separator=";" '{print NF}'`
field1=`echo ${l} | awk --field-separator=";" '{print }'`
field2=`echo ${l} | awk --field-separator=";" '{print }'`
field3=`echo ${l} | awk --field-separator=";" '{print }'`
echo ${n} ${field1} ${field2} ${field3}
done < temp
Where temp contains only the following line:
其中 temp 仅包含以下行:
xx;yy;zz
The answer I get on the command line is:
我在命令行上得到的答案是:
1 xx;yy;zz
I am not sure I understand this output. Any explanations would be nice, given that it does work for other files. I am working on a Mac while this code uses awk
within a bash
script.
我不确定我是否理解这个输出。任何解释都会很好,因为它确实适用于其他文件。我在 Mac 上工作,而这段代码awk
在bash
脚本中使用。
采纳答案by Ed Morton
Your awk has no idea what --field-separator=";"
means so when you do this:
--field-separator=";"
当你这样做时,你的 awk 不知道是什么意思:
awk --field-separator=";" '{print }'
your awk is still using the default FS of a space, and so $1 contains your whole input line while $2 and $3 are empty. Use -F';'
to set the FS.
您的 awk 仍在使用空格的默认 FS,因此 $1 包含您的整个输入行,而 $2 和 $3 为空。使用-F';'
设置FS。
You are WAY, WAY off the mark in how to write the script you want. If you tell us more about what "process each field" is, we can help you.
在如何编写您想要的脚本方面,您做得很好。如果您告诉我们更多关于“处理每个字段”是什么,我们可以为您提供帮助。
回答by Aleks-Daniel Jakimenko-A.
Why awk when you can do it in pure bash?
当您可以在纯 bash 中执行此操作时,为什么要使用 awk?
while IFS=';' read -r field1 field2 field3; do
echo "Field1: $field1"
echo "Field2: $field2"
echo "Field3: $field3"
done < file.txt
Or if you don't know the field count:
或者,如果您不知道字段数:
while IFS=';' read -ra fields; do
echo "Number of fields: ${#fields[@]}"
echo "Field1 ${fields[0]}"
done < file.txt
回答by konsolebox
It's probably a bug with your awk. Try other formats like these:
这可能是你的 awk 的一个错误。尝试其他格式,例如:
while read l
do
n=`echo "${l}" | awk -F\; '{print NF}'`
field1=`echo "${l}" | awk -F\; '{print }'`
field2=`echo "${l}" | awk -F\; '{print }'`
field3=`echo "${l}" | awk -F\; '{print }'`
echo "${n} ${field1} ${field2} ${field3}"
done < temp
Or
或者
while read l
do
n=`echo "${l}" | awk -v 'FS=;' '{print NF}'`
field1=`echo "${l}" | awk -v 'FS=;' '{print }'`
field2=`echo "${l}" | awk -v 'FS=;' '{print }'`
field3=`echo "${l}" | awk -v 'FS=;' '{print }'`
echo "${n} ${field1} ${field2} ${field3}"
done < temp
Or
或者
while read l
do
n=`echo "${l}" | awk 'BEGIN{FS=";"}{print NF}'`
field1=`echo "${l}" | awk 'BEGIN{FS=";"}{print }'`
field2=`echo "${l}" | awk 'BEGIN{FS=";"}{print }'`
field3=`echo "${l}" | awk 'BEGIN{FS=";"}{print }'`
echo "${n} ${field1} ${field2} ${field3}"
done < temp
Try other awks like mawk
or nawk
as well.
尝试其他 awk 之类的mawk
或者也nawk
一样。