我有一个名为foo.txt的文件,包含两行:
foo bar
hello world
我正在尝试使用以下代码输出第二行:
cut -d$'\n' -f2 foo.txt
但它会打印出两行内容。cut 命令能够接受换行符作为分隔符吗?
cut
命令作用于输入的所有行,因此我认为换行符不能作为分隔符。
引自 POSIX.1-2017 的 cut(1p)
:
NAME
cut - cut out selected fields of each line of a file
SYNOPSIS
cut -b list [-n] [file...]
cut -c list [file...]
cut -f list [-d delim] [-s] [file...]
DESCRIPTION
The cut utility shall cut out bytes (-b option), characters (-c option), or
character-delimited fields ( -f option) from each line in one or more
files, concatenate them, and write them to standard output.
例子:
$ printf 'foo bar1\nfoo bar2\n'
foo bar1
foo bar2
$ printf 'foo bar1\nfoo bar2\n' | cut -f 2 -d ' '
bar1
bar2
要仅打印输入的第二行,可以使用sed
:
$ printf 'foo bar1\nfoo bar2\n'
foo bar1
foo bar2
$ printf 'foo bar1\nfoo bar2\n' | sed '2p;d'
foo bar2
不太清楚你想要什么,但这是在空行上分割并使用awk
折叠其他内容:
awk 'BEGIN {RS=""} {gsub("\n",""); print}'
这可能不是非常优雅的解决方案,但我经常只使用head
和tail
,因为它们通常在速度上优于sed
或awk
#!/bin/sh
input='test0\ntest1\ntest2\ntest3\ntest4\ntest5'
echo -e "$input" | head --lines 3
# test0
# test1
# test2
echo -e "$input" | tail --lines 3
# test3
# test4
# test5
get_index() {
echo -e "$1" |
head --lines "$(( $2 + 1 ))" |
tail --lines "1"
}
get_index "$input" "0"
# test0
get_index "$input" "4"
# test4
$'\n'
是有效的 bash 语法。你也可以使用nl=$(printf '\n:'); nl=${nl%:}
。我的示例也会被剥离新行,因为它是一条注释:$ cat foo.txt; foo bar hello world $ cut -d$'\n' -f2 foo.txt; hello world
你必须相信我,如果 cut 按照这种方式工作,新行就在你期望的位置。 - dannl=$(printf '\n:'); nl=${nl%:}; cut -d "$nl" -f 2'
,并确认你的shell是bash,用echo "$0"命令。我查看了POSIX(2018)标准,但是cut规范在我看来并不是100%清晰。它使用术语“字段”。我能找到的最接近涉及此问题的是:“没有字段分隔符的行将被保持原样,除非指定了-s选项。”。 - dan