[英]How can I use “~” as an AWK field separator?
I am trying to use the file separator as "~" but somehow it is not working properly, where as other delimiter such as ",", "|"我正在尝试将文件分隔符用作“〜”,但不知何故它无法正常工作,而其他分隔符如“,”,“|” are working as expected.正在按预期工作。 I mean there is no error but I am not getting desired output.我的意思是没有错误,但我没有得到想要的 output。
I am trying with the same data in "example.txt" file I have added 2 rows which are delimitd by ~, and then changed it to |.我正在尝试在“example.txt”文件中使用相同的数据,我添加了 2 行由 ~ 分隔,然后将其更改为 |。
Also main intention is to add the splitted name at the end of the record (interedsted only in newmodifiedline) rest i am just trying to test the values.主要目的是在记录末尾添加拆分名称(仅在 newmodifiedline 中感兴趣)rest 我只是想测试这些值。
awk code: awk 代码:
awk 'BEGIN { FS = "~" } ; {line=$0}; {fullname=$1}; {split(fullname, a, " ")}; {arrysize=length(a)}; {newmodifiedline=line"~"a[1]"~"a[2];}; {print fullname, arrysize, a[1], a[2], "-->",newmodifiedline}' example.txt
This works both with POSIX and GNU awk:这适用于 POSIX 和 GNU awk:
echo '1~2~3' | awk -v FS="~" '{print $2}'
echo '1~2~3'| awk 'BEGIN{FS="~"} {print$2}'
Both print 2
as expected.两者都按预期打印2
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.