每行都有一个单词和一个数字.我需要以某种方式选择第n个字母,它们将共同构成一个新单词.例如:
and 3
for 3
map 2
wrestle 1
draw
它必须像这样开始
cat char.txt | ...
并且只允许我使用sed(没有awk,perl等).
我知道如何选择所有数字
sed 's/\(.*\) \(.*\)/\2/g'
或文字
sed 's/\(.*\) \(.*\)/\1/g'
我在想
cat char.txt | head -c $(sed 's/\(.*\) \(.*\)/\2/g') | tail -c 1 | sed 's/\n\//g'
但是它不起作用,因为它不会迭代所有行,并且由于某种原因甚至无法在一行上运行.
需要一些帮助和指导
泰
解决方法:
while read w n; do echo -n ${w:(($n-1)):1}; done < filename
输出:
draw
${parameter:offset:length}:
Substring Expansion. Expands to up to length characters of parameter
starting at the character specified by offset.
标签:bash,sed,linux
来源: https://codeday.me/bug/20191029/1958093.html