AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题 / 412240
Accepted
pntshere
pntshere
Asked: 2017-12-22 03:25:35 +0800 CST2017-12-22 03:25:35 +0800 CST 2017-12-22 03:25:35 +0800 CST

显示控制台输出下面的 1 行或多行

  • 772

当我运行tail ~/SOMEFILE我得到的命令时,例如:

testenv@vps_1:~# tail ~/SOMEFILE
    This is the content of SOMEFILE.

testenv@vps_1:~#但是如果我想在:和输出之间有一个回车怎么办:This is the content of SOMEFILE.

所以最终的结果是这样的:

testenv@vps_1:~# tail ~/SOMEFILE

    This is the content of SOMEFILE.

或这个:

testenv@vps_1:~# tail ~/SOMEFILE


    This is the content of SOMEFILE.

或这个:

testenv@vps_1:~# tail ~/SOMEFILE



    This is the content of SOMEFILE.

注意:第一个示例显示两部分之间的一行间距,第二个示例显示两行,第三个示例显示三行。

有没有办法确保tail像我在示例中所示的那样,在 Bash 中,仅针对这个特定命令(当然不是所有命令),该问题的输出(或任何其他输出)会被间隔开?

bash command-line
  • 3 3 个回答
  • 967 Views

3 个回答

  • Voted
  1. Kevin Lemaire
    2017-12-22T03:39:51+08:002017-12-22T03:39:51+08:00

    tail没有任何论据来管理这个。

    作为一种解决方法,您可以做的是在运行 tail 命令之前打印一个空行。

    echo && tail ~/SOMEFILE
    

    对于多行: yes也可以使用命令。是的,这里建议的手册页: bash: print x number of blank lines

    yes '' | sed 5q && tail ~/SOMEFILE
    

    将 5 替换为所需的空行数。

    注意:您可能还想看看编辑终端提示。但随后它将是终端范围的,并且不仅与您的特定命令相关联。

    • 3
  2. Praveen Kumar BS
    2017-12-22T07:07:07+08:002017-12-22T07:07:07+08:00

    对于单个空行

    sed '{x;1p;x;}' filename | tail
    

    对于开头的 5 个空行

    sed '{x;1p;1p;1p;1p;1p;x;}' filename | tail
    
    • 1
  3. Best Answer
    nxnev
    2017-12-22T08:02:10+08:002017-12-22T08:02:10+08:00

    最简单的选择是手动打印那些额外的换行符,例如:

    printf '\n\n\n'; tail ~/SOMEFILE
    

    但如果你想:

    • 这样做只是为了tail
    • 不要在每次tail调用时编写额外的命令
    • 简单而全面地控制换行符的数量

    那么我建议你在你的 aliases/rc 文件中添加一个函数。

    例如:

    # Bash version
    
    # In Bash we can override commands with functions
    # thanks to the `command` builtin
    tail() {
    
      # `local` limit the scope of variables,
      # so we don't accidentally override global variables (if any).
      local i lines
    
      # `lines` gets the value of the first positional parameter.
      lines="$1"
    
      # A C-like iterator to print newlines.
      for ((i=1; i<=lines; i++)); do
        printf '\n'
      done
    
      # - `command` is a bash builtin, we can use it to run a command.
      #   whose name is the same as our function, so we don't trigger
      #   a fork bomb: <https://en.wikipedia.org/wiki/Fork_bomb>
      #
      # - "${@:2}" is to get the rest of the positional parameters.
      #   If you want, you can rewrite this as:
      #
      #       # `shift` literally shifts the positional parameters
      #       shift
      #       command "${@}"
      #
      #   to avoid using "${@:2}"
      command tail "${@:2}"
    
    }
    
    #===============================================================================
    
    # POSIX version
    
    # POSIX standard does not demand the `command` builtin,
    # so we cannot override `tail`.
    new_tail() {
    
      # `lines` gets the value of the first positional parameter.
      lines="$1"
    
      # `i=1`, `[ "$i" -le "$lines" ]` and `i=$((i + 1))` are the POSIX-compliant
      # equivalents to our C-like iterator in Bash
      i=1
      while [ "$i" -le "$lines" ]; do
        printf '\n'
        i=$((i + 1))
      done
    
      # Basically the same as Bash version
      shift
      tail "${@}"
    
    }
    

    因此,您可以将其称为:

    tail 3 ~/SOMEFILE
    
    • 1

相关问题

  • 没有服务器的命令行 pub/sub?

  • 从文本文件传递变量的奇怪问题

  • 虽然行读取保持转义空间?

  • `tee` 和 `bash` 进程替换顺序

  • 运行一个非常慢的脚本直到它成功

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    JSON数组使用jq来bash变量

    • 4 个回答
  • Marko Smith

    日期可以为 GMT 时区格式化当前时间吗?[复制]

    • 2 个回答
  • Marko Smith

    bash + 通过 bash 脚本从文件中读取变量和值

    • 4 个回答
  • Marko Smith

    如何复制目录并在同一命令中重命名它?

    • 4 个回答
  • Marko Smith

    ssh 连接。X11 连接因身份验证错误而被拒绝

    • 3 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Marko Smith

    systemctl 命令在 RHEL 6 中不起作用

    • 3 个回答
  • Marko Smith

    rsync 端口 22 和 873 使用

    • 2 个回答
  • Marko Smith

    以 100% 的利用率捕捉 /dev/loop -- 没有可用空间

    • 1 个回答
  • Marko Smith

    jq 打印子对象中所有的键和值

    • 2 个回答
  • Martin Hope
    EHerman JSON数组使用jq来bash变量 2017-12-31 14:50:58 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST
  • Martin Hope
    Drux 日期可以为 GMT 时区格式化当前时间吗?[复制] 2017-12-26 11:35:07 +0800 CST
  • Martin Hope
    AllisonC 如何复制目录并在同一命令中重命名它? 2017-12-22 05:28:06 +0800 CST
  • Martin Hope
    Steve “root”用户的文件权限如何工作? 2017-12-22 02:46:01 +0800 CST
  • Martin Hope
    Bagas Sanjaya 为什么 Linux 使用 LF 作为换行符? 2017-12-20 05:48:21 +0800 CST
  • Martin Hope
    Cbhihe 将默认编辑器更改为 vim for _ sudo systemctl edit [unit-file] _ 2017-12-03 10:11:38 +0800 CST
  • Martin Hope
    showkey 如何下载软件包而不是使用 apt-get 命令安装它? 2017-12-03 02:15:02 +0800 CST
  • Martin Hope
    youxiao 为什么目录 /home、/usr、/var 等都具有相同的 inode 编号 (2)? 2017-12-02 05:33:41 +0800 CST
  • Martin Hope
    user223600 gpg —list-keys 命令在将私钥导入全新安装后输出 uid [未知] 2017-11-26 18:26:02 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve