粘贴命令不识别脚本中的换行符 (/n)
Paste Command Not Recognizing Linebreak (/n) In Script
我正在使用以下代码将 header "Both" 和一个空行添加到文件中。
sed -i '1i Both \n' file1
当我打开文件时,我可以看到换行符。
但是,当我使用以下命令粘贴文件时,它删除了 shell 中的换行符。
paste file1 file2 | column -s $'\t' -t | sed '1i\'
有人知道为什么粘贴无法识别它吗?
更具体地说,/n 字符被识别,但粘贴将删除同一行中的换行符。
正在输出什么:
Header1 Header2
abc def
ghi jkl
应该是什么:
Header1 Header2
abc def
ghi jkl
我知道它会删除换行符,因为当我只在一个文件前添加一个新行时,它看起来像这样:
Header1 Header2
def
abc jkl
ghi
作为临时解决方法,我使用 sed -i '1i Both \n----' file1
强制粘贴打印新行,因为它不为空:
Header1 Header2
---- ----
abc def
ghi jkl
而且,它会保留换行符,所以我想,当将两个文件粘贴在一起时,我如何才能一直保留换行符?
这里不需要sed
。观察:
# defining functions here to make this a standalone reproducer
# remove these and change <(file1) to just file1 to use files instead
file1() { printf '%s\n' Header1 abc ghi; }
file2() { printf '%s\n' Header2 def jkl; }
# use braces to create a code block, and redirect that whole block to your output file
{
printf '%s\n' Both '' # header, then blank line
paste <(file1) <(file2) | column -s $'\t' -t # body content
} >out.txt # redirection
...显然,如果您根本不将输出重定向到文件,则不需要重定向,也不需要块。这也更有效:不需要 column
的输出被 sed
.
读取然后写入
我正在使用以下代码将 header "Both" 和一个空行添加到文件中。
sed -i '1i Both \n' file1
当我打开文件时,我可以看到换行符。
但是,当我使用以下命令粘贴文件时,它删除了 shell 中的换行符。
paste file1 file2 | column -s $'\t' -t | sed '1i\'
有人知道为什么粘贴无法识别它吗?
更具体地说,/n 字符被识别,但粘贴将删除同一行中的换行符。
正在输出什么:
Header1 Header2
abc def
ghi jkl
应该是什么:
Header1 Header2
abc def
ghi jkl
我知道它会删除换行符,因为当我只在一个文件前添加一个新行时,它看起来像这样:
Header1 Header2
def
abc jkl
ghi
作为临时解决方法,我使用 sed -i '1i Both \n----' file1
强制粘贴打印新行,因为它不为空:
Header1 Header2
---- ----
abc def
ghi jkl
而且,它会保留换行符,所以我想,当将两个文件粘贴在一起时,我如何才能一直保留换行符?
这里不需要sed
。观察:
# defining functions here to make this a standalone reproducer
# remove these and change <(file1) to just file1 to use files instead
file1() { printf '%s\n' Header1 abc ghi; }
file2() { printf '%s\n' Header2 def jkl; }
# use braces to create a code block, and redirect that whole block to your output file
{
printf '%s\n' Both '' # header, then blank line
paste <(file1) <(file2) | column -s $'\t' -t # body content
} >out.txt # redirection
...显然,如果您根本不将输出重定向到文件,则不需要重定向,也不需要块。这也更有效:不需要 column
的输出被 sed
.