字符串中的数字以在该字符串中查找字符 UNIX
问题描述:
我有一个包含单词和数字的字符串,如下所示:
I have a string that contains a word and a number, like this:
"6 ovenbread"
如何读取数字(我们称之为 i
)并找到单词的第 i
个字符?
How can I read the number (let's call it i
) and find the i
th character of the word?
似乎 s/([0-9]) (.){\1}/\2/
不起作用.
答
我觉得 awk 更简单,它有 substr
功能,可以让你选择第 n 个字母.
I think awk is easier, it has substr
function, allows you to pick the n-th letter.
awk '{print substr($2,$1,1)}'
使用您的数据:
kent$ echo "6 ovenbread"|awk '{print substr($2,$1,1)}'
r