Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.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
Unix 在bourne shell中从字符串中提取n个单词_Unix_Sh - Fatal编程技术网

Unix 在bourne shell中从字符串中提取n个单词

Unix 在bourne shell中从字符串中提取n个单词,unix,sh,Unix,Sh,我想从字符串中提取特定数量的单词 例如:s1=“你好:我是这个论坛的新手” 如何从字符串中提取前4个单词“Hello:I am new”?您可以使用这个 #!/bin/bash s1="Hello: I am new to this forum" echo $s1 | cut -d' ' -f-4 -d'':将空间用于现场测力仪 -f-4:第4场及之前 人工切割 -d, --delimiter=DELIM use DELIM instead of TAB for fi

我想从字符串中提取特定数量的单词

例如:
s1=“你好:我是这个论坛的新手”

如何从字符串中提取前4个单词
“Hello:I am new”

您可以使用这个

#!/bin/bash
s1="Hello: I am new to this forum"
echo $s1 | cut -d' ' -f-4
-d'':将空间用于现场测力仪 -f-4:第4场及之前

人工切割

   -d, --delimiter=DELIM
          use DELIM instead of TAB for field delimiter

   -f, --fields=LIST
          select only these fields;  also print any line that contains no delimiter character, unless the -s option is specified
用awk溶液

#!/bin/bash
s1="Hello: I am new to this forum"
echo $s1 | awk  '{printf "%s %s %s %s\n", $1, $2, $3, $4}' 
或者只是回声

#!/bin/bash
s1="Hello: I am new to this forum"
echo ${s1/%\ to*/}
与sed

#!/bin/bash
s1="Hello: I am new to this forum"
echo $s1 | sed -E 's/(([^ ]+ ){4}).*/\1/'  

伯恩壳牌,还是POSIX壳牌?