无法分离分号分隔行AWK [英] Unable to separate semi-colon separated line awk
问题描述
我试图做到以下几点:结果
1.逐行读取一个文件行。结果
2.每行的结构如下:字段1,场2; FIELD3结果
3.使用 AWK
来各个区域分开,然后再处理这些领域的进一步
I am trying to do the following:
1. Read a file line by line.
2. Each line has the following structure: field1;field2;field3
3. Use awk
to separate each of these fields and then process each of these fields further
code的片断我是:
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 $1}'`
field2=`echo ${l} | awk --field-separator=";" '{print $2}'`
field3=`echo ${l} | awk --field-separator=";" '{print $3}'`
echo ${n} ${field1} ${field2} ${field3}
done < temp
如果温度只包含以下行:
Where temp contains only the following line:
xx;yy;zz
我得到的命令行上的答案是:
The answer I get on the command line is:
1 xx;yy;zz
我不知道我理解这个输出。任何的解释将是很好的,因为它对于其他文件。我工作在Mac上,而这code使用 AWK
A 庆典
脚本中
推荐答案
您的awk不知道是什么 - 现场分隔符=;
表示,所以当你做这样的:
Your awk has no idea what --field-separator=";"
means so when you do this:
awk --field-separator=";" '{print $1}'
您的awk依然采用了空间的默认FS,所以$ 1包含你的整个输入线,而$ 2和$ 3为空。使用 -F;
来设置FS
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.
您是方式,方法关在怎么写你想要的脚本标记。如果你告诉我们更多关于什么是过程中每场是,我们可以帮你。
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.
这篇关于无法分离分号分隔行AWK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!