在awk中的反斜杠("\")定界符上分割字符串?
我正在尝试基于某些定界符将字符串拆分到文件中.但是我无法正确实现它.这是下面的代码.
I am trying to split the string in a file based on some delimiter.But I am not able to achieve it correctly... Here is my code below.
awk 'var=split($2,arr,'\'); {print $var}' file1.dat
这是我的样本数据专家.
Here is my sample data guys.
Col1 Col2
abc 123\abc
abcd 123\abcd
期望输出:
Col1 Col2
abc abc
abcd abcd
样本数据和输出是我对您要求的最佳猜测
Sample data and output is my best guess at your requirement
echo '1:2\\a\\b:3' | awk -F: '{
n=split($2,arr,"\\")
# print "#dbg:n=" n
var=arr[3]
print var
}'
输出
b
回想一下split
返回它发现要拆分的字段数.您可以取消注释调试行,然后将看到返回值3.
Recall that split
returns the number of fields that it found to split. You can uncomment the debug line and you'll see the value 3 returned.
还请注意,对于我的测试,我必须使用2个'\'字符来处理1个字符.我认为您不需要在文件中使用它,但是如果这不适用于文件,请尝试根据需要在数据中添加额外的"\".我尝试了几种使用'\'的变体,这似乎是最简单的.欢迎其他人发表评论!
Note also that for my test, I had to use 2 '\' chars for 1 to be processed. I don't think you'll need that in a file, but if this doesn't work with a file, then try adding extra '\' as needed to your data. I tried several variations on how to use '\', and this seems the most straightforward. Others are welcome to comment!
我希望这会有所帮助.