How to select the nth char of a string for each row in a file?

advertisements

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:

and 3
for 3
map 2
wrestle 1

draw

it has to start like this

cat char.txt | ...

and I'm only allowed to use sed (no awk, perl, ...).

I know how to select all the numbers

sed 's/\(.*\) \(.*\)/\2/g'

or the text

sed 's/\(.*\) \(.*\)/\1/g'

and I was thinking about

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


while read w n; do echo -n ${w:(($n-1)):1}; done < filename

Output:

draw


${parameter:offset:length}:
   Substring Expansion. Expands to up to length characters of parameter
   starting at the character specified by offset.