Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String bash中字符串的最后n个单词_String_Bash - Fatal编程技术网

String bash中字符串的最后n个单词

String bash中字符串的最后n个单词,string,bash,String,Bash,我有一个包含许多(总数不同)单词的字符串,我需要得到最后10个单词。我该怎么做?我在看awk、grep和cut,但什么都没想到 举个例子(尽管我觉得这个问题很清楚): 我要这个字符串的最后10个字 同样,初始字符串中的单词总数没有定义。当您试图在最后查找单词或字符时,最好在正则表达式中使用行尾锚点$ $ echo "aaa bda fdkfj fds fsd bar dsad dsas dsad zrthd shshh" | grep -o '[^[:space:]]\+\([[:space:]

我有一个包含许多(总数不同)单词的字符串,我需要得到最后10个单词。我该怎么做?我在看awk、grep和cut,但什么都没想到

举个例子(尽管我觉得这个问题很清楚):

我要这个字符串的最后10个字


同样,初始字符串中的单词总数没有定义。

当您试图在最后查找单词或字符时,最好在正则表达式中使用行尾锚点
$

$ echo "aaa bda fdkfj fds fsd bar dsad dsas dsad zrthd shshh" | grep -o '[^[:space:]]\+\([[:space:]]\+[^[:space:]]\+\)\{9\} *$'
bda fdkfj fds fsd bar dsad dsas dsad zrthd shshh
您也可以在sed中使用相同的正则表达式

$ echo "aaa bda fdkfj fds fsd bar dsad dsas dsad zrthd shshh" | grep -oP '\S+(?:\s+\S+){9} *$'
bda fdkfj fds fsd bar dsad dsas dsad zrthd shshh

当您试图在最后查找单词或字符时,最好在正则表达式中使用行尾锚点
$

$ echo "aaa bda fdkfj fds fsd bar dsad dsas dsad zrthd shshh" | grep -o '[^[:space:]]\+\([[:space:]]\+[^[:space:]]\+\)\{9\} *$'
bda fdkfj fds fsd bar dsad dsas dsad zrthd shshh
您也可以在sed中使用相同的正则表达式

$ echo "aaa bda fdkfj fds fsd bar dsad dsas dsad zrthd shshh" | grep -oP '\S+(?:\s+\S+){9} *$'
bda fdkfj fds fsd bar dsad dsas dsad zrthd shshh

awk
中,内置变量NF设置为每行的字段数(默认为单词)。因此,您可以:

echo "${STRING}" | awk '{
    for (i = NF - 9; i <= NF; i++) {printf "%s ", $i}
    printf "\n"
}'
echo“${STRING}”|awk'{

对于
awk
中的(i=NF-9;i),内置变量NF设置为每行上的字段数(默认为单词)。因此您可以:

echo "${STRING}" | awk '{
    for (i = NF - 9; i <= NF; i++) {printf "%s ", $i}
    printf "\n"
}'
echo“${STRING}”|awk'{

对于(i=NF-9;i只需玩
tr
tail
xargs

$ echo "1 2 3 4 5 6 7 8 9 10" | tr ' ' '\n' | tail -5 | xargs -n5
6 7 8 9 10
这将在每行打印一个单词,以便
tail
获得所需数量的单词。然后,
xargs
将它们“重新合并”到同一行中

您还可以在反转文本后将awk的
NF
设置为所需的值:

$ echo "1 2 3 4 5 6 7 8 9 10" | rev | awk '{NF=5}1' | rev
6 7 8 9 10

只需玩
tr
tail
xargs

$ echo "1 2 3 4 5 6 7 8 9 10" | tr ' ' '\n' | tail -5 | xargs -n5
6 7 8 9 10
这将在每行打印一个单词,以便
tail
获得所需数量的单词。然后,
xargs
将它们“重新合并”到同一行中

您还可以在反转文本后将awk的
NF
设置为所需的值:

$ echo "1 2 3 4 5 6 7 8 9 10" | rev | awk '{NF=5}1' | rev
6 7 8 9 10

你想要shell吗?这是纯shell。不
awk
,不
cut
,不
sed
,不
perl
。你卖的最多。(好吧,我用的是
wc
,它是一个实用工具,不是Bash shell的一部分,但其他一切都是Bash的一部分)


你想要shell吗?这是纯shell。不
awk
,不
cut
,不
sed
,不
perl
。你卖的最多。(好吧,我用的是
wc
,它是一个实用工具,不是Bash shell的一部分,但其他一切都是Bash的一部分)


规范的、纯粹的Bash方法是使用
read

string='one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen forty two'
read -r -d '' -a array < <(printf '%s\0' "$string")
# Print only ten last words:
printf '%s\n' "${array[*]: -10}"
<代码>字符串=“234567891011一121314151617181940二”
read-r-d''-数组<执行此操作的规范、纯Bash方法是使用
read

string='one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen forty two'
read -r -d '' -a array < <(printf '%s\0' "$string")
# Print only ten last words:
printf '%s\n' "${array[*]: -10}"
<代码>字符串=“234567891011一121314151617181940二” 读取-r-d''-a数组<您的字符串:

string="Lorem ipsum dolor sit amet"
最后四个字使用纯Bash/Shell一行程序:

echo ${string/${string% * * * *} /}
重复或删除
*
以获取更多或更少的单词

解释
我们使用
${parameter/pattern/string}
将x个单词替换为零。当模式
${str%**}
返回最后4个单词前面的所有内容时,它将从字符串中删除前导的
Lorem

您的字符串:

string="Lorem ipsum dolor sit amet"
最后四个字使用纯Bash/Shell一行程序:

echo ${string/${string% * * * *} /}
重复或删除
*
以获取更多或更少的单词

解释

我们使用
${parameter/pattern/string}
将x字替换为零,并作为模式
${str%**}
返回最后4个单词前面的所有内容,它会从字符串中删除前导的
Lorem

一个示例会更好。先反转然后取10?一个示例会更好。先反转然后取10?这取决于路径名扩展!
FOO='*one-two[abc]*“
。不太好。你真的不需要将
wc
$#
扩展到位置参数集的数量。@gniourf\u gniourf啊!这就是我缺少的。你说得对。我会更新我的答案。这需要进行路径名扩展!
FOO='*1-2[abc]*“
。不太好。你真的不需要
wc
$#
扩展到位置参数集的数量。@gniourf\u gniourf啊!这就是我缺少的。你说得对。我会更新我的答案。