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壳牌?