【问题标题】:Add more new lines to end of line in stdout在标准输出的行尾添加更多新行
【发布时间】:2021-07-22 20:07:54
【问题描述】:

我希望能够用额外的新行打印出我的日志。

这就是日志的样子

2021/04/29 12:30:19 [error] 30098#30098: *443497 FastCGI sent in stderr: "PHP message: PHP Warning:  Use of undefined constant last_login - assumed 'last_login' (this will throw an Error in a future version of PHP) in /var/www/html/app/addons/my_changes/func.php on line 9PHP message: PHP Notice:  Array to string conversion in /var/www/html/app/addons/my_changes/func.php on line 9" while reading response header from upstream, client: 172.70.34.13, server: www.example.com, request: "GET /Total-9-Abrasive-Grinding-Disc-TAC2232301 HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.3-fpm.sock:", host: "www.example.com"
2021/04/29 12:30:24 [error] 30098#30098: *443501 FastCGI sent in stderr: "PHP message: PHP Warning:  Use of undefined constant last_login - assumed 'last_login' (this will throw an Error in a future version of PHP) in /var/www/html/app/addons/my_changes/func.php on line 9PHP message: PHP Notice:  Array to string conversion in /var/www/html/app/addons/my_changes/func.php on line 9" while reading response header from upstream, client: 172.70.34.73, server: www.example.com, request: "GET /Kitchen-and-Dinning/Tableware/Glass/Luminarc-Tumbler-6pcs-Scotland-33cl-N0763?sort=rating&order=DESC&limit=100 HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.3-fpm.sock:", host: "www.example.com"
2021/04/29 12:30:25 [error] 30098#30098: *443503 FastCGI sent in stderr: "PHP message: PHP Warning:  Use of undefined constant last_login - assumed 'last_login' (this will throw an Error in a future version of PHP) in /var/www/html/app/addons/my_changes/func.php on line 9PHP message: PHP Notice:  Array to string conversion in /var/www/html/app/addons/my_changes/func.php on line 9" while reading response header from upstream, client: 141.101.105.129, server: www.example.com, request: "GET /index.php?dispatch=product_features.add_product&product_id=9534&redirect_url=index.php%3Ffeatures_hash=1-877%26sort_by=popularity%26sort_order=desc%26layout=short_list%26dispatch=categories.view%26category_id=30%26page=15 HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.3-fpm.sock:", host: "www.example.com"

它们在终端中看起来很杂乱,虽然有换行符,但我想再添加 1 个换行符以使其更具可读性。

我尝试过使用

tail -n 10 /var/log/nginx/example.com_error.log | sed 's/\n/\n\n/g'

tail -n 10 /var/log/nginx/example.com_error.log | tr '\n' '\n\n'

无济于事。

【问题讨论】:

    标签: linux bash command-line terminal


    【解决方案1】:
    $ printf 'foo\nbar\n' | pr -td
    foo
    
    bar
    
    $
    

    不完全确定可移植性,但开放组规范 (https://pubs.opengroup.org/onlinepubs/9699919799/utilities/pr.html#tag_20_93_01) 状态:

    -d
    Produce output that is double-spaced; append an extra <newline> following every <newline> found in the input.
    

    -t
    Write neither the five-line identifying header nor the five-line trailer usually supplied for each page. Quit writing after the last line of each file without spacing to the end of the page
    

    所以这很标准。

    【讨论】:

      【解决方案2】:

      $ 表示sed 中的行尾。

      $: cat file
      12312312
      123123123
      123123123
      123123123
      
      $: sed 's/$/\n/' file
      12312312
      
      123123123
      
      123123123
      
      123123123
      

      【讨论】:

      • 不适用于 Mac OS Catalina。输出每一行,如12312312n,后面有一个换行符。
      • $: awk '{print $0"\n"}' file 可能在做类似的事情?
      • 你可以尝试这样的怪事:$: tr "\012" @&lt;file|sed 's/@/@@/g'|tr @ "\012"
      猜你喜欢
      • 2020-01-03
      • 1970-01-01
      • 2021-05-05
      • 2012-04-30
      • 1970-01-01
      • 2012-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多