I am looking to read a file with names. I am trying to make sure that every name is capitalized properly. An example of the desired outcome
cat names.txt
joHn smiTH
MichAel jAckson
Hello World
annie-marie
When running the code:
./namefixer.bash names.txt fixedNames.txt
cat fixedNames.txt
John Smith
Michael Jackson
Hello World
Annie-Marie
Any Help?
My current code looks like this:
#!/bin/bash
while IFS= read -r line
do
"$line" |tr '[:upper:]' '[:lower:]'
for word in $line
do
mv -- "$word" "${word^}"
done
$line>>$2
done < $1
You could do this with GNU sed:
This captures a "word" character (
\w) in the first capture group, and then as many "word" characters as possible in the second capture group (\w*), and uses the special\uand\Lsequences to control casing in the substitution string. Thegflag applies the change globally for each line (instead of just for the first match).If it really has to fit the exact command you show, you could make these the contents of
namefixer.bash: