簡體   English   中英

遍歷awk中的行並追加字符串

[英]Iterate through rows in awk and append string

請告訴我如何遍歷ps axo %mem,pid,euser,cmd | sort -nr | head -n 10輸出中找到的每一行ps axo %mem,pid,euser,cmd | sort -nr | head -n 10 ps axo %mem,pid,euser,cmd | sort -nr | head -n 10 ps axo %mem,pid,euser,cmd | sort -nr | head -n 10以便新輸出在每行末尾包含字符串“ new_line”?

我嘗試了以下操作,但是根據行中的內容量,它並不總是輸出所有數據:

ps axo %mem,pid,euser,cmd | sort -nr | head -n 10 | awk '{print $1"|"$2"|"$3"|"$4"|"$5" new_line "}'

電流輸出:

15.9|1077|git|sidekiq|2.7.5 new_line 
14.0|878|git|unicorn_rails|master new_line 
13.3|1090|git|unicorn_rails|worker[1] new_line 
13.3|1088|git|unicorn_rails|worker[0] new_line 
3.9|716|mysql|/usr/sbin/mysqld| new_line 
1.2|3412|max|-bash| new_line 
1.1|919|root|/usr/bin/python|/usr/bin/fail2ban-server new_line 
1.0|746|www-data|php-fpm:|pool new_line 
1.0|745|www-data|php-fpm:|pool new_line 
1.0|744|www-data|php-fpm:|pool new_line 

預期的輸出(而不是空格,而是一個“ |”):

15.8  1077 git      sidekiq 2.7.5 gitlab [0 of 25 busy] new_line                                                                                                                                                              
14.0   878 git      unicorn_rails master -c /home/git/gitlab/config/unicorn.rb -E production new_line                                            
13.3  1090 git      unicorn_rails worker[1] -c /home/git/gitlab/config/unicorn.rb -E production new_line                                         
13.3  1088 git      unicorn_rails worker[0] -c /home/git/gitlab/config/unicorn.rb -E production new_line
 3.9   716 mysql    /usr/sbin/mysqld new_line
 1.2  3412 max      -bash new_line
 1.1   919 root     /usr/bin/python /usr/bin/fail2ban-server -b -s /var/run/fail2ban/fail2ban.sock new_line
 1.0   746 www-data php-fpm: pool www new_line                                        
 1.0   745 www-data php-fpm: pool www new_line                                        
 1.0   744 www-data php-fpm: pool www new_line

謝謝,馬克斯。

$ ps axo %mem,pid,euser,cmd \
  | sort -nr \
  | awk -v OFS="|" '{$1=$1; print $0 " newline"} NR==10{exit}'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM