不同长度的2个未排序文本文件如何可以是由侧显示侧(在列)在一个shell
给定one.txt
和two.txt
:
$ cat one.txt
apple
pear
longer line than the last two
last line
$ cat two.txt
The quick brown fox..
foo
bar
linux
skipped a line
Run Code Online (Sandbox Code Playgroud)
显示:
apple The quick brown fox..
pear foo
longer line than the last two bar
last line linux
skipped a line
Run Code Online (Sandbox Code Playgroud)
paste one.txt two.txt
几乎没有诀窍,但没有很好地对齐列,因为它只是在第1列和第2列之间打印一个选项卡.我知道如何使用emacs和vim,但希望输出显示为stdout用于管道等.
我提出的解决方案使用sdiff
然后管道sed删除输出sdiff
添加.
sdiff one.txt two.txt | sed -r 's/[<>|]//;s/(\t){3}//'
我可以创建一个函数并将其粘贴在我的.bashrc
但是肯定已经存在的命令(或者可能是更清洁的解决方案)?
sh
和之间有什么区别source
?
source: source filename [arguments]
Read and execute commands from FILENAME and return. The pathnames
in $PATH are used to find the directory containing FILENAME. If any
ARGUMENTS are supplied, they become the positional parameters when
FILENAME is executed.
Run Code Online (Sandbox Code Playgroud)
并为man sh
:
NAME
bash - GNU Bourne-Again SHell
SYNOPSIS
bash [options] [file]
COPYRIGHT
Bash is Copyright (C) 1989-2004 by the Free Software Foundation, Inc.
DESCRIPTION
Bash is an sh-compatible command language interpreter that executes commands read …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用tput
Bash脚本,并尽力避免随机错误喷出.为此,我写了以下这一行:
COLS="$(tput cols 2> /dev/null)"
Run Code Online (Sandbox Code Playgroud)
令我惊讶的是,当我运行它时,无论我的终端窗口的宽度是多少,COLS
都始终设置为80
.(为了演示,我的终端恰好是115列宽.)为了弄清楚发生了什么,我在命令行上尝试了一些事情:
$ tput cols
115
$ tput cols | cat
115
$ echo "$(tput cols)"
115
$ tput cols 2> /dev/null
115
$ echo "$(tput cols 2> /dev/null)"
80
Run Code Online (Sandbox Code Playgroud)
因此,tput
当它的stderr被重定向时,或者当它被嵌入到进程替换中时,似乎成功地找出终端特性,但不是两者都有.多奇怪啊!
我在Linux和OS X上测试了这个,行为是一样的.
这里发生了什么?而作为一个实际问题,tput
在抑制stderr喷射的同时,最好的工作方式是什么?
注意:我知道$COLUMNS
.我特别感兴趣的是使用tput
.
我试图从 Vim 中获取终端大小,但无论终端的实际大小如何,调用:!tput cols
总是返回。80
从终端运行命令会返回正确的值。
有没有办法从 Vim 中获取终端中的列数?为什么上面的命令会产生错误的值?我猜这与 Vim 运行外部命令的方式有关。
还有$COLUMNS
shell 变量,但看起来这些变量没有导出到子进程(vim),如下所述:脚本中丢失的行和列环境变量
我经常在vi中工作,暂停vi,在cli上运行一些东西,然后fg回到vi来处理结果.例如,修复我运行cli命令时出现的错误.
但是,当我fg vi时,vi"擦除"当前的终端缓冲区,我无法在回滚缓冲区中看到终端输出的"最后屏幕".
在vi(或屏幕,我使用屏幕)中有一些设置可以帮助我吗?
我已经搜索谷歌很长一段时间没有答案.我也意识到还有其他工作流可以解决这个问题,但它们并不完美(从内部运行vi意味着没有shell完成等).
我想设置core.pager以管理包装文本的长行.为了做到这一点,我使用这个命令:
$ GIT_PAGER="fold -sw $COLUMNS" git log
Run Code Online (Sandbox Code Playgroud)
我试图设置core.pager,但Bash过早评估$ COLUMNS:
$ git config core.pager "fold -sw $COLUMNS"
$ grep pager .git/config
pager = fold -sw 80
Run Code Online (Sandbox Code Playgroud)
如果我试图逃避$ COLUMNS,我得到:
$ git config core.pager "fold -sw \$COLUMNS"
$ grep pager .git/config
pager = fold -sw $COLUMNS
$ LC_ALL=en_US git log
fold: option requires an argument -- 'w'
Try 'fold --help' for more information.
Run Code Online (Sandbox Code Playgroud)
如果我尝试使用sh -c
我得到:
$ git config core.pager 'sh -c fold -sw $COLUMNS'
$ grep pager .git/config
pager = sh …
Run Code Online (Sandbox Code Playgroud) 假设我想在一堆日志文件中搜索"ERROR".
我想为包含"ERROR"的每个文件打印一行.
在每一行中,我想打印最左边的日志文件路径,而最右边的"ERROR"数.
我试过用:
printf "%-50s %d" $filePath $errorNumber
Run Code Online (Sandbox Code Playgroud)
...但它并不完美,因为黑色控制台可能会有很大差异,文件路径有时可能会很长.
只是为了眼睛的快乐,但我根本无法这样做.任何人都可以帮我解决这个问题吗?
bash ×3
linux ×3
shell ×3
unix ×2
vim ×2
command-line ×1
format ×1
git ×1
gnu-screen ×1
scroll ×1