如何使用awk不处理第一行?

huangapple go评论60阅读模式
英文:

how not process the first line using awk?

问题

我想将 ps 虚拟内存大小的输出从 KiB 更改为 Mib 并添加符号,但不想更改标题(第一行),也不想从结果中删除它。

示例

PID COMMAND            VSZ
    9 bash              6304
  537 ps                7476

PID COMMAND            VSZ
    9 bash              6MB
  537 ps                7MB
英文:

I want to change the ps virtual memory size output from KiB to Mib and add sign to it, but neither want to change the header(first line),nor remove it from the result.

example
from

PID COMMAND            VSZ
    9 bash              6304
  537 ps                7476

to

PID COMMAND            VSZ
    9 bash              6MB
  537 ps                7MB

答案1

得分: 1

  1. 默认的隐式打印只会在没有提供操作时发生。
  2. (...) 不同于 {...}
NR!=1 ($2=$2"MB")
  • 整行被视为单一模式。
  • 连接$2"MB",并赋值给$2
  • 连接1和圆括号中表达式的结果(即$2的值)。
  • 测试是否NR不等于那个值。

由于NR始终是一个数字而"MB"不是,因此测试将始终成功(即模式匹配)。

没有提供操作,因此将执行默认的打印操作。

{ if(NR==1){ print } else {$2=$2"mb"} }
  • 整行被视为单一操作。
  • 没有提供模式,因此此操作对每条记录执行。
  • 如果NR等于1,则明确打印记录。
  • 否则,更新$2的值 - 没有默认打印。

可能您想要的是:

NR!=1 { $2 = int($2/1024) "MB" }
      { print }

或等效地:

NR!=1 { $2 = int($2/1024) "MB" } 1
英文:
  1. the default implicit print only happens if no action is provided
  2. (...) is not the same as {...}
NR!=1 ($2=$2"MB")
  • whole line is treated as a single pattern
  • concatenate $2 and "MB" and assign to $2
  • concatenate 1 and result of parenthesised expression (ie. value of $2)
  • test if NR is not equal to that

As NR is always a number and "MB" is not, the test will always succeed. (ie. the pattern matches)

No action has been provided, so the default print is done.

{ if(NR==1){ print } else {$2=$2"mb"} }
  • whole line is treated as single action
  • no pattern provided, so this action is performed for every record
  • record is (explicitly) printed if NR equals 1
  • otherwise value of $2 is updated - there is no default print

Probably you want something like:

NR!=1 { $2 = int($2/1024) "MB" }
      { print }

or equivalently:

NR!=1 { $2 = int($2/1024) "MB" } 1

答案2

得分: 0

我尝试并找到了答案:ps -o pid,vsz| awk 'NR ==1 {print} NR>1 ($2=int($2/1024)"MB") '
我不知道为什么这个有效。

英文:

I try out and found the answer ps -o pid,vsz| awk 'NR ==1 {print} NR>1 ($2=int($2/1024)"MB") '

and I have no idea why this work.

答案3

得分: 0

以下是翻译好的部分:

input00='PID COMMAND            VSZ
        9 bash              6304
      537 ps                7476'

printf '%s\n' "$input00" | gcat -b
printf '%s\n' "$input00" | 

>     mawk 'NR==!_ ||        sub(__  = "[0-9]+[[:space:]]*$", 
>                     int(substr(___ = $+_,
>                          match(___, __), RLENGTH) / 4^5) "MB")^_' FS='\n' | gcat -b
    
     
         1	PID COMMAND            VSZ
         2	    9 bash              6304
         3	  537 ps                7476 
>    
     
         1	PID COMMAND            VSZ
         2	    9 bash              6MB
         3	  537 ps                7MB
     
This approach preserves the random combination of spaces and tabs from the input, other than trailing ones.

*this performs mere integer truncation instead of proper rounding. adjust accordingly if needed.*

but if you don't care to preserve all the spaces and tabs, then it's simply :

>     mawk 'NR==1 ? NF=NF : $NF = int($NF/4^5) "MB"'

     1	PID COMMAND VSZ
     2	9 bash 6MB
     3	537 ps 7MB

请注意,已经保留了代码部分,没有进行翻译。

英文:
input00='PID COMMAND            VSZ
    9 bash              6304
  537 ps                7476'

printf '%s\n' "$input00" | gcat -b
printf '%s\n' "$input00" | 

> mawk 'NR==!_ || sub(__ = "[0-9]+[[:space:]]*$",
> int(substr(___ = $+,
> match(
_, __), RLENGTH) / 4^5) "MB")^' FS='\n' | gcat -b

     1	PID COMMAND            VSZ
     2	    9 bash              6304
     3	  537 ps                7476 

>

     1	PID COMMAND            VSZ
     2	    9 bash              6MB
     3	  537 ps                7MB

This approach preserves the random combination of spaces and tabs from the input, other than trailing ones.

this performs mere integer truncation instead of proper rounding. adjust accordingly if needed.

but if you don't care to preserve all the spaces and tabs, then it's simply :

> mawk 'NR==1 ? NF=NF : $NF = int($NF/4^5) "MB"'

 1	PID COMMAND VSZ
 2	9 bash 6MB
 3	537 ps 7MB

huangapple
  • 本文由 发表于 2023年2月19日 13:02:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75498111.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定