简体   繁体   English

output 字段分隔符不一致

[英]Inconsistency in output field separator

We have to find the difference(d) Between last 2 nos and display rows with the highest value of d in ascending order我们必须找到最后 2 个编号之间的差异 (d),并按升序显示 d 值最高的行
INPUT输入

1 | Latha | Third | Vikas | 90 | 91  
2 | Neethu | Second | Meridian | 92 | 94  
3 | Sethu | First | DAV | 86 | 98  
4 | Theekshana | Second | DAV | 97 | 100  
5 | Teju | First | Sangamithra | 89 | 100  
6 | Theekshitha | Second | Sangamithra | 99 |100 

Required OUTPUT需要 OUTPUT

4$Theekshana$Second$DAV$97$100$3  
5$Teju$First$Sangamithra$89$100$11  
3$Sethu$First$DAV$86$98$12 
awk 'BEGIN{FS="|";OFS="$";}{
    avg=sqrt(($5-$6)^2)
    print $1,$2,$3,$4,$5,$6,avg
}'|sort -nk7 -t "$"| tail -3

Output: Output:

4 $ Theekshana $ Second $ DAV $ 97 $ 100$3  
5 $ Teju $ First $ Sangamithra $ 89 $ 100$11  
3 $ Sethu $ First $ DAV $ 86 $ 98$12  

As you can see there is space before and after $ sign but for the last column ( avg ) there is no space, please explain why its happening如您所见, $符号前后有空格,但最后一列( avg )没有空格,请解释为什么会这样

2) 2)

awk 'BEGIN{FS=" | ";OFS="$";}{
    avg=sqrt(($5-$6)^2)
    print $1,$2,$3,$4,$5,$6,avg
}'|sort -nk7 -t "$"| tail -3

OUTPUT OUTPUT

4$|$Theekshana$|$Second$|$0  
5$|$Teju$|$First$|$0  
6$|$Theekshitha$|$Second$|$0  

I have not mentiond |我没有提到| as the output field separator but still it appears, why is this happening and the difference is zero too作为 output 字段分隔符,但仍然出现,为什么会发生这种情况,差异也为零

I am just 6 days old in unix,please answer even if its easy我在 unix 才 6 天大,即使很容易也请回答

your field separator is only the pipe symbol, so surrounding whitespace is part of the field definitions and that's what you see in the output.您的字段分隔符只是 pipe 符号,因此周围的空格是字段定义的一部分,这就是您在 output 中看到的内容。 In combined uses pipe has the regex special meaning and need to be escaped.组合使用 pipe 具有正则表达式特殊含义,需要转义。 In your second case it means space or space is the field separator.在第二种情况下,这意味着空格或空格是字段分隔符。

$ awk 'BEGIN {FS=" *\\| *"; OFS="$"} 
             {d=sqrt(($NF-$(NF-1))^2); $1=$1; 
              print d "\t" $0,d}' file | sort -n | tail -3 | cut -f2-

4$Theekshana$Second$DAV$97$100$3
5$Teju$First$Sangamithra$89$100$11
3$Sethu$First$DAV$86$98$12

a slight rewrite will eliminate the number of fields dependency and fixes the format.轻微的重写将消除字段依赖的数量并修复格式。

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

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