I'm trying to replace all the numbers in a certain text with its full written word form.
For example, if the input is
this university has 9123 students
one of its departments has 2315 students
The output would be
this university has nine thousand one hundred and twenty three students
one of its departments has two thousand three hundred and fifteen students
I have a script called num2words.sh
that can convert a number like 9123 to its word form. I want to use this script to substitute all the numbers in a file called file1.txt
. I've been trying to use sed but I can't get it to work. So far I have this,
cat file1.txt | sed -e "s/\([0-9]\ \)/)/$(./num2words.sh \1)"
I guess the \1 is not being passed correctly. Can anyone provide a solution for this? Using sed
would be preferable but I'm open to solutions with awk
and perl
as well.
CodePudding user response:
With perl,
perl -pe 's#[0-9] #`./num2words.sh $&`#eg' file1.txt
CodePudding user response:
This might work for you (GNU sed):
sed -E 's#[0-9] #$(./num2words.sh &)#g;T;s/.*/echo "&"/e' file
Put numbers into $(./num2words.sh x)
where x
is a number.
Bail out if no substitution.
Put the whole line double quotes and then echo it out using the e
flag to evaluate the pattern space.
N.B. There maybe unwanted side effects.