简体   繁体   English

awk中的多个字段分隔符

[英]multiple Field Separators in awk

i have this string 我有这个字符串

-foo {{0.000 0.000} {648.0 0.000} {648.0 1980.0} {0.000 1980.0} {0.000 0.000}}

i want to separate it to numbers and iterate over them ,thanks tried to use Field separator without success how can i do it with awk? 我想将它分成数字并迭代它们,谢谢尝试使用字段分隔符,但没有成功我怎么能用awk做到这一点?

Try doing this : 试着这样做:

awk -F'}+|{+| ' '{for (i=1; i<=NF; i++) if ($i ~ "[0-9]") print $i}' file.txt

The Field Separator FS (the -F switch) can be a character, a word, a regex or a class of characters. 字段分隔符FS-F开关)可以是字符,单词,正则表达式或一类字符。

You can use this too : 您也可以使用它:

awk 'BEGIN{FS="}+|{+| "} {for(i=1;i<=NF;i++) if($i ~ "[0-9]")print $i}' file.txt

explanations 说明

  • foo|bar|base is a regex where it can match any of the strings separated by the | foo|bar|base是一个正则表达式,它可以匹配由|分隔的任何字符串
  • in }+|{+| in }+|{+| , we have the choice to match a literal } at least one : + , or a literal { at least one : + , or a space. ,我们可以选择匹配文字}至少一个: + ,或文字{至少一个: + ,或空格。
  • you can use a class of character too to do the same : [{} ] , both works 你也可以使用一类角色来做同样的事情: [{} ] ,两者都有效

One way with awk : awk的一种方法:

awk -F'[{} ]' '{ for( i=1; i<=NF; i++ ) if( $i ~ /[0-9.]+/ ) print $i }' file

In the line above, we went through those numbers, but I didn't do anything special, just printed them. 在上面的行中,我们浏览了这些数字,但我没有做任何特别的事情,只是打印出来。 You could add your logic to that part. 您可以将逻辑添加到该部分。

Output: 输出:

0.000
0.000
648.0
0.000
648.0
1980.0
0.000
1980.0
0.000
0.000

If you just want to display each number on a new line then simply use grep : 如果您只想在新行上显示每个数字,那么只需使用grep

$ egrep -o '[0-9]+\.[0-9]+' file
0.000
0.000
648.0
0.000
648.0
1980.0
0.000
1980.0
0.000
0.000

Admittedly, I am being very simple-minded in my offering. 不可否认,我的提议非常简单。 In my experience, the regex examples for the field separator are the most valuable to learn, especially if you have to deal with XML, etc. But in this case, we must remember that UNIX gives you many alternatives when confronted with characters that are irrelevant. 根据我的经验,字段分隔符的正则表达式示例是最有价值的,特别是如果您必须处理XML等。但在这种情况下,我们必须记住,当面对不相关的字符时,UNIX会为您提供许多选择。 A simple fix is to just remove the unwanted characters. 一个简单的解决方法就是删除不需要的字符。 There are various ways, but I would use tr -d '{}' like so: 有各种方法,但我会像这样使用tr -d '{}'

tr -d '{}' file.txt | awk '{ for( i=2; i<=NF; i++ ) print $i }'

Starting the loop counter i at 2 is just a quick way to skip the first argument ( -foo ) 启动循环计数器i在2只是跳过第一个参数(一个快速的方法-foo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM