Every line has a word and a number. I need somehow to select the nth letter which all together will make a new word. For example:
JavaScript
x
and 3
for 3
map 2
wrestle 1
draw
it has to start like this
JavaScript
cat char.txt |
and I’m only allowed to use sed (no awk, perl, …).
I know how to select all the numbers
JavaScript
sed 's/(.*) (.*)/2/g'
or the text
JavaScript
sed 's/(.*) (.*)/1/g'
and I was thinking about
JavaScript
cat char.txt | head -c $(sed 's/(.*) (.*)/2/g') | tail -c 1 | sed 's/n//g'
but it won’t work because it won’t iterate thought all the lines and for some reason it won’t even work on a single line.
Need some help and guidance pls
Ty
Advertisement
Answer
JavaScript
while read w n; do echo -n ${w:(($n-1)):1}; done < filename
Output:
JavaScript
draw
JavaScript
${parameter:offset:length}:
Substring Expansion. Expands to up to length characters of parameter
starting at the character specified by offset.